Chapter 3: Files and directories
Create, copy, move and remove
Five commands do almost all file housekeeping: mkdir, cp, mv, rm and rmdir. They are simple, but they act immediately and there is no wastebasket, so this lesson also covers the habits that keep a typo from becoming a lost afternoon.
- Create directories, including whole paths at once, with
mkdirandmkdir -p. - Copy and move files and directories with
cp,cp -randmv, and predict where the result ends up. - Remove files and directories with
rm,rm -randrmdir, safely.
Make directories
mkdir creates one or more directories:
[student@servera ~]$ mkdir reports drafts
[student@servera ~]$ ls
drafts reportsIt fails if the directory already exists, or if the directory it should go in doesn't exist yet. -p (parents) creates the whole path, as many levels as needed, and doesn't complain about directories that already exist:
[student@servera ~]$ mkdir backup/2026
mkdir: cannot create directory ‘backup/2026’: No such file or directory
[student@servera ~]$ mkdir -p archive/2026/q1
[student@servera ~]$ ls -R archive
archive:
2026
archive/2026:
q1
archive/2026/q1:Copy and move
cp SOURCE DESTINATION copies; mv SOURCE DESTINATION moves. A move within the same file system is really a rename, which is why there is no separate rename command: renaming is moving a file to a new name. Both follow the same rule about the destination. Select a command to see the result:
Where does the file end up?: Select a command. The result depends only on whether the destination is an existing directory.
- If the destination is an existing directory, the file goes inside it, keeping its name.
- Otherwise the destination is the new name for the file.
- With several sources, the last argument must be a directory:
cp a.txt b.txt c.txt reports/.
[student@servera ~]$ touch drafts/plan.txt drafts/budget.txt drafts/notes.txt
[student@servera ~]$ cp drafts/plan.txt reports/plan-v1.txt
[student@servera ~]$ mv -v drafts/budget.txt reports/
renamed 'drafts/budget.txt' -> 'reports/budget.txt'
[student@servera ~]$ mv drafts/notes.txt drafts/meeting-notes.txt
[student@servera ~]$ ls drafts reports
drafts:
meeting-notes.txt plan.txt
reports:
budget.txt plan-v1.txt-v (verbose) makes cp and mv say what they did.
Directories need -r to copy
cp copies files only, unless you add -r (recursive), which copies a directory with everything inside it. mv moves directories without any option.
[student@servera ~]$ cp drafts reports
cp: -r not specified; omitting directory 'drafts'
[student@servera ~]$ cp -rv reports archive/2026/q1/
'reports' -> 'archive/2026/q1/reports'
'reports/budget.txt' -> 'archive/2026/q1/reports/budget.txt'
'reports/plan-v1.txt' -> 'archive/2026/q1/reports/plan-v1.txt'Overwriting
If the destination file exists, cp and mv overwrite it without asking. Add -i (interactive) to be asked first. Copies get the current time as their time stamp; cp -p keeps the original's times and permissions, which matters when you back up configuration.
Remove
rm removes files. It refuses directories unless you add -r, which removes a directory and everything below it. rmdir removes only empty directories, which makes it a safe way to tidy up:
[student@servera ~]$ rmdir drafts
rmdir: failed to remove 'drafts': Directory not empty
[student@servera ~]$ rm drafts
rm: cannot remove 'drafts': Is a directory
[student@servera ~]$ rm -r drafts
[student@servera ~]$ ls
archive reportsrm -i asks before each removal, and -f (force) never asks and doesn't complain about missing files. If you give both, -f wins.
[student@servera ~]$ rm -ri archive
rm: descend into directory 'archive'? y
rm: descend into directory 'archive/2026'? y
...output omitted...
rm: remove directory 'archive'? yCheck your understanding
- reports is an existing directory. What does cp plan.txt reports do?
- How do you rename notes.txt to minutes.txt?
- cp prints "-r not specified; omitting directory". What happened?
- Which command creates /srv/web/site/assets when none of those directories exist?
- Which removes the directory old only if it is empty?
- You deleted the wrong file with rm. Where can you get it back from?