How to Delete Non-Empty Directory in Linux?

Directories always play a crucial role in storing files and sub-directories in the Operating System. Like other OSs, the old or useless directories consume a lot of space in a Linux system. To avoid such happening, the useless directories are deleted to have some free space in the system. This article will demonstrate the possible ways to delete a non-empty directory in a Linux system.

The supported content is below:

  • Method 1: How to Delete Non-Empty Directory in Linux Using Terminal?
  • Method 2: How to Delete a Non-Empty Directory in Linux Using GUI?

Method 1: How to Delete Non-Empty Directory in Linux Using Terminal?

In Linux distributions, the “rm” command is used to delete the non-empty directory; the basic syntax that is followed to delete the specified directory is provided below:

In Linux, deleting the non-empty directory is possible by executing the below script:

$ rm -r dir_name

The “rm” is the command, and the “r‘ flag shows recursive nature (remove directories/sub-directories).

Example 1: How to Delete a Non-Empty Directory?

An example is considered deleting the non-empty directory. A text file named “program.txt” is present that will be deleted in the specified directory. To display the directory content after executing the below script:

$ cd Office/Ceo

Remove the Ceo Directory

The “rm” command is utilized to delete the “Ceo” non-empty directory. To do so, run the following command:

$ rm -r Ceo

After executing the command, the specified “Ceo” directory has been deleted.

Example 2: How to Delete a Non-Empty Directory With All Content?

Another example is adapted to delete the non-empty directory with all content. For instance, the “-r” utility removes the complete directory with subdirectories and files. Additionally, the “-v” verbose utility is used to list out the deleted files and subdirectories in the terminal:

$ rm -r -v Office

You can confirm that all subdirectories and files have been successfully deleted.

Example 3: How to Delete Multiple Non-Empty Directories?

Linux provides an interesting feature to delete multiple non-empty directories. Before deleting multiple directories, you can verify that two directories, “Ceo” and “Staff”, are presented in the “Office” directory as shown below:

$ cd Office

To delete multiple directories, specify the name of the directories “Ceo” and “Staff” with “-r” utility of “rm” command:

$ rm -r Ceo Staff

You can verify from the “ls” command that specified directories had been deleted.

Note: For more details on the “rm” command, click here to read our article on the “rm” command.

Method 2: How to Delete a Non-Empty Directory in Linux Using GUI?

The easiest method to delete a non-empty directory is possible through the graphical representation. Here, an existing directory is considered to delete via the GUI method. The directory name is “Ceo”, and the path is “Home/Office/Ceo”. You can verify that the “Ceo” directory contains 3 items at the bottom right of the window:

Press the right button of the mouse on the particular non-empty directory. It generates a dropdown list with various options. Select the “Move to Trash” option in the list:

After pressing the option, you can verify that the “Ceo” directory has been successfully deleted.

Note: You can delete the non-empty directory by pressing the “Delete” key from the keyboard.

Conclusion

In Linux distributions, you can delete the non-empty directory by executing the “sudo rm -r <dir_name>” command in the terminal. Users can delete the specific file, all files, subdirectories, and multiple directories simultaneously through this terminal. Additionally, the GUI method provides an easier way to delete the non-empty directory. This guide has covered every possible way to delete the non-empty directory in Linux systems.