Skip to main content

Viewing and editing files


Table of contents

  1. Displaying content: cat, less, more
  2. Start and end of a file: head, tail
  3. Searching in files: grep
  4. The nano editor for beginners
  5. Introduction to vim
  6. 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

OptionEffect
-nNumber all lines
-bNumber non-empty lines
-AShow invisible characters
-sCompress multiple empty lines
warning

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:

KeyAction
Space or fNext page
bPrevious page
or jNext line
or kPrevious line
gBeginning of the file
GEnd of the file
/textSearch for "text"
nNext occurrence
NPrevious occurrence
qQuit
tip

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

CommandUse case
catSmall files, concatenation
lessLarge files, navigation
moreSimple 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
DevOps

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

OptionEffect
-iIgnore case
-nShow line numbers
-cCount occurrences
-vInvert (lines that do NOT contain)
-rRecursive (in subfolders)
-lShow only the file names
-wWhole word only
-A nShow n lines after
-B nShow n lines before
-C nShow 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

ShortcutAction
Ctrl + OSave (Write Out)
Ctrl + XQuit
Ctrl + KCut the line
Ctrl + UPaste
Ctrl + WSearch
Ctrl + \Search and replace
Ctrl + GHelp
Ctrl + CCursor position

Typical workflow

  1. Open: nano fichier.txt
  2. Edit the content
  3. Save: Ctrl + O then Enter
  4. Quit: Ctrl + X
tip

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

ModeUsageAccess
NormalNavigation, commandsEsc
InsertWrite texti, a, o
CommandSave, quit:
VisualSelect textv

Survival commands

# Ouvrir vim
vim fichier.txt

In vim:

CommandAction
iInsert mode (before cursor)
aInsert mode (after cursor)
EscReturn to normal mode
:wSave
:qQuit
:wqSave and quit
:q!Quit without saving
ddDelete a line
yyCopy a line
pPaste
uUndo
/textSearch

Minimal vim workflow

  1. Open: vim fichier.txt
  2. Enter insert mode: press i
  3. Write your text
  4. Return to normal mode: Esc
  5. Save and quit: type :wq then Enter
Classic pitfall

If you are "stuck" in vim:

  1. Press Esc several times
  2. Type :q! and Enter to quit without saving
KeyMovement
hLeft
jDown
kUp
lRight
ggBeginning of the file
GEnd of the file
0Beginning of line
$End of line
wNext word
bPrevious 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

  • cat displays everything, less lets you navigate
  • head -n N = first N lines, tail -n N = last N
  • tail -f = monitor a file in real time
  • grep = search for text (-i ignores case, -r recursive)
  • nano = simple editor (Ctrl+O saves, Ctrl+X quits)
  • vim = powerful editor (i insert, Esc normal, :wq saves and quits)

🔝 Back to table of contents


← Previous chapter | Next chapter: Help and documentation →