How to Delete an Empty Directory in Linux

September 29, 2023 / How-to Guide

In this article, we will explain how to delete an empty directory in Linux.

If you want to permanently remove any directory in linux, you have two main commands at your disposal. The “rmdir” or “rm -d” command is used for removing empty directories, while the “rm -r” command is used to delete the non-empty directories.

How to Remove an Empty Directory Using the rm Command.

In Linux, there is no recycle bin or trash folder. Consequently, once a file, folder, or directory is deleted, there is no way to restore them. If you accidentally delete something, the sole method for recovery is to restore it from a backup.

  1. Use the “rmdir” command that helps you to prevent such mishaps, as it functions for empty directories. If the directory contains files, executing this command will result in the following error message:
    rmdir: failed to remove ‘Directory’: Directory not empty
  2. The syntax for the rmdir command is as follows:
    rmdir option DirectoryName
  3. You can use the rmdir command without an option by removing it from the syntax. As the command line is case-sensitive, remember to type the folder name accordingly. For example, you can see a command for deleting the Simple-Directory folder:
    rmdir Simple-Directory
  4. You can delete multiple empty directories simultaneously using the rmdir command by adding the directories as additional arguments after rmdir. The syntax is as follows:
    rmdir Directory_A Directory_B Directory_C
  5. You can combine the rmdir command with various options. For example, you can use the -p option to delete a subdirectory along with its parent directory.
    For example, the following command will delete the /Directory/SubDirectory directory path:

    rmdir -p /Directory/SubDirectory

    The above command will delete the SubDirectory folder in the Directory path. Then, it will also delete the Directory folder if it is empty.

  6. The next option is -v or verbose. This option tells the command line interface to print a confirmation message to verify that rmdir has successfully deleted the specified directory. Here’s an example of this command:
    rmdir -v Simple-Directory
  7. You will get this kind of output message will appear similar to the following:
    rmdir: removing directory, ‘Simple-Directory’

In this way, you can delete an empty directory in Linux. For more information on Linux, visit our knowledge base section.

Also Read : How to Delete a Non-Empty Directory Using the rm Command

Spread the love