Site icon AranaCorp

Some Linux terminal commands

In this tutorial, we will look at some essential Linux commands for using a Linux machine and a Raspberry Pi in particular.

Machine information

uname allows you to obtain system information (kernel name, kernel version, OS, etc.)

pi@raspberrypi:~ $ uname
Linux
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.97+ #1294 Thu Jan 30 13:10:54 GMT 2020 armv6l GNU/Linux
pi@raspberrypi:~ $ uname --all
Linux raspberrypi 4.19.97+ #1294 Thu Jan 30 13:10:54 GMT 2020 armv6l GNU/Linux
pi@raspberrypi:~ $ 

lshw is used to get the hardware configuration. You can use the -short option to display the summary (does not work with Raspberry Pi)

On Raspberry Pi, you can use the cat /proc/cpuinfo command

pi@raspberrypi:~ $ cat /proc/cpuinfo
processor	: 0
model name	: ARMv7 Processor rev 4 (v7l)
BogoMIPS	: 38.40
Features	: half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xd03
CPU revision	: 4

df -h allows to know the free space on the memory card (very useful with raspberry pi)

pi@raspberrypi:~ $ df -h
Sys. de fichiers Taille Utilisé Dispo Uti% Monté sur
/dev/root          3,3G    3,1G   13M 100% /
devtmpfs           181M       0  181M   0% /dev
tmpfs              185M       0  185M   0% /dev/shm
tmpfs              185M    5,2M  180M   3% /run
tmpfs              5,0M    4,0K  5,0M   1% /run/lock
tmpfs              185M       0  185M   0% /sys/fs/cgroup
/dev/mmcblk0p6     253M     53M  200M  21% /boot
tmpfs               37M       0   37M   0% /run/user/1000

Linux Super User Command

sudo est la commande qui permet de passer en super utilisateur ou administrateur. Généralement, utiliser avec une autre commande et le mot de passe de la session.

To be used when the command returns the error “13 : Permission denied

Linux installation commands

The apt-get command allows you to install software and packages on the machine.

In particular, downloading and installing OS updates

sudo apt-get update
sudo apt-get upgrade

apt-get is also used to install or remove packages

sudo apt-get install 
sudo apt-get remove 
sudo apt-get purge -y 

Finally, two commands to free up memory space

sudo apt-get autoremovesudo apt-get clean

La commande wget permet le téléchargement de fichier distant et peut aussi permettre de télécharger des packages à installer

Example: to download the Arduino software

cd /tmp
wget https://downloads.arduino.cc/arduino-1.8.9-linux64.tar.xz
mkdir -p ~/opt/
tar -xf arduino-1.8.9-linux64.tar.xz -C ~/opt/
cd $HOME/opt/arduino-1.8.9
./install.sh

Linux commands to navigate through a folder

The pwd command allows to know in which directory you are

pi@raspberrypi:~ $ pwd
/home/pi

ls allows you to list all the folders and files contained in the directory where you are

pi@raspberrypi:~ $ ls
2020-05-27-144933_1824x984_scrot.png  Desktop    MagPi    Public
2020-05-27-144941_1824x984_scrot.png  Documents  Modèles  Téléchargements
Applications                          Images     Musique  Vidéos

cd allows to move in a folder :

pi@raspberrypi:~ $ cd Desktop
pi@raspberrypi:~/Desktop $ W

The syntax of this command is as follows: we put “cd”, then a space and finally the name of the folder we want to go to. There are different additions that modify the basic command:

cd ..                       ##Permet de revenir dans le dossier précédent 
cd                          ##Permet de revenir à la racine
cd Bureau/Work              ##Permet d'aller dans le dossier Work qui se trouve dans le dossier Bureau
cd ../..                    ##Permet de remonter deux dossiers plus haut dans l'arborescence

To create directories

mkdir to create a folder

pi@raspberrypi:~/Desktop $ ls
reset_usb.py  rpi-arduino-i2c.py  rpi-arduino-serial.py  rpi-pca9685.py
pi@raspberrypi:~/Desktop $ mkdir Work
pi@raspberrypi:~/Desktop $ ls
reset_usb.py  rpi-arduino-i2c.py  rpi-arduino-serial.py  rpi-pca9685.py  Work

