×
rm and rmdir commands

rm and rmdir are the most widely used command to remove files and directories in Linux-based operating systems. If you are a new user in Linux then you should be very careful while running rm and rmdir command because you can not recover the files and directory after deleting them.

In this tutorial, we will explain how to use rm and rmdir commands to remove files and directories in Linux.

RM Command

rm also stands for remove is a command line tool used to remove objects such as, files, directories and symbolic links from the file system. You can also remove special files such as, pipes, sockets and device nodes with the rm command.

By default, rm does not remove directories. You will need to rum rm with -r or -R options recursively deletes any matching directories and their sub-directories. When you remove the file with rm, there is no way to undo it.

Basic syntax of rm command is shown below:

rm [-f] [-i] [-I] [-r] [-d] [--no-preserve-root] [--one-file-system] [--preserve-root] [FILENAME]

Where:

  • -f : This will overrides minor protection and removes the file forcefully without prompt.
  • -i : This will ask the user for confirmation before removing each file.
  • -I : This will prompt you once before removing more than three files.
  • -r : This will delete all the files and sub-directories recursively
  • -d : This will used to remove empty directory.
  • –no-preserve-root : This will not treat the root (/) directory.
  • –preserve-root : This will not remove the root (/) directory.

You should see all the options available with rm command with the following command:

rm --help

You should see the following screen:

RMDIR Command

rmdir is a command line tool used to remove an empty directory in Linux-based operating systems. rmdir command removes each directory specified with rmdir command only if these directories are empty. If there is any file in the specified directory then rmdir can not delete the directory. rmdir is very similar to the command rm -d.

Basic syntax of rmdir command is shown below:

rmdir [--ignore-fail-on-non-empty] [-p] [-v] [--help] [DIRECTORYNAME]

Where:

  • –ignore-fail-on-non-empty : Normaly when you remove the non-empty directory with rmdir command it will reports an error. Using this option with rmdir do not report a failure error.
  • -p : Each directory argument is treated as a pathname of which all components will be removed. This option remove directory and its ancestors.
  • -v : This will display the verbose output of every directory processed.
  • –help : This will disply the syntax of the command along with the various options and give a brief description about each option.

You should see all the options available with rmdir command with the following command:

rmdir --help

You should see the following screen:

Remove File with rm Command

In this section, we will demonstrate how to remove the file using rm command with some examples.

To remove a single file with name file1.txt run the following command:

rm file1.txt

To remove multiple files at once run the following command:

rm file1.txt file2.txt file3.txt file4.txt

To remove all the files in the current directory run the following command:

rm *

To remove file interactively, use option -i with rm command. This will prompt you before deleting a file.

rm -i file1.txt

You will be prompt before removing the file:

rm: remove regular empty file ‘file1.txt’? y

Type y and hit enter to remove the file.

If you want to remove more than three files and prompt only once before removing them use -I option with the rm command:

rm -I file1.txt fil2.txt file3.txt file4.txt

You will be prompt only once before removing the file:


rm: remove all arguments? y

Type y and hit enter to remove the files.

To remove multiple files with verbose output run the following command:

rm -v file1.txt fil2.txt file3.txt file4.txt

You should see the following screen:

To remove an empty directory run the following command:

rm -d dir1

To remove the directory with all the sub-directories run the following command:

rm -rf dir1

To remove a file whose name begins with a dash (-), you will need to use — with rm command:

rm -- -file1.txt

Remove Directory with rmdir Command

rmdir is a command line tool used to remove an empty directory in Linux-based operating systems. You can not remove the directory that contains files with rmdir command.

In this section, we will explain how to remove directory by running rmdir command with some example.

First, let’s create an empty directory using the following command:

mkdir dir1

Now, remove the directory with rmdir command:

rmdir dir1

To remove multiple directories run the following command:

rmdir dir1 dir2 dir3

Next, try to create a directory with file inside it.

mkdir dir1
touch dir1/file1.txt

Now, remove the directory with rmdir command:

rmdir dir1

You will get an error message as shown below:

rmdir: failed to remove ‘dir1’: Directory not empty

That means rmdir command can only remove an empty directory.

If you don’t want to get an error message after removing non-empty directory run the following command:

rmdir --ignore-fail-on-non-empty dir1

To remove the directories with verbose output run the following command:

rmdir -v dir1 dir2 dir3

You should see the following output:

rmdir: removing directory, ‘dir1’
rmdir: removing directory, ‘dir2’
rmdir: removing directory, ‘dir3’

Next, create a directory named dir1 with child directories dir2 and dir3:

mkdir -p dir1/dir2/dir3

In this example, dir3 is empty, dir2 only contained dir3 and dir1 only contained dir2 directory. You can remove all these directories using -p option with rmdir command:

rmdir -p dir1/dir2/dir3

If the files and directories are owned by other users then you can not remove them. However, you can remove them by running the command as a superuser:

sudo rmdir dir1
sudo rm -rf dir1

You will be prompt to provide sudo user password to remove the directory.

Conclusion

Now you have enough knowledge of how to use rm and rmdir command to remove files and directories in Linux. Be careful when removing files and directories, because after removing the file, it cannot be recovered easily. For more information, you can visit the official documentation at rmdir and rm