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.txtSuccess is silent. A missing file produces an error:
$ rm nothere
rm: cannot remove 'nothere': No such file or directoryAsk first with -i
-i prompts before every removal. Type y to confirm:
$ rm -i notes.txt
rm: remove regular file 'notes.txt'? ySome 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 directoryTo remove an empty directory, use rmdir photos. To remove a directory with everything inside it, use -r:
$ rm -r photosForce
-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 *.logBe 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
- Make a directory called
junkand create a few files in it. - Remove one with
rm -i. - Preview a wildcard with
ls, then remove the matches. - 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.