Managing files and folders
Table of contents
- Creating files and folders
- Copying with cp
- Moving and renaming with mv
- Deleting with rm and rmdir
- Wildcards and patterns
- Hands-on exercises
1 - Creating files and folders
Create an empty file: touch
# Créer un fichier vide
touch fichier.txt
# Créer plusieurs fichiers
touch fichier1.txt fichier2.txt fichier3.txt
# Mettre à jour la date de modification (si le fichier existe)
touch fichier_existant.txt
If the file already exists, touch updates its modification date without changing its content.
Create a directory: mkdir
# Créer un dossier
mkdir mon_dossier
# Créer plusieurs dossiers
mkdir dossier1 dossier2 dossier3
# Créer une arborescence complète (option -p)
mkdir -p projets/2024/janvier
# Créer avec permissions spécifiques
mkdir -m 755 dossier_public
The -p (parents) option
# Sans -p : erreur si le parent n'existe pas
mkdir projets/2024/janvier
# mkdir: cannot create directory 'projets/2024/janvier': No such file or directory
# Avec -p : crée toute l'arborescence
mkdir -p projets/2024/janvier
# Crée projets/, puis 2024/, puis janvier/
Create a file with content
# Avec echo et redirection
echo "Bonjour" > fichier.txt
# Avec cat et heredoc
cat > fichier.txt << EOF
Ligne 1
Ligne 2
Ligne 3
EOF
🔝 Back to table of contents
2 - Copying with cp
cp = copy
Syntax
cp [options] source destination
Copy files
# Copier un fichier
cp fichier.txt copie.txt
# Copier vers un autre répertoire
cp fichier.txt /tmp/
cp fichier.txt /tmp/nouveau_nom.txt
# Copier plusieurs fichiers vers un dossier
cp fichier1.txt fichier2.txt /tmp/
Copy directories
# Copier un répertoire (option -r obligatoire)
cp -r dossier/ copie_dossier/
# Copier récursivement
cp -r projets/ /backup/projets/
Important options
| Option | Effect |
|---|---|
-r or -R | Recursive (mandatory for folders) |
-i | Ask for confirmation before overwriting |
-v | Verbose mode (shows what is copied) |
-p | Preserve permissions and dates |
-u | Copy only if the source is more recent |
-n | Do not overwrite existing files |
Practical examples
# Copier avec confirmation
cp -i important.txt /backup/
# Copier en préservant les attributs
cp -p script.sh /usr/local/bin/
# Copier avec affichage détaillé
cp -rv projet/ /backup/projet/
# Copier seulement les fichiers plus récents
cp -u *.log /archive/
By default, cp overwrites existing files without asking! Use -i to be careful.
🔝 Back to table of contents
3 - Moving and renaming with mv
mv = move
mv is used to move AND rename files and folders.
Syntax
mv [options] source destination
Rename
# Renommer un fichier
mv ancien_nom.txt nouveau_nom.txt
# Renommer un dossier
mv ancien_dossier nouveau_dossier
Move
# Déplacer un fichier
mv fichier.txt /tmp/
# Déplacer et renommer
mv fichier.txt /tmp/nouveau_nom.txt
# Déplacer plusieurs fichiers
mv fichier1.txt fichier2.txt /destination/
# Déplacer un dossier
mv mon_dossier/ /autre/emplacement/
Important options
| Option | Effect |
|---|---|
-i | Ask for confirmation before overwriting |
-v | Verbose mode |
-n | Do not overwrite existing files |
-u | Move only if the source is more recent |
-f | Force (no confirmation) |
Practical examples
# Déplacer avec confirmation
mv -i important.txt /backup/
# Renommer plusieurs fichiers (avec for)
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
# Déplacer tous les logs
mv -v *.log /archive/
# Organiser par extension
mv *.jpg images/
mv *.pdf documents/
mv does not copy the data, it just changes the location. It is instantaneous on the same file system!
🔝 Back to table of contents
4 - Deleting with rm and rmdir
rmdir - Delete an empty directory
# Supprimer un dossier vide
rmdir dossier_vide
# Supprimer plusieurs dossiers vides
rmdir dossier1 dossier2
# Supprimer l'arborescence vide
rmdir -p a/b/c # Supprime c, puis b, puis a (si vides)
rmdir ONLY works on empty directories. To delete a folder with content, use rm -r.
rm - Delete files and folders
# Supprimer un fichier
rm fichier.txt
# Supprimer plusieurs fichiers
rm fichier1.txt fichier2.txt fichier3.txt
# Supprimer un répertoire et son contenu
rm -r dossier/
# Forcer la suppression (sans confirmation)
rm -f fichier.txt
# Combinaison courante
rm -rf dossier/
rm options
| Option | Effect |
|---|---|
-r or -R | Recursive (folders and content) |
-f | Force (ignore non-existent files, no confirmation) |
-i | Ask for confirmation for each file |
-I | Ask for confirmation if >3 files |
-v | Verbose mode |
Precautions
There is NO trash bin on the command line. Once deleted, it's permanent!
# TRÈS DANGEREUX - Ne jamais faire :
rm -rf / # Supprime TOUT le système
rm -rf /* # Idem
rm -rf ~/* # Supprime tout le home
Best practices
# 1. Utiliser -i pour confirmer
rm -ri dossier/
# 2. Vérifier d'abord avec ls
ls dossier_a_supprimer/
rm -r dossier_a_supprimer/
# 3. Utiliser le chemin complet
rm -r /home/user/tmp/dossier/
# 4. Créer un alias de sécurité (dans .bashrc)
alias rm='rm -i'
🔝 Back to table of contents
5 - Wildcards and patterns
Wildcards allow you to select several files with a pattern.
The main wildcards
| Wildcard | Meaning | Example |
|---|---|---|
* | Any characters | *.txt = all .txt files |
? | A single character | fichier?.txt = fichier1.txt, fichierA.txt |
[abc] | One character among a, b, or c | fichier[123].txt |
[a-z] | One character in the range | fichier[a-z].txt |
[!abc] | None of these characters | fichier[!0-9].txt |
Examples with *
# Tous les fichiers .txt
ls *.txt
# Tous les fichiers commençant par "log"
ls log*
# Tous les fichiers contenant "2024"
ls *2024*
# Copier tous les .jpg
cp *.jpg images/
# Supprimer tous les .tmp
rm *.tmp
Examples with ?
# Fichiers avec un seul caractère avant .txt
ls ?.txt
# Résultat : a.txt, 1.txt, etc.
# Fichiers log avec un chiffre
ls log?.txt
# Résultat : log1.txt, log2.txt, etc.
Examples with []
# Fichiers avec chiffre
ls fichier[0-9].txt
# Fichiers avec lettre majuscule
ls fichier[A-Z].txt
# Fichiers avec a, b ou c
ls fichier[abc].txt
# Tout SAUF les chiffres
ls fichier[!0-9].txt
Combinations
# Tous les fichiers commençant par a-f
ls [a-f]*
# Fichiers .txt ou .log
ls *.txt *.log
# Images jpg ou png
cp *.[jp][pn]g images/
# ou plus simplement
cp *.{jpg,png} images/
Brace expansion
# Créer plusieurs fichiers
touch fichier{1,2,3,4,5}.txt
# Crée fichier1.txt, fichier2.txt, etc.
# Créer une séquence
touch fichier{01..10}.txt
# Crée fichier01.txt à fichier10.txt
# Copier avec renommage
cp fichier.txt{,.bak}
# Équivalent à : cp fichier.txt fichier.txt.bak
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Create a structure
# Créez cette arborescence :
# projet/
# ├── src/
# ├── docs/
# ├── tests/
# └── README.md
mkdir -p projet/{src,docs,tests}
touch projet/README.md
# Vérifiez
ls -la projet/
Exercise 2: Copy and move
# Préparation
mkdir -p exercice/{originaux,copies,archives}
touch exercice/originaux/fichier{1..5}.txt
# 1. Copiez tous les fichiers vers copies/
cp exercice/originaux/*.txt exercice/copies/
# 2. Déplacez fichier1.txt vers archives/
mv exercice/copies/fichier1.txt exercice/archives/
# 3. Renommez fichier2.txt en important.txt
mv exercice/copies/fichier2.txt exercice/copies/important.txt
# Vérifiez
ls -R exercice/
Exercise 3: Cleanup
# Préparation
mkdir -p nettoyage
touch nettoyage/fichier{1..10}.txt
touch nettoyage/fichier{1..5}.tmp
touch nettoyage/fichier{1..3}.log
# 1. Supprimez tous les .tmp
rm nettoyage/*.tmp
# 2. Supprimez les .log avec confirmation
rm -i nettoyage/*.log
# 3. Comptez les fichiers restants
ls nettoyage/ | wc -l
Quiz
Q1. How do you create the folder a/b/c if they don't exist?
Answer
mkdir -p a/b/c
Q2. How do you copy a folder with all its content?
Answer
cp -r dossier/ destination/
Q3. Which wildcard represents "a single character"?
Answer
? (question mark)
Q4. How do you delete a non-empty folder?
Answer
rm -r dossier/ or rm -rf dossier/
🔝 Back to table of contents
Key takeaways
touchcreates empty files,mkdircreates foldersmkdir -pcreates the whole directory treecpcopies (-rfor folders)mvmoves AND renamesrmdeletes (-rfor folders) - IRREVERSIBLE!- Wildcards:
*(everything),?(one character),[abc](list) - Always check before deleting!