Note: The rm command removes the file permanently without moving it to the Trash/Recycle Bin. Hence, ensure you fully understand the rm command, or you might end up losing your important files forever.
Knowing more about rm
To get started, you might want to use –help to find out more about rm and its usage:
How to Remove a Single File
You can remove a single file using the following command:
How to Remove Multiple Files
For removing multiple files, you only need to add the names of the files, separated with a space: If you want to remove all the files with the same extension, you can use * as the placeholder. For example, to remove all “.txt” files: Similarly, to remove all files (with extension) in a directory, you can use:
How to Remove a Directory
For removing an empty directory, you can use the -d option, or rmdir command: or For director with files, you have to use the -r (recursive) option to remove a directory. This will also remove all files within the folder. Similarly, you can remove everything, including subfolders and the files within, from a directory:
Get a Prompt Before Removing a File
If you want to verify everything before removing any file, it is better to use the -i option. This option shows a confirmation prompt before removing any file from the system. Once you execute the above command, the system will ask you to press Y or N to confirm your selection. In case you are deleting more than three files with the rm command, please use the -I option instead: or
Get a Message After Removing Files
With the -v option, the rm command shows what is being done by the command: You can use both the -v and -i option to remove files interactively:
Remove a File Forcefully
The -f option overrides any minor protection of a write-protected file to remove it forcefully. You can combine this with the -r option to forcefully remove a directory and its subfolders. Note: the -rf option coupled with sudo can be a lethal command that can remove any/all files and folders in the system. Use it with care.
Remove a File Named with a Dash (-)
If you try to remove a file that has a dash in its name, you may get an error message. To deal with the above error, please use a double dash (–) that works as an “end of options” instruction for a command in Linux:
Combine Rm with Xargs Command
You can combine the rm command with the xargs command in Linux to delete many files efficiently. For instance, let’s delete the files listed in the info.txt file: