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.txtDeleting 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 directoryDeleting 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
Exercise: Bash File Operations
What is the difference between "mv" and "cp" when used on a file?