Terminal : commandes utiles

nora.nckm.eu

Illustration
Table des matières

Cette page liste des commandes et des scripts shell utiles.

Raccourcis

Ces raccourcis sont disponibles avec le shell fish. La recherche de fichiers nécessite en plus fzf.

Ctrl + A / E
Move to the line beginning/end
Alt + /
Jump to the previous/next word or directory
Alt + /
Switch to the previous/next command
Alt + /
Switch to the previous/next arguments
Ctrl + T
Select a file or directory
Ctrl + R
Select a command from history
Alt + C
Change current directory
Ctrl + L
Clear screen
Ctrl + U
Delete the line
Ctrl + C
Cancel the line
Alt + H
Show the command man page
Alt + S
Prepends sudo to the line
Return
Change current directory
Space
Command not saved in history
**/*.po
Glob stars

Commandes

Commandes utiles pour divers tâches sous Linux.

Gestion des paquets (Arch Linux)

📑 pacman

pacman -Syu     # mise à jour
pacman -Qdt     # paquets orphelins
pacman -Qm      # paquets étrangers
pacman -Rcsn    # désinstallation
pacman -Scc     # nettoyage
pacdiff         # gestion des fichiers de configuration pac*

Gestion des paquets (Arch User Repository)

📑 aur

git clone https://aur.archlinux.org/package.git
git pull
git show
makepkg -sirc   # syncdeps/install/rmdeps/clean

Gestion des services (Arch Linux)

📑 systemd

systemctl
systemctl --failed
systemctl list-unit-files
systemctl <start stop restart status> <unit>
systemctl <enable disable> <unit>
systemctl <reboot poweroff suspend hibernate>

systemd-analyze
systemd-analyze <blame critical-chain>
systemd-analyze plot > boot.svg

journalctl -b
journalctl -fu unit

networkctl

timedatectl
timedatectl set-ntp true

Gestion des services (Alpine Linux)

📑 openrc

rc-status
rc-service -l
rc-service <service> <start stop restart status>
rc-update show
rc-update <add del> <service>
reboot
poweroff

Accès distant

📑 ssh

ssh user@host -p 22         # connexion ssh
scp file user@host:dir      # copie fichier
sshfs user@host:dir mnt     # montage répertoire
fusermount -u mnt           # démontage répertoire

Installation clés de chiffrement

ssh-keygen -t ed25519
ssh-copy-id [-i .ssh/id_ed25519.pub] user@host

Sauvegarde distante

ssh user@host "tar -cJf save.tar.xz dir" && scp user@host:save.tar.xz .
ssh root@host "dd if=/dev/mmcblk0 | pv -pterb | gzip -1 -" | dd of=save.img.gz

Sauvegarde locale chiffrée

📑 tar, gpg

tar -cJ /home/user/doc | gpg -c -o save.tar.xz.gpg      # sauvegarde
gpg -d save.tar.xz.gpg | tar -xJ                        # restauration

Recherche de fichiers

📑 grep

grep -nri what where    # line numbers/recursive/case insensitive

Liste des plus grands répertoires et fichiers

📑 du

du -s * | sort -rn | head -n 10     # résumé
du -a * | sort -rn | head -n 10     # complet

Gestion d'une image ISO

📑 dd

dd if=/dev/sdx of=fichier.iso                   # création depuis un périphérique
dd if=fichier.iso of=/dev/sdx bs=4M && sync     # écriture sur un périphérique

Détail des partitions

📑 fdisk

fdisk -l

Liste des périphériques

📑 mount, lspci, lsusb, lsblk, lshw

mount
lspci
lsusb
lsblk
lshw

Gestion de l'énergie

📑 tlp

tlp-stat

Extinction programmée

📑 shutdown

shutdown 23h30
shutdown +60

Tâche planifiée

📑 crontab, systemd-run

systemd-run --on-active="1h 30m" --user <command>
crontab -e
    # mm  hh  DD  MM  W  /path/progam (W=weekday: 0-6 Sun=0)
    */10  *  *  *  *  $HOME/bin/script.sh

Benchmark et stress test

📑 7z, s-tui

7z b        # p7zip benchmark
s-tui       # stress test

Test de débit

Speed test en ligne : LibreSpeed.

wget http://test-debit.free.fr/8192.rnd -O /dev/null
wget http://test-debit.free.fr/1048576.rnd -O /dev/null

Wifi (Arch Linux)

Configuration de l'interface en DHCP dans le fichier /etc/systemd/network/wlan0.network :

[Match]
Name=wlan0
[Network]
DHCP=yes

Activation des services network et iwd :

systemctl start systemd-networkd.service
systemctl enable systemd-networkd.service
systemctl start iwd.service
systemctl enable iwd.service

Wifi (Alpine Linux)

Configuration de l'interface en DHCP dans le fichier /etc/network/interfaces :

auto wlan0
iface wlan0 inet dhcp

Activation des services networking et iwd :

rc-service networking restart
rc-service iwd start
rc-update add iwd boot && rc-update add dbus boot

Wifi

📑 iwd, iwctl

Contrôle de iwd avec le client iwctl :

iwctl device list
iwctl station wlan0 scan
iwctl station wlan0 get-networks
iwctl station wlan0 connect ESSID
iwctl station wlan0 show

Bluetooth

📑 doc

Contrôle du bluetooth avec le client bluetoothctl :

bluetoothctl
$ power on
$ scan on
$ agent on
$ pair mac_addr
$ trust mac_addr
$ connect mac_addr

Les commandes peuvent être lancées avec bluetoothctl -- command.

Envoi d'un email

📑 msmtp

printf "Subject: Hello\nHello World!" | msmtp user@example.com

Capture d'écran

📑 grim, slurp, swappy

grim                                    # desktop screenshot
grim -g "$(slurp)"                      # region screenshot
grim -g "$(slurp)" - | swappy -f -      # edit screenshot

Imprimante (CUPS)

📑 cups, lpr

lpr /usr/share/cups/data/testprint
lpr document.pdf

Administration via l'interface web : http://localhost:631/

Scanner (SANE)

📑 sane, scanimage

scanimage -L    # liste les appareils : "alto:libusb:001:004"

scanimage -o img.pdf -d "alto:libusb:001:004" --format=pdf --resolution=150 --progress
scanimage -o img.jpg -d "alto:libusb:001:004" --format=jpeg --resolution=150 --progress

scanimage --batch=img%d.jpg -d "alto:libusb:001:004" --format=jpeg --resolution=150 --progress
scanimage --batch=img%d.tiff -d "alto:libusb:001:004" --format=tiff --resolution=100 --progress

Manipulation de fichiers PDF (poppler)

pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf
pdfimages in.pdf out-dir
pdftotext in.pdf out.pdf

Traitement d'images

📑 doc, imagemagick, magick

magick img.jpg img.webp

magick img.png -quality 50 -define webp:lossless=true img.webp
magick img.png -quality 50 -define webp:lossless=true -scale 100x100 img.webp

magick img*.jpg -quality 100 -rotate 90 img.pdf
magick mogrify -format jpg -quality 50 -colorspace gray *.tiff

PostgreSQL

📑 doc

Installation et création d'une base de données :

$ sudo pacman -S postgresql
$ sudo -iu postgres
[postgres]$ initdb -D /var/lib/postgres/data
$ sudo systemctl start postgresql
$ sudo -iu postgres
[postgres]$ createuser --interactive # myuser
[postgres]$ createdb -O myuser myuser
$ psql
[psql]# create user USERNAME with encrypted password 'USERPASS';
[psql]# alter role USERNAME set CLIENT_ENCODING to 'utf8';
[psql]# alter role USERNAME set DEFAULT_TRANSACTION_ISOLATION to 'read committed';
[psql]# create database DBNAME;
[psql]# grant all privileges on database DBNAME to USERNAME;
[psql]# \help
[psql]# \?
[psql]# \l
[psql]# \du
[psql]# \dt
[psql]# \q

Scripts

Les scripts ci-dessous utilisent le shell fish.

Sauvegarde d'un répertoire vers un compte email

L'archive est chiffrée, découpée en plusieurs fichiers joints et envoyée via SMTP ou IMAP : dup.sh

Lancement d'un minuteur avec notification

L'état du minuteur est visible dans la barre des tâches et une notification est affichée grâce à notify-send/mako : timer.sh

Configuration dans .config/waybar/config :

"modules-right": [
    "custom/timer",
],

"custom/timer": {
    "format": " ⏲ {}",
    "interval": 60,
    "exec": "~/doc/src/timer_check.sh",
    "on-click": "~/doc/src/timer.sh 1h30m",
    "on-click-right": "~/doc/src/timer.sh off"
}
Emojis

Un commentaire sur un de mes articles ? Commencez une discussion sur ma liste de diffusion en envoyant un email à ~nora/public-inbox@lists.sr.ht [règles]