1. Moving around #
Paths and navigation.
Absolute and relative paths #
An absolute path starts from the root (/etc/nginx/nginx.conf), a relative path starts from the current directory (../logs). ~ means the home directory, . the current directory, .. the parent.
pwd # où suis-je ?
cd /var/log ; cd - # aller puis revenir au répertoire précédent
ls -lah # liste détaillée, fichiers cachés, tailles lisibles
ls -lt | head # fichiers les plus récents
tree -L 2 /etc/nginx # arborescence sur 2 niveauxThe prompt and history #
history | grep systemctl # retrouver une commande
!! # relancer la dernière commande (sudo !! si oubli de sudo)
Ctrl+R # recherche incrémentale dans l'historique
Ctrl+A / Ctrl+E # début / fin de ligne
Tab # complétion des commandes et chemins2. Handling files #
Create, copy, move, delete.
Basic commands #
mkdir -p projet/{src,docs,tests} # créer une arborescence
touch notes.txt # créer un fichier vide
cp -a /etc/nginx /etc/nginx.bak # copier en préservant droits et dates
mv rapport.txt archives/ # déplacer ou renommer
rm -i fichier ; rm -r dossier # supprimer (sans corbeille !)
ln -s /srv/app/current app # lien symbolique
stat fichier ; file fichier # métadonnées, type réelCommon mistake:rm -rfasks for no confirmation and there is no trash. Check the variable beforerm -rf "$DIR"/*: if it is empty, the command targets the root.
Wildcards (globbing) #
| Pattern | Matches |
|---|---|
*.log | all files ending in .log |
app?.conf | app1.conf, appA.conf |
[0-9]* | starts with a digit |
{a,b}.txt | a.txt and b.txt (expansion, even if they do not exist) |
It is the shell that expands wildcards before running the command: echo *.log shows what the command will receive.
Reading a file #
cat fichier # tout afficher
less /var/log/syslog # paginer (/ pour chercher, q pour quitter)
head -n 20 ; tail -n 50
tail -f /var/log/nginx/error.log # suivre en temps réel
wc -l fichier # nombre de lignes3. Redirections and pipes #
Assembling small commands.
Standard streams #
| Stream | Number | Redirection |
|---|---|---|
| Standard input (stdin) | 0 | < file |
| Standard output (stdout) | 1 | > file (overwrite), >> file (append) |
| Standard error (stderr) | 2 | 2> errors.log, 2>&1 (to stdout) |
find / -name '*.conf' 2>/dev/null # masquer les erreurs de permission
./backup.sh > backup.log 2>&1 # tout dans un fichier
./backup.sh 2>&1 | tee backup.log # afficher et enregistrer
sudo tee /etc/motd <<'EOF' # écrire un fichier root (heredoc)
Serveur de production : soyez prudent.
EOFThe pipe #
Good to know:sudo echo x > /etc/filefails: the redirection is done by your shell, without root rights. Useecho x | sudo tee /etc/file.
4. Searching and transforming text #
find, grep, sed, awk and friends.
Finding files #
find /var/log -name '*.log' -mtime -1 # modifiés depuis 24 h
find / -xdev -type f -size +500M 2>/dev/null # gros fichiers sur la partition racine
find /srv -user www-data -perm -o+w # fichiers modifiables par tous
find /tmp -type f -mtime +7 -delete # nettoyer
locate nginx.conf # recherche rapide par index (updatedb)Searching inside files #
grep -i error /var/log/syslog
grep -rn 'listen' /etc/nginx/ # récursif avec numéros de ligne
grep -v '^#' sshd_config | grep -v '^$' # configuration sans commentaires ni lignes vides
grep -E 'error|fail' -c app.log # expression étendue, compter
zgrep 'Out of memory' /var/log/syslog*.gzTransforming #
cut -d: -f1,7 /etc/passwd # colonnes 1 et 7, séparateur :
sort -t: -k3 -n /etc/passwd # trier numériquement sur la 3e colonne
sort | uniq -c | sort -rn # compter les occurrences
tr 'a-z' 'A-Z' < fichier # remplacer des caractères
sed -i.bak 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config # remplacer en place, avec sauvegarde
sed -n '10,20p' fichier # afficher les lignes 10 à 20
awk -F: '$3 >= 1000 {print $1}' /etc/passwd # utilisateurs « humains »
awk '{s += $10} END {print s/1024/1024 " Mo"}' access.log # somme d'une colonne
diff -u ancien.conf nouveau.conf # comparer deux fichiersGood practice: Before runningsed -ion a configuration file, test without-ito see the result, and keep a copy (-i.bak).
5. Archives and editing #
tar, compression and vim.
Archiving and compressing #
tar -czf etc-$(date +%F).tar.gz /etc # créer une archive gzip
tar -tzf etc-2026-10-26.tar.gz | head # lister le contenu
tar -xzf archive.tar.gz -C /tmp/restore # extraire ailleurs
tar -cJf logs.tar.xz /var/log/app # compression xz (plus forte)
zip -r site.zip /srv/www ; unzip site.zip
gzip -9 gros.log ; zcat gros.log.gz | lessSurviving in vim #
| Key | Action |
|---|---|
i | switch to insert mode |
Escape | back to normal mode |
:w / :q / :wq / :q! | save / quit / both / quit without saving |
/pattern, n | search, next occurrence |
dd, yy, p | cut, copy, paste a line |
u | undo |
:%s/old/new/g | replace everywhere |
nano is simpler for beginners (shortcuts shown at the bottom). Choose your default editor with sudo update-alternatives --config editor or the EDITOR variable.
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Linux system architecture: kernel, boot and directory tree
Understand how a Linux server works: kernel and user space, the UEFI, GRUB, initramfs and systemd...
Processes, memory and performance: monitoring and troubleshooting a Linux server
Understand and control Linux processes (ps, top, signals, nice, jobs), interpret load, memory and...
Ansible: ad-hoc commands
Run immediate actions on a fleet of servers with ansible: the ping, command, shell, copy, apt, se...
Ansible: generating dynamic files with Jinja2 and facts
Create dynamic configuration files: Jinja2 templates, variables, facts, filters, loops, condition...
Understanding Google Cloud: regions, projects, hierarchy, tools and pricing
Google Cloud basics: regions and zones, the organization, folders and projects hierarchy, APIs to...
Understanding Azure: regions, resource hierarchy, tools and pricing
Microsoft Azure basics: geographies, regions, region pairs and availability zones, the hierarchy ...
PowerShell for Windows administrators
Master PowerShell to administer Windows: cmdlets and help, object pipeline, filters and formats, ...
Windows networking: IP, DNS, DHCP, firewall and remote access
Configure and troubleshoot Windows networking with PowerShell, deploy a DHCP server, master Windo...
Users, groups, NTFS permissions and SMB shares
Manage local accounts and groups, understand UAC, apply NTFS permissions with inheritance, create...