Skip to content

Linux Foundations / Find your way around Linux ยท Step 24 of 63

Lesson: rm (Remove)Switch
โ† Linux Foundations

Shell and files

rm (Remove)

Delete files and directories with rm, carefully, because there is no recycle bin.

rm removes files. Read this one slowly: rm has no recycle bin and no undo. Once a file is removed, assume it is gone unless you have a backup.

Removing files

$ ls
a.txt  b.txt  c.txt
$ rm a.txt
$ rm b.txt c.txt

Success is silent. A missing file produces an error:

$ rm nothere
rm: cannot remove 'nothere': No such file or directory

Ask first with -i

-i prompts before every removal. Type y to confirm:

$ rm -i notes.txt
rm: remove regular file 'notes.txt'? y

Some distributions alias rm to rm -i, particularly for root. Do not depend on that, since another machine may not.

Directories

Plain rm refuses directories:

$ rm photos
rm: cannot remove 'photos': Is a directory

To remove an empty directory, use rmdir photos. To remove a directory with everything inside it, use -r:

$ rm -r photos

Force

-f ignores missing files and never prompts. Combined as rm -rf, it deletes a whole tree without questions. That is useful and dangerous. A typo such as an extra space can change what gets removed: rm -rf project /old targets two separate things.

Safe habits

Wildcards are expanded by the shell before rm runs. Preview a pattern with ls first:

$ ls *.log
app.log  error.log
$ rm *.log

Be sure of your location with pwd, and think twice before using sudo with rm. GNU rm refuses to recurse on / by default, but that safeguard does not protect your other directories.

A file name that starts with a dash looks like an option. Prefix it with ./, for example rm ./-oddname.

Practise on throwaway files in the files and links lab.

Try it

  1. Make a directory called junk and create a few files in it.
  2. Remove one with rm -i.
  3. Preview a wildcard with ls, then remove the matches.
  4. Delete the directory with rm -r junk.

Check your understanding

rm (Remove)

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.