How to use the rm command in Linux

In this article, you can get a better and detailed understanding of the “rm” and “rmdir” commands and how they operate with various available options.

Updated: 05 Mar, 23 by Susith Nonis 6 Min

List of content you will read in this article:

Linux allows the users to leverage its command-line terminal for carrying out various operations on files and folders. One of the most common tasks in Linux is removing or deleting files and folders. For this, Linux has commands like rm and rmdir for removing files and directories respectively. These commands will work similarly to the del and deltress commands in Windows and DOS systems.

Linux commands (rm and rmdir) comes with various options that we will be discussing in this article.

While using this command, you must understand that running rm and rmdir commands will not move the files and directories to the trash. They will be immediately removed from your system leaving no option to recover them. If you delete the files and directories accidentally then you cannot restore them. So make sure before permanently deleting the files, you create some backup.

Let’s understand the use of rm and rmdir commands along with various options available in Linux.

  • One of the simple tasks of deleting a file is from the current working directory. You only need to run the rm command along with the file name that you want to delete. You can consider the below example.

rm file_name_1.txt

  • If the desired file that you want to delete is not present in the current working directory, then you need to enter the complete path to that file name as shown below.

rm ./path/to/the/file/file_name1.txt

  • Not only this, you can use the rm command for deleting multiple files that you can specify after the rm command with a space between each file name as shown below.

rm file_name_2.txt file_name_3.txt

  • If you want to delete a bunch of files that are having the same extension for example files ending with “.pdf”. You can run the below “rm” command along with the extension. This command will help in deleting all the pdf files in the current working directory.

rm *.pdf

  • This command will delete all the files that is having the pdf extension. But if you enter the wildcard character like “?”, it will delete the files that is having a single character extension.

rm *.?

If a specific file is having write-protected permissions, then you will be asked for confirmation for deleting that file permanently. If you want to proceed, you can press “y” and hit enter accordingly.

  • For reducing the risk of using the “rm” command directly to deleting the file permanently, it is better that you use the “-i” option along with the “rm” command to ask you to confirm the deletion.

rm -i *.dat

  • If you want to delete a file forcefully then you can use the “-f” option along with the “rm” command even the specific file has the write-protected permission for that file.

rm -f filename

  • If you are removing the directories that are empty then you can specify the “-d” option along with the rm command. Also, you can use the wildcards (* and ?) within the names of the directory similar to the filenames.

rm -d directory

  • If you want to delete multiple directories then you can specify different directories after the rm command separated by spaces as shown below.

rm -d directory1 directory2 /path/to/directory3

  • If you are removing a directory that is not empty, you can use the “-r” option along with the “rm” command for the recursive operation. It will remove the directories along with the files within it.

rm -r directory1 directory2 directory3

  • If the directory is having write-protected permission then you will be prompted for permission to delete that directory. For deleting directories that are not empty along with suppressing the prompt, you can use the following command with “-r” and “-f” options.

rm -rf directory

  • For running the above command, you have to be careful as it will delete the file without any prompt and cannot be recovered. So make sure you have the proper understanding of the directory structure. For solving this problem, you can use the tree command.

But first, you need to install the package for t=using tree command on your Unix or Linux distribution.

sudo apt-get install tree

If you run the “tree” command, you will get a tree diagram of the directory structure and the files stored within it.

tree

For deleting the directory, you can also use the “rmdir” command. But the main difference between using “rm” and “rmdir” commands is that the “rmdir” command will help in deleting those directories that are empty. It cannot be used for deleting files.

  • In Linux, one of the easiest commands for deleting the empty directory, you can use “rmdir” command. But you cannot use this command for deleting multiple directories as it is possible with “rm” command. 

rmdir directory

  • For deleting multiple empty directories, you can use the following format of the command.

rmdir directory1 directory2 directory3

  • If the specific directory that you want to delete is not in the current working directory, then you can provide the path of that directory as shown below.

rmdir /path/to/directory

  • If you use the “rmdir” command for deleting the directory that is not empty, you will get an error message.

If you want to delete the directory along with its parent directory, then you can use the “-p” option with the “rmdir” command. It is possible as the “rmdir” command will start from the target directory and then move to the parent directory. The target directory now will be empty so it can be deleted by the “rmdir” command. The process will keep on repeating till the path is provided to the rmdir command.

Linux system provides you with flexible and the most powerful commands that help in performing various actions. One of the powerful commands is the “rm” and “rmdir” that can be used for deleting files and directories. There are different available options to ensure the different functionality of these commands. 

In this article, you can get a better and detailed understanding of the “rm” and “rmdir” commands and how they operate with various available options.

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.