How to use the Linux rm command to delete directories and files
The rm command for Linux is used to completely delete files and directories without making any copies. We’d, therefore, recommend you use the command carefully and make it more precise using the options available to you.
What is the Linux rm command?
If you want to delete files or complete directories forever, then the Linux command rm is exactly what you need. The rm stands for ‘remove’. When working on the terminal in Linux distributions such as Ubuntu you are likely to need the command quite often. The command is the quickest way to delete files and directories and helps you clean up folders and superfluous files from your directories. However, you need to be careful! Unlike with other operating systems these won’t end up in the recycle bin. If you use the rm command, the deleted data will not be recoverable. Therefore, you need to be very careful when using it.
How does the rm command work?
The way the rm command works is fairly easy to explain. If you want to delete files or complete directories, you simply need to direct the system to do so. You simply tell it where to find the file or directory in the terminal and it will delete it. Alongside the permanent deletion of the data, organising your folders is another important task you can do with the command. This is because the rm command can also delete directories which still contain other data. This deletion will then remove all the documents in a folder.
What does the rm syntax look like?
The syntax for the Linux rm command looks as follows:
$ rm [Option] [File or Folder]
shellrm is the command to be carried out. The files or folders to be deleted are then added at the end. If you want to delete multiple files, you can add them one after the other. You can also change the options used by the command.
What options does the rm command have?
There are different options for the Linux rm command. These are the most important:
- -f or –force: This forces the files to be deleted and will not ask for confirmation. You should, generally avoid using this option since it has huge potential to go wrong.
- -i or –interactive: Ensures that before anything is deleted it needs to be confirmed. Only once confirmed will the corresponding file be deleted. This prevents any unwanted data being deleted.
- -r, -R or –recursive: This causes a recursive deletion. In other words alongside the actual directory to be deleted all subfolders and files are removed. However, using this option can also be dangerous.
- -v or –verbose: This will show what the command is currently doing.
Example of the Linux rm command
Finally, we will show you some practical examples of how the rm command works.
$ rm examplefile.txt
shellUse this to delete examplefile.txt.
$ rm examplefile.txt second_examplefile.txt third_examplefile.txt
shellThis command will delete all three files.
$ rm -r /Example folder
shellThis command is used to delete the example folder as well as all files and subfolders in it.
$ rm -ri /Example folder
shellBy using this command you will also delete the example folder and all files and subfolders contained within it. However, before every item is deleted you need to confirm that it should be deleted.