Viewing and editing files
Table of contents
- Displaying content: cat, less, more
- Start and end of a file: head, tail
- Searching in files: grep
- The nano editor for beginners
- Introduction to vim
- Hands-on exercises
1 - Displaying content: cat, less, more
cat - Display all the content
cat = concatenate
# Afficher un fichier
cat fichier.txt
# Afficher plusieurs fichiers
cat fichier1.txt fichier2.txt
# Afficher avec numéros de ligne
cat -n fichier.txt
# Afficher les caractères invisibles
cat -A fichier.txt
cat options
| Option | Effect |
|---|---|
-n | Number all lines |
-b | Number non-empty lines |
-A | Show invisible characters |
-s | Compress multiple empty lines |
cat displays the ENTIRE file at once. For large files, use less or more.
less - Paginated viewing
less displays the file page by page, letting you navigate.
less fichier.txt
less /var/log/syslog
Navigation in less:
| Key | Action |
|---|---|
Space or f | Next page |
b | Previous page |
↓ or j | Next line |
↑ or k | Previous line |
g | Beginning of the file |
G | End of the file |
/text | Search for "text" |
n | Next occurrence |
N | Previous occurrence |
q | Quit |
less is preferable to more because it lets you navigate in both directions.
more - Basic viewing
more fichier.txt
more is older and limited: you can only move forward.
Comparison
| Command | Use case |
|---|---|
cat | Small files, concatenation |
less | Large files, navigation |
more | Simple alternative to less |
🔝 Back to table of contents
2 - Start and end of a file: head, tail
head - Display the beginning
# 10 premières lignes (par défaut)
head fichier.txt
# N premières lignes
head -n 20 fichier.txt
head -20 fichier.txt
# N premiers octets
head -c 100 fichier.txt
tail - Display the end
# 10 dernières lignes (par défaut)
tail fichier.txt
# N dernières lignes
tail -n 20 fichier.txt
tail -20 fichier.txt
# Suivre les modifications en temps réel
tail -f /var/log/syslog
The -f (follow) option
# Suivre un fichier de log en temps réel
tail -f /var/log/syslog
# Suivre plusieurs fichiers
tail -f /var/log/*.log
# Suivre avec nombre de lignes initial
tail -n 50 -f /var/log/syslog
tail -f is indispensable for monitoring logs in real time. Use Ctrl + C to stop.
Useful combinations
# Lignes 10 à 20 d'un fichier
head -n 20 fichier.txt | tail -n 10
# Tout sauf les 5 premières lignes
tail -n +6 fichier.txt
# Tout sauf les 5 dernières lignes
head -n -5 fichier.txt
🔝 Back to table of contents
3 - Searching in files: grep
grep = Global Regular Expression Print
Basic usage
# Rechercher "erreur" dans un fichier
grep "erreur" fichier.txt
# Rechercher dans plusieurs fichiers
grep "erreur" *.log
# Rechercher récursivement
grep -r "erreur" /var/log/
Common options
| Option | Effect |
|---|---|
-i | Ignore case |
-n | Show line numbers |
-c | Count occurrences |
-v | Invert (lines that do NOT contain) |
-r | Recursive (in subfolders) |
-l | Show only the file names |
-w | Whole word only |
-A n | Show n lines after |
-B n | Show n lines before |
-C n | Show n lines before and after |
Practical examples
# Ignorer la casse
grep -i "error" fichier.log
# Avec numéros de ligne
grep -n "warning" fichier.log
# Compter les occurrences
grep -c "error" *.log
# Lignes ne contenant PAS "debug"
grep -v "debug" application.log
# Mot entier "root" (pas "rooted")
grep -w "root" /etc/passwd
# Contexte autour de la correspondance
grep -C 3 "error" application.log
Grep with pipe
# Filtrer la sortie d'une commande
ps aux | grep nginx
# Chercher dans l'historique
history | grep "apt"
# Filtrer les fichiers listés
ls -la | grep ".txt"
Basic regular expressions
# Commence par "Error"
grep "^Error" fichier.log
# Termine par "failed"
grep "failed$" fichier.log
# Contient un chiffre
grep "[0-9]" fichier.log
# Adresse IP (simplifié)
grep -E "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" fichier.log
🔝 Back to table of contents
4 - The nano editor for beginners
nano is a simple text editor, ideal for getting started.
Open nano
# Ouvrir un fichier (existant ou nouveau)
nano fichier.txt
# Ouvrir avec numéros de ligne
nano -l fichier.txt
The nano interface
GNU nano 6.2 fichier.txt
Contenu du fichier ici...
│
│
│
│
│
^G Help ^O Write Out ^W Where Is ^K Cut ^U Paste
^X Exit ^R Read File ^\ Replace ^J Justify ^T Spell
Essential shortcuts
| Shortcut | Action |
|---|---|
Ctrl + O | Save (Write Out) |
Ctrl + X | Quit |
Ctrl + K | Cut the line |
Ctrl + U | Paste |
Ctrl + W | Search |
Ctrl + \ | Search and replace |
Ctrl + G | Help |
Ctrl + C | Cursor position |
Typical workflow
- Open:
nano fichier.txt - Edit the content
- Save:
Ctrl + OthenEnter - Quit:
Ctrl + X
The ^ in the shortcuts means Ctrl. So ^X = Ctrl + X.
🔝 Back to table of contents
5 - Introduction to vim
vim (Vi IMproved) is a powerful editor, standard on all Unix/Linux systems.
Why learn vim?
- Present on all Linux servers
- Extremely fast once mastered
- Indispensable for DevOps
The modes of vim
| Mode | Usage | Access |
|---|---|---|
| Normal | Navigation, commands | Esc |
| Insert | Write text | i, a, o |
| Command | Save, quit | : |
| Visual | Select text | v |
Survival commands
# Ouvrir vim
vim fichier.txt
In vim:
| Command | Action |
|---|---|
i | Insert mode (before cursor) |
a | Insert mode (after cursor) |
Esc | Return to normal mode |
:w | Save |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
dd | Delete a line |
yy | Copy a line |
p | Paste |
u | Undo |
/text | Search |
Minimal vim workflow
- Open:
vim fichier.txt - Enter insert mode: press
i - Write your text
- Return to normal mode:
Esc - Save and quit: type
:wqthenEnter
If you are "stuck" in vim:
- Press
Escseveral times - Type
:q!andEnterto quit without saving
Navigation in normal mode
| Key | Movement |
|---|---|
h | Left |
j | Down |
k | Up |
l | Right |
gg | Beginning of the file |
G | End of the file |
0 | Beginning of line |
$ | End of line |
w | Next word |
b | Previous word |
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Viewing
# Créez un fichier de test
seq 1 100 > nombres.txt
# 1. Affichez tout le fichier
cat nombres.txt
# 2. Affichez les 5 premières lignes
head -n 5 nombres.txt
# 3. Affichez les 5 dernières lignes
tail -n 5 nombres.txt
# 4. Visualisez avec less
less nombres.txt
# (appuyez sur q pour quitter)
Exercise 2: Searching with grep
# Créez un fichier de log fictif
cat > test.log << EOF
2024-01-15 10:00:00 INFO Application started
2024-01-15 10:01:00 DEBUG Processing request
2024-01-15 10:02:00 ERROR Connection failed
2024-01-15 10:03:00 WARNING Low memory
2024-01-15 10:04:00 INFO Request completed
2024-01-15 10:05:00 ERROR Timeout occurred
EOF
# 1. Trouvez toutes les erreurs
grep "ERROR" test.log
# 2. Comptez les erreurs
grep -c "ERROR" test.log
# 3. Trouvez tout sauf DEBUG
grep -v "DEBUG" test.log
# 4. Trouvez ERROR avec contexte
grep -C 1 "ERROR" test.log
Exercise 3: Editing with nano
# 1. Créez un nouveau fichier avec nano
nano mon_script.sh
# 2. Tapez ce contenu :
#!/bin/bash
echo "Bonjour, Linux !"
date
# 3. Sauvegardez (Ctrl+O, Entrée)
# 4. Quittez (Ctrl+X)
# 5. Vérifiez le contenu
cat mon_script.sh
Quiz
Q1. How do you display the last 20 lines of a file?
Answer
tail -n 20 fichier.txt or tail -20 fichier.txt
Q2. How do you follow a log file in real time?
Answer
tail -f fichier.log
Q3. How do you quit vim without saving?
Answer
Esc then :q!
Q4. How do you search for "error" without regard to case?
Answer
grep -i "error" fichier.txt
🔝 Back to table of contents
Key takeaways
catdisplays everything,lesslets you navigatehead -n N= first N lines,tail -n N= last Ntail -f= monitor a file in real timegrep= search for text (-iignores case,-rrecursive)nano= simple editor (Ctrl+Osaves,Ctrl+Xquits)vim= powerful editor (iinsert,Escnormal,:wqsaves and quits)