Bash Remove Files

rm deletes files permanently from the command line, with no trash bin to recover from — which makes it the most dangerous everyday command in bash.

Deleting a File

rm removes the files you name immediately. Unlike deleting a file through a graphical file manager, there is no recycle bin or trash folder involved — the moment the command completes, the file's data is unlinked and generally unrecoverable.

Delete a file

$ rm old-draft.txt
$ ls
final-report.txt

Deleting Multiple Files

List several filenames to delete them all in one command. Wildcards like *.tmp are commonly combined with rm to clear out a whole category of files at once — but expand the wildcard with ls first to double check exactly what it matches.

Delete a batch of temp files

$ ls *.tmp
cache1.tmp  cache2.tmp  cache3.tmp
$ rm *.tmp
$ ls *.tmp
ls: cannot access '*.tmp': No such file or directory

Deleting Directories with -r

rm refuses to delete a directory by default. Adding -r (recursive) tells it to delete the directory and everything inside it, at every nesting level, without asking about each file individually.

Delete a folder and its contents

$ rm -r old-build/
$ ls
final-report.txt
  • rm file.txt — delete a single file
  • rm a.txt b.txt c.txt — delete several files at once
  • rm -r folder/ — delete a directory and everything inside it
  • rm -i file.txt — ask for confirmation before deleting
  • rm -f file.txt — force delete, suppressing prompts and missing-file errors
Note: rm -rf is the single most destructive command in everyday bash use: -r deletes directories recursively and -f suppresses every confirmation and error, so a mistyped path or an extra space (rm -rf / folder instead of rm -rf /folder) can silently wipe far more than you intended, with no undo and no trash to restore from.
Note: Before running rm -r on anything you're not 100% sure about, run ls on the same path first, and consider rm -ri to confirm each deletion individually while you're still building confidence.

Exercise: Bash File Operations

What is the difference between "mv" and "cp" when used on a file?