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.

Make directories

mkdir creates one or more directories:

[student@servera ~]$ mkdir reports drafts
[student@servera ~]$ ls
drafts  reports

It 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.

[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  reports

rm -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'? y

Check your understanding