Read

Module 2 · Files and directories

Deleting with rm and rmdir, safely

Removing files with rm, empty directories with rmdir and whole trees with rm -r, plus the habits that keep a typo from becoming a disaster.

What you will learn

  • Delete files with `rm` and empty directories with `rmdir`.
  • Delete a directory tree with `rm -r` and know what `-f` really does.
  • Adopt habits that make deleting safe: look first, use `-i`, avoid stray spaces.

rm — *remove* — deletes files. There is no recycle bin and no undo: once rm returns, the name is gone, and as soon as no other name or program refers to the data, its space is reused. That is not a reason to fear rm; it is a reason to read the line once before pressing Enter. In this VM everything lives in RAM and a reload restores it all, which makes it the ideal place to practise.

~% cd /root/lab/l15
l15% ls -F
empty/  junk.txt  keep.txt  old-site/
l15% rm junk.txt
l15% rm old-site
rm: 'old-site' is a directory
l15% rmdir old-site
rmdir: 'old-site': Directory not empty
l15% rmdir empty
l15% rm -r old-site
l15% ls -F
keep.txt

Directories: rmdir or rm -r

Plain rm refuses directories. You have two tools. rmdir removes a directory only if it is empty, which makes it the safe choice: if something is still inside, it says Directory not empty and does nothing. rmdir -p a/b/c removes c, then b, then a, stopping at the first one that is not empty. rm -r (*recursive*) removes a directory and everything under it, at every depth, without asking. Use rmdir when you believe a directory is empty, and let it prove you right.

CommandRemovesAsks?
rm FILE...Files (and links). Refuses directories.No
rm -i FILE...The same, one confirmation per file.Yes, y/n
rmdir DIRAn empty directory only.No, but fails safely
rm -r DIRA directory and everything inside.No
rm -f FILEFiles; no error if they do not exist.Never

What -f really means

-f (*force*) does not mean "delete harder". It means "never prompt, and do not complain about files that are not there". That is useful in scripts that clean up something which may or may not exist, and it is why rm -rf is the classic command for wiping a tree. It is also why rm -rf is dangerous: it removes every safety message you might have seen. -v does the opposite of hiding things and prints removed 'x' for each item.

Habits that save you

  • Look first. Run ls with exactly the same arguments, then press the up arrow and change ls into rm.
  • Prefer rmdir for directories you think are empty, and rm -i when you are unsure.
  • Watch the spaces. rm -r /root/lab /tmp and rm -r /root/lab/tmp differ by one character and remove very different things.
  • Use absolute paths for anything recursive, so the result does not depend on where you happen to be.

Commands in this lesson

CommandWhat it does
rm FILE...Delete files. No undo.
rm -i FILE...Ask before each deletion.
rmdir DIRDelete an empty directory; fails safely otherwise.
rmdir -p A/B/CDelete a chain of empty directories.
rm -r DIRDelete a directory and everything in it.
rm -f FILENever prompt; ignore missing files.

Quiz

  1. Where does a file go after `rm`?

    • To a trash folder in your home
    • Nowhere: it is gone, there is no undo
    • To /tmp until the next reboot
  2. `rmdir photos` answers `Directory not empty`. What has been deleted?

    • Nothing
    • The files inside, but not the directory
    • Everything
  3. What does `-f` add to `rm`?

    • It deletes files that are normally protected from deletion
    • Never prompt, and no error for files that do not exist
    • It overwrites the data with zeros
  4. Which command deletes the directory `old` together with everything inside it?

    • `rmdir old`
    • `rm old`
    • `rm -r old`
  5. What is the safest way to check what `rm -r /root/lab/l15/old*` would delete?

    • Run it with `-f` first
    • Run `ls -d /root/lab/l15/old*` with the same arguments first
    • There is no way to know

Practice

  1. Delete the file /root/lab/l15/junk.txt. Leave `keep.txt` alone.

  2. /root/lab/l15/empty is an empty directory. Remove it with the command that refuses to delete directories that still contain something.

  3. Delete the directory /root/lab/l15/old-site and everything inside it (it has files and a subdirectory). Nothing else in /root/lab/l15 must go.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.