Lesson 3 of 18 · User
Files and directories: touch, mkdir, cp, mv, rm
Objective
Create, copy, move, rename and delete files and directories from the command line, knowing the options that prevent disasters.
Key points
touchcreates an empty file,mkdircreates a directory (-pto create a whole branch).cpcopies (the original stays),mvmoves or renames (the original disappears).rmdeletes permanently: there is no recycle bin on the command line.- For a directory:
cp -r,rm -r.rmdironly deletes an empty directory. -iasks for confirmation,-vshows what is being done. Use them while you are a beginner.
1. Creating: touch and mkdir
touch
touch creates an empty file if it does not exist. If it already exists, it does not change its content; it only sets its modification date to the current time.
touch commandes.log # creates an empty file
touch lundi.txt mardi.txt # several files at once
ls -l commandes.log # size 0
mkdir
mkdir factures # creates a directory
mkdir clients fournisseurs # several directories
mkdir archives/2026 # ERROR if archives does not exist
mkdir -p archives/2026/janvier # -p creates missing parents, no error if they exist
mkdir -pv archives/2026/fevrier # -v shows each directory created
With bash braces, you can create a whole structure in one line:
mkdir -p archives/2026/{janvier,fevrier,mars}
# bash turns this into: archives/2026/janvier archives/2026/fevrier archives/2026/mars
2. Copying: cp
cp stock.csv stock-sauvegarde.csv # copy with a new name
cp stock.csv archives/ # copy into a directory, same name
cp stock.csv archives/stock-janvier.csv # copy into a directory, new name
cp a.txt b.txt c.txt archives/ # several files → the last target must be a directory
cp -r site/ site-sauvegarde/ # -r (recursive): required to copy a directory
cp -i stock.csv archives/ # -i: asks before overwriting an existing file
cp -p stock.csv archives/ # -p: keeps the date, permissions and owner
cp -a site/ site-sauvegarde/ # -a (archive): -r + -p + links, the most faithful copy
Warning: by default,
cpoverwrites without warning a file with the same name at the destination (unless acp -ialias exists, as for root on Rocky / RHEL).
3. Moving and renaming: mv
On Linux, renaming = moving to a new name. So the standard command to rename is mv (a rename command also exists, but its syntax varies from one distribution to another: avoid it at first).
mv vieux.tmp ancien.tmp # renames within the same directory
mv ancien.tmp archives/ # moves into a directory
mv vieux.tmp archives/ancien.tmp # moves AND renames
mv dossier1 dossier2 # a directory moves too, without the -r option
mv -i stock.csv archives/ # asks before overwriting
mv -n stock.csv archives/ # never overwrites (no clobber)
An mv on the same disk is instant, even for a 50 GB directory: Linux only changes the name in the disk's directory index; it does not copy the data again.
4. Deleting: rm and rmdir
rm vieux.tmp # deletes a file
rm -i *.tmp # asks for confirmation for each file
rm -v vieux.tmp # shows what is deleted
rmdir brouillons # deletes an EMPTY directory (otherwise: Directory not empty)
rm -r brouillons # deletes a directory AND all its content
rm -rf brouillons # -f: asks no questions, ignores missing files
There is no recycle bin. A file deleted with rm is lost (unless you have a backup). Adopt three habits:
- Read the command again before pressing Enter, especially with
-rand*. - First run an
lswith the same pattern:ls *.tmp, thenrm *.tmp. - Watch out for spaces:
rm -rf / srv/boutique(with a space after the/) would target the root. Recent versions ofrmrefuse to delete/without the--no-preserve-rootoption, but do not rely on this safety net.
5. Wildcards (globbing)
The shell replaces certain characters with the list of matching names, before running the command:
| Pattern | Matches | Example |
|---|---|---|
* |
any sequence of characters (even empty) | *.csv → all CSV files |
? |
exactly one character | f?.txt → f1.txt, fA.txt |
[abc] |
one character from the list | essai[12].txt |
[0-9] |
one digit | ticket-[0-9][0-9].txt |
ls *.csv # all .csv files in the current directory
cp *.csv archives/2026/ # copies them all
rm essai?.txt # deletes essai1.txt, essai2.txt… but not essai10.txt
* does not include hidden files (those starting with a dot).
6. An end-to-end example
Youssef runs a grocery shop in Fès. Every month, he archives his stock file:
cd /srv/boutique
mkdir -p archives/2026 # prepare the storage
cp -p stock.csv archives/2026/stock-janvier.csv # keep a dated copy
mv vieux.tmp archives/ancien.tmp # put away a forgotten file
ls brouillons # check what will be deleted
rm -r brouillons # then delete it
ls -R archives # check the result
Command summary
| Command | Role | Key option |
|---|---|---|
touch f |
create an empty file / update its date | — |
mkdir d |
create a directory | -p parents |
cp src dst |
copy | -r directories, -a faithful copy, -i confirmation |
mv src dst |
move / rename | -i, -n |
rm f |
delete | -r directories, -i confirmation, -f force |
rmdir d |
delete an empty directory | — |
Common pitfalls
- Forgetting
-rto copy a directory:cp: -r not specified; omitting directory. - Overwriting a file with
cpormvwithout noticing: use-ior-n. rm -rfwith an empty variable in a script:rm -rf "$DOSSIER"/*becomesrm -rf /*if the variable is empty. Always check your variables.- Confusing
rmdirandrm -r:rmdiris deliberately cautious and refuses a non-empty directory.
Lab
On this Ubuntu 24.04 lab, a small shop keeps its files in /srv/boutique. In order, you will:
- create an empty order log file;
- create the year's archive directory in one command;
- archive a copy of the stock (the original must stay);
- put away and rename an old temporary file;
- delete the drafts directory.
Run ls -R /srv/boutique at the start and at the end to see how far you have come.
Quiz
- What is the difference between
cpandmv? - What does
mkdir -p a/b/cdo ifadoes not exist? - How do you rename
rapport.txttorapport-2026.txt? - Why can
rmdir brouillonsfail, and what should you use instead? - What does
touchdo to a file that already exists?
Answers
cpcreates a copy and leaves the original;mvmoves (or renames): the original disappears from its old location.- It creates
a, thena/b, thena/b/c, without error. mv rapport.txt rapport-2026.txt- Because the directory is not empty; you need
rm -r brouillons(after checking its content). - It does not touch the content; it updates the modification date.
Reference sources: Debian Reference (free licence), Ubuntu Server Guide (CC BY-SA 3.0), Red Hat Enterprise Linux documentation (CC BY-SA 3.0). Original MasterLinux text.