Linux Command-Line Fundamentals
Exit

Lesson 3 of 18 · User

Files and directories: touch, mkdir, cp, mv, rm

25 min Ubuntu 24.04

Objective

Create, copy, move, rename and delete files and directories from the command line, knowing the options that prevent disasters.

Key points

  • touch creates an empty file, mkdir creates a directory (-p to create a whole branch).
  • cp copies (the original stays), mv moves or renames (the original disappears).
  • rm deletes permanently: there is no recycle bin on the command line.
  • For a directory: cp -r, rm -r. rmdir only deletes an empty directory.
  • -i asks for confirmation, -v shows 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, cp overwrites without warning a file with the same name at the destination (unless a cp -i alias 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:

  1. Read the command again before pressing Enter, especially with -r and *.
  2. First run an ls with the same pattern: ls *.tmp, then rm *.tmp.
  3. Watch out for spaces: rm -rf / srv/boutique (with a space after the /) would target the root. Recent versions of rm refuse to delete / without the --no-preserve-root option, 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 -r to copy a directory: cp: -r not specified; omitting directory.
  • Overwriting a file with cp or mv without noticing: use -i or -n.
  • rm -rf with an empty variable in a script: rm -rf "$DOSSIER"/* becomes rm -rf /* if the variable is empty. Always check your variables.
  • Confusing rmdir and rm -r: rmdir is 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:

  1. create an empty order log file;
  2. create the year's archive directory in one command;
  3. archive a copy of the stock (the original must stay);
  4. put away and rename an old temporary file;
  5. delete the drafts directory.

Run ls -R /srv/boutique at the start and at the end to see how far you have come.

Quiz

  1. What is the difference between cp and mv?
  2. What does mkdir -p a/b/c do if a does not exist?
  3. How do you rename rapport.txt to rapport-2026.txt?
  4. Why can rmdir brouillons fail, and what should you use instead?
  5. What does touch do to a file that already exists?
Answers
  1. cp creates a copy and leaves the original; mv moves (or renames): the original disappears from its old location.
  2. It creates a, then a/b, then a/b/c, without error.
  3. mv rapport.txt rapport-2026.txt
  4. Because the directory is not empty; you need rm -r brouillons (after checking its content).
  5. 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.

Practical exercises

To do on Ubuntu 24.04

The online terminal is coming soon. Meanwhile, do these tasks on your own Linux machine, a virtual machine or WSL.

  1. Create an empty file /srv/boutique/commandes.log
    Show a hintThe touch command creates an empty file if it does not exist.
    Show the solution
    touch /srv/boutique/commandes.log

    Check : test -f /srv/boutique/commandes.log

  2. Create the directory /srv/boutique/archives/2026 in one go (the archives directory does not exist yet).
    Show a hintmkdir needs the -p option to also create the missing parent directories.
    Show the solution
    mkdir -p /srv/boutique/archives/2026

    Check : test -d /srv/boutique/archives/2026

  3. Copy /srv/boutique/stock.csv into /srv/boutique/archives/2026 under the name stock-janvier.csv (the original must stay in place).
    Show a hintcp source destination — the destination can include the new name.
    Show the solution
    cp /srv/boutique/stock.csv /srv/boutique/archives/2026/stock-janvier.csv

    Check : grep -q pain /srv/boutique/archives/2026/stock-janvier.csv && test -f /srv/boutique/stock.csv

  4. Move /srv/boutique/vieux.tmp into /srv/boutique/archives and rename it ancien.tmp
    Show a hintmv moves AND renames in a single command.
    Show the solution
    mv /srv/boutique/vieux.tmp /srv/boutique/archives/ancien.tmp

    Check : test -f /srv/boutique/archives/ancien.tmp && test ! -e /srv/boutique/vieux.tmp

  5. Delete the /srv/boutique/brouillons directory with all its content.
    Show a hintrmdir refuses a non-empty directory; you need rm with the -r option. Read the command again before pressing Enter!
    Show the solution
    rm -r /srv/boutique/brouillons

    Check : test -d /srv/boutique/archives && test ! -e /srv/boutique/brouillons

Official references consulted: Debian Reference — chapitre 1, commandes de manipulation de fichiers ; Ubuntu Server Guide — notions de base. Original MasterLinux text.