There are two methods when it comes to removing files or directories in Linux. If you are using Linux GUI distributions, you can delete the file directly by right click and select the “Delete” option. While the command line method for deleting files is a little bit tricky. The command-based method is to use rm command in Linux to remove a file. It is proven to be useful when we have to delete large amounts of data using the command line.
In this guide, we will explain how you can use the rm command to remove multiple files and directories. We will cover commonly used options available in the rm command.
Table of Contents
How to use the rm command
To use the rm command, follow the syntax mentioned below.
rm [OPTIONS] FILENAME
If you by mistake, type the only rm without any option mentioned or a file name, it will not remove any directories or files.
To delete any single file you have to type rm with a filename that you want to delete.
In above example, we assumed that the file you want to delete is filename.zip
Once you hit the ENTER button, if the file is successfully deleted the command will not return any output message. If you remove any write-protected files, the command will ask confirmation to whether you want to delete the file or not.
rm: remove write-protected regular empty file 'filename'?
To allow the deletion of write-protected file type y and press ENTER to delete the file. If you want to get rid of this message, use -f option with the rm command.
rm -f filename.zip
To print out the verbose output of the file you want to delete, use -v option with the rm command.
rm -v filename.zip
How to remove multiple files using rm command
It is possible to remove multiple files in Linux using rm command. While the command and syntax have a slight change. Whatever files you want to delete, provide each file name separated by space.
rm filename.zip sample-video.mkv database.csv
Use rm to delete file matching with regular expressions
Let’s assume you want to delete files starting with any word but ends with .db extension. For that rm can be useful to delete the file by extension.
Remove directories using rm command
rm command helps you to delete files as well as directories too. To remove empty directories you have to use -d option with directory name as an argument.
rm -d emptydirectoryname
To remove non-empty directories and it’s all files recursively, use -r option with the rm command.
rm -r nonemptydirectory
Ask for confirmation before removing the file in rm command
Sometimes, it happens like you accidentally delete useful files using rm command. So, to avoid such a disaster, one novice Linux user always uses -i option with the rm command. This always prompts you whether the given file should be deleted or not. Once you confirm by typing Y and press ENTER, it will proceed.
Remove protected directory in Linux without prompts
To remove write-protected directory in Linux without receiving prompts you have to use -rf option together.
Conclusion on using rm command
The rm command is very useful when you have proper knowledge of it and you are aware of what you are doing with it. While using multiple options together with rm command, make sure to check command before you hit ENTER. We suggest you always use -i option to get a prompt before deleting files.