rmdir to delete a folder

pi@raspberrypi:~/Desktop $ rmdir Work
pi@raspberrypi:~/Desktop $ ls
reset_usb.py  rpi-arduino-i2c.py  rpi-arduino-serial.py  rpi-pca9685.py

rm -R to delete a folder and all its contents

To read a file

cat to display the whole file

pi@raspberrypi:~ $ cat /var/log/syslog
Mar 18 15:40:32 raspberrypi rsyslogd:  [origin software="rsyslogd" swVersion="8.1901.0" x-pid="300" x-info="https://www.rsyslog.com"] rsyslogd was HUPed
Mar 18 15:40:34 raspberrypi rsyslogd:  [origin software="rsyslogd" swVersion="8.1901.0" x-pid="300" x-info="https://www.rsyslog.com"] rsyslogd was HUPed
Mar 18 15:40:34 raspberrypi systemd[1]: logrotate.service: Succeeded.
Mar 18 15:40:34 raspberrypi systemd[1]: Started Rotate log files.
Mar 18 15:40:37 raspberrypi systemd[1]: systemd-hostnamed.service: Succeeded.
Mar 18 15:40:45 raspberrypi systemd[1]: man-db.service: Succeeded.
Mar 18 15:40:45 raspberrypi systemd[1]: Started Daily man-db regeneration.

head to read the header of a file

pi@raspberrypi:~ $ head  -n 10 /var/log/syslog
Mar 18 15:40:32 raspberrypi rsyslogd:  [origin software="rsyslogd" swVersion="8.1901.0" x-pid="300" x-info="https://www.rsyslog.com"] rsyslogd was HUPed
Mar 18 15:40:34 raspberrypi rsyslogd:  [origin software="rsyslogd" swVersion="8.1901.0" x-pid="300" x-info="https://www.rsyslog.com"] rsyslogd was HUPed
Mar 18 15:40:34 raspberrypi systemd[1]: logrotate.service: Succeeded.
Mar 18 15:40:34 raspberrypi systemd[1]: Started Rotate log files.
Mar 18 15:40:37 raspberrypi systemd[1]: systemd-hostnamed.service: Succeeded.
Mar 18 15:40:45 raspberrypi systemd[1]: man-db.service: Succeeded.
Mar 18 15:40:45 raspberrypi systemd[1]: Started Daily man-db regeneration.
Mar 18 15:40:47 raspberrypi dbus-daemon[633]: [session uid=1000 pid=633] Activating via systemd: service name='org.gtk.vfs.UDisks2VolumeMonitor' unit='gvfs-udisks2-volume-monitor.service' requested by ':1.7' (uid=1000 pid=750 comm="pcmanfm --desktop --profile LXDE-pi ")
Mar 18 15:40:47 raspberrypi systemd[531]: Starting Virtual filesystem service - disk device monitor...
Mar 18 15:40:50 raspberrypi dbus-daemon[633]: [session uid=1000 pid=633] Successfully activated service 'org.gtk.vfs.UDisks2VolumeMonitor'

tail to read the end of the file

pi@raspberrypi:~ $ tail -f /var/log/syslog
Mar 18 16:00:09 raspberrypi kernel: [ 1254.094894] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:10:06 raspberrypi kernel: [ 1850.887447] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:10:07 raspberrypi kernel: [ 1851.909164] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:14:14 raspberrypi kernel: [ 2098.887584] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:14:15 raspberrypi kernel: [ 2099.925466] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:14:16 raspberrypi kernel: [ 2100.949293] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:17:01 raspberrypi CRON[1461]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 18 16:18:38 raspberrypi kernel: [ 2362.996869] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:18:39 raspberrypi kernel: [ 2364.016852] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!
Mar 18 16:18:40 raspberrypi kernel: [ 2365.040711] ICMPv6: NA: bc:a8:a6:e7:a1:c6 advertised our address 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 on wlan0!

To create and edit a file

It is possible to create and edit files directly from the terminal. This is convenient, especially when accessing the microcontroller remotely like a Raspberry Pi Headless.

touch MON_FICHIER.py        ##Permet de créer un fichier 
nano MON_FICHIER.py         ##Permet de créer un fichier (si il n'existe pas encore) et d'ouvrir un éditeur de texte intégré à la console Linux

First of all, the “nano” command allows you to open a file via a text editor integrated into the terminal console. The “nano” command opens this file and creates it if the file does not exist.

To copy, move, delete a file

mv pour déplacer un fichier

mv  [fichier_répertoire_source]  [fichier_répertoire_cible]

cp pour copier un fichier

cp [option]   

Example:

cp -r /path/to/source/ /destination/ #Pour copier le dossier source dans le dossier destination
cp -r /path/to/source/* /destination/ #Pour copier le contenu du dossier source dans le dossier destination

rm pour supprimer un fichier ou un dossier

 rm /répertoire/nom_du_fichier

To search for a string in a file or directory

find To find a file

pi@raspberrypi:~ $ sudo find / -name syslog
find: ‘/run/user/1000/gvfs’: Permission non accordée
/run/systemd/journal/syslog
/var/log/syslog

To find text in a file, use the grep or fgrep command

pi@raspberrypi:~ $ grep Arduino /var/log/syslog
Mar 18 17:10:22 raspberrypi kernel: [ 5466.943038] usb 1-1: Product: Arduino Uno
Mar 18 17:10:22 raspberrypi kernel: [ 5466.943049] usb 1-1: Manufacturer: Arduino (www.arduino.cc)
pi@raspberrypi:~ $ grep tty /var/log/syslog
Mar 18 17:10:22 raspberrypi kernel: [ 5467.184359] cdc_acm 1-1:1.0: ttyACM0: USB ACM device

To do a recursive search for text from the current directory, use fgrep -ri

 fgrep -ri mystring * # Pour rechercher "mystring" dans tous les types de fichiers de manière récursive

N.B.: Grep uses regular expressions (regexp) which means that you can specify your search. To practice with regular expressions you can use this site.

Process management

top (or htop) is the equivalent of the task manager in Windows. It gives you an idea of the health of the machine and the active processes.

ps -aux to display running processes

pi@raspberrypi:~ $ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.2  2.0  32724  7832 ?        Ss   15:39   0:10 /sbin/init spla
root         2  0.0  0.0      0     0 ?        S    15:39   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        I<   15:39   0:03 [kworker/0:0H-k
root         6  0.0  0.0      0     0 ?        I<   15:39   0:00 [mm_percpu_wq]
root         7  0.0  0.0      0     0 ?        S    15:39   0:01 [ksoftirqd/0]
root         8  0.0  0.0      0     0 ?        S    15:39   0:00 [kdevtmpfs]
root         9  0.0  0.0      0     0 ?        I<   15:39   0:00 [netns]
root        11  0.0  0.0      0     0 ?        S    15:39   0:00 [khungtaskd]
root        12  0.0  0.0      0     0 ?        S    15:39   0:00 [oom_reaper]
root        13  0.0  0.0      0     0 ?        I<   15:39   0:00 [writeback]

combine with grep to find particular processes

pi@raspberrypi:~ $ ps -aux | grep python
pi        1817  0.8  1.5  12660  5860 pts/1    S+   17:21   0:00 python
pi        1820  0.0  0.4   7348  1880 pts/0    S+   17:21   0:00 grep --color=auto python

kill/killall to delete a process once you have found the pid with grep (here 1817 for the python process)

pi@raspberrypi:~ $ ps -aux | grep python
pi        1817  0.8  1.5  12660  5860 pts/1    S+   17:21   0:00 python
pi        1820  0.0  0.4   7348  1880 pts/0    S+   17:21   0:00 grep --color=auto python
pi@raspberrypi:~ $ kill -9 1817

Result if a python session is opened in another terminal

pi@raspberrypi:~ $ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Processus arrêté
pi@raspberrypi:~ $ 

Identification of USB ports

lsusb to get the list of usb devices

pi@raspberrypi:~ $ lsusb
Bus 001 Device 003: ID 2341:0001 Arduino SA Uno (CDC ACM)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

dmesg | grep tty to get the detected devices

dmesg | grep tty
[    0.000000] console [tty0] enabled
[    1.537553] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    1.537762] 00:07: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A

ls /dev/tty* to get the list of devices (including USB ports)

pi@raspberrypi:~ $ ls /dev/tty*
/dev/tty    /dev/tty16  /dev/tty24  /dev/tty32  /dev/tty40  /dev/tty49  /dev/tty57  /dev/tty8
/dev/tty0   /dev/tty17  /dev/tty25  /dev/tty33  /dev/tty41  /dev/tty5   /dev/tty58  /dev/tty9
/dev/tty1   /dev/tty18  /dev/tty26  /dev/tty34  /dev/tty42  /dev/tty50  /dev/tty59  /dev/ttyACM0
/dev/tty10  /dev/tty19  /dev/tty27  /dev/tty35  /dev/tty43  /dev/tty51  /dev/tty6   /dev/ttyAMA0
/dev/tty11  /dev/tty2   /dev/tty28  /dev/tty36  /dev/tty44  /dev/tty52  /dev/tty60  /dev/ttyprintk
/dev/tty12  /dev/tty20  /dev/tty29  /dev/tty37  /dev/tty45  /dev/tty53  /dev/tty61  /dev/ttyS0
/dev/tty13  /dev/tty21  /dev/tty3   /dev/tty38  /dev/tty46  /dev/tty54  /dev/tty62
/dev/tty14  /dev/tty22  /dev/tty30  /dev/tty39  /dev/tty47  /dev/tty55  /dev/tty63
/dev/tty15  /dev/tty23  /dev/tty31  /dev/tty4   /dev/tty48  /dev/tty56  /dev/tty7

Linux commands for the network

hostname allows you to obtain the name or IP address of the machine

pi@raspberrypi:~ $ hostname
raspberrypi
pi@raspberrypi:~ $ hostname -i
127.0.1.1
pi@raspberrypi:~ $ hostname -I
192.168.1.43 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1 

The ifconfig command is used to obtain information about the network, including IP addresses.

pi@raspberrypi:~ $ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Boucle locale)
        RX packets 17  bytes 1004 (1004.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17  bytes 1004 (1004.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.43  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2001:861:3742:78b0:76b1:2f81:bb6e:5bd1  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::1d5d:81c0:3c34:73cd  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:d5:83:aa  txqueuelen 1000  (Ethernet)
        RX packets 13878  bytes 1570890 (1.4 MiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 6020  bytes 1933262 (1.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ping is useful to test the network communication. You can put an IP address or a web address as an argument (add the option -c 10 to execute only 10 requests)

ping -c 10 192.168.1.43
PING 192.168.1.43 (192.168.1.43) 56(84) bytes of data.
64 bytes from 192.168.1.43: icmp_seq=1 ttl=64 time=0.393 ms
64 bytes from 192.168.1.43: icmp_seq=2 ttl=64 time=0.300 ms
64 bytes from 192.168.1.43: icmp_seq=3 ttl=64 time=0.276 ms
64 bytes from 192.168.1.43: icmp_seq=4 ttl=64 time=0.278 ms
64 bytes from 192.168.1.43: icmp_seq=5 ttl=64 time=0.278 ms
64 bytes from 192.168.1.43: icmp_seq=6 ttl=64 time=0.280 ms
64 bytes from 192.168.1.43: icmp_seq=7 ttl=64 time=0.344 ms
64 bytes from 192.168.1.43: icmp_seq=8 ttl=64 time=0.339 ms
64 bytes from 192.168.1.43: icmp_seq=9 ttl=64 time=0.355 ms
64 bytes from 192.168.1.43: icmp_seq=10 ttl=64 time=0.356 ms

--- 192.168.1.43 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 318ms
rtt min/avg/max/mdev = 0.276/0.319/0.393/0.046 ms

arp returns a list of devices connected to the network

pi@raspberrypi:~ $ arp -a
bbox.lan (192.168.1.254) at 48:83:c7:59:27:8c [ether] on wlan0
pi@raspberrypi:~ $ arp -e
Adresse                  TypeMap AdresseMat          Indicateurs           Iface
bbox.lan                 ether   48:83:c7:59:27:8c   C                     wlan0

Turn off the machine

shutdown to stop the system properly

halt to shut down the system by stopping all running processes

sudo reboot to reboot

Sources

Exit mobile version