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

September 30, 2023 / How-to Guide

In this article, we will explain how to delete a Non-Empty directory using the rm command.

  1. To delete non-empty directories, use the rm command, which is primarily for file removal. You can combine it with options such as -r, -rf, and -d to delete directories. Below is the command syntax:
    rm option FileOrFolderName
  2. To remove both the directory and its contents, use the -r or recursive option.
    For example, you can execute the following command that will delete “Simple-Directory,” including all its subdirectories and files:Note: When using the rm -r command to delete a directory, be aware that it will also delete all of its contents. Exercise caution and ensure you have a backup if you need to recover any data.

    rm -r Simple-Directory
  3. While attempting to delete a write-protected directory, the command line interface will prompt for confirmation. To delete a directory in Linux without any confirmation you can use the -rf option.
    rm -rf Simple-Directory
  4. In Linux, you can use the rm command to delete empty directories as well. Just remember to include the -d option.
    rm -d Simple-Directory
  5. Like the rmdir command, you can also use rm to delete multiple directories. Simply include the directory names as additional arguments in the command line:
    rm -r Directory_1 Directory_2 Directory_3

By following this method, you can delete a non-empty directory using the rm command. If you are looking for an article, on how to Delete an Empty Directory in Linux,  please refer to the corresponding article.

Spread the love