← Linux and the Command Line Lesson 7 of 51 · 10 XP

rm — deleting

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

rm file.txt                # delete a file
rm a.txt b.txt             # several
rm -i file.txt             # ask for confirmation
rmdir empty_dir            # delete an EMPTY folder
rm -r folder               # delete a folder with all its contents (recursive)
rm -rf folder              # -f: don't ask and don't complain about missing files

The terminal has no recycle bin. What's deleted is deleted forever.

rm -rf is the most dangerous command in Linux. One stray-space mistake like rm -rf / home/kai/tmp (a space after /!) deletes the whole system. Survival rules:

  • before rm -rf, run ls with the same path and look at what will be deleted;
  • don't use rm -rf with variables without checking them: if $DIR happens to be empty, rm -rf "$DIR/" turns into rm -rf /;
  • rm -ri asks about every file.

Patterns (globs) are expanded by the shell: * — any characters, ? — one character.

rm *.log                   # all files ending in .log
rm report_?.txt            # report_1.txt, report_a.txt…

Task. The starter code has created files. Delete all the .log files and the whole cache folder, but keep notes.txt.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.