The rm (remove) command is used to delete files and directories. Deleting files is a fundamental operation for a sysadmin. Be careful with the command because it does not prompt for a confirmation unless the -i option is added.
The command unlinks a file name in the filesystem from its associated data. The space occupied by the file is then available for future disk writes. As soon as the space is “released”, the operating system will use it for something else. Please note, if you are a Windows user, the Linux command line does not have a recycle bin.
Common options for the rm command are:
- -f or –force ignore nonexistent files and never prompt before removing
- -i prompt for each file
- -r or –recursive remove directories and their contents recursively
- -v or –verbose output to the terminal what is being done
- -I prompt once before removing more than three files, or when removing recursively. If you are new to Linux I recommend running all rm commands with the -I option.
If you want to delete hidden files run shopt -s dotglob before running rm or see command (7) below.
1) Delete a single file
rm myfile.txt
2) Delete multiple files
rm myfile1.txt myfile2.txt myfile3.txt
3) Force a prompt before each delete action
rm -i myfile1.txt
4) Remove a folder with all its contents (including all interior folders)
rm -rf /path/to/directory
5) Remove the contents of the folder (including all interior folders) but not the folder itself
rm -rf /path/to/directory/*
6) Remove all files/folders from the working directory (be careful)
rm -rf *
7) Remove a folder with all its contents (including interior folders) and hidden files/directories
rm -rf /path/to/directory/{*,.*}
8) To remove all the “files” from inside a folder (not removing interior folders)
rm -f /path/to/directory/{*,.*}