How to Delete a Directory in Linux

While working on a Linux distro, you may need to manage the Linux files and directories. Sometimes, we want to delete or remove old Linux directories. To delete Linux directories, two methods are mainly used. 

For casual use, the graphical user interface (GUI) provided by your desktop environment (like Gnome or KDE) lets you quickly delete files and folders. Simply find the item you want to remove, right-click, and choose Delete. However, the command line is your best option if you’re working on a server without a desktop or GUI environment and want to delete multiple directories in one go.

This article discusses different commands for deleting Linux directories.

Table of Contents:

  1. How to Delete a Directory in Linux
  2. How to Delete Directories in Linux Using GUI
  3. Conclusion

1. How to Delete a Directory in Linux

In Linux, deleting a directory means erasing its location from its file system. After you delete a directory using the commands, it will remove them immediately.  This means the directory and its contents can not be accessed through the file system. However, the actual data blocks on the storage device are not immediately wiped; they are marked as available for reuse.

Some primary methods for directory deletion are discussed here.

1.1. Using the rm Command

The rm is a basic command for deleting a file or directory. This command also removes all the subdirectories or folders inside the main directory. 

Before using the rm command to delete a directory, check the directory name first. To check the directory name, you can list it using the ls command. However, if you need to find the directory path or check which directory you are present in, use the pwd command.

Delete A Directory In Linux A

Before deleting any directory, ensure you are inside the parent directory where that subdirectory exists.

To remove the directory with rm, specify the option with the rm command. Following is the example syntax for the rm command:

rm [options] [directory_name]

If you don’t specify the option with the rm command, you will encounter an error:

Delete A Directory In Linux B

This is because the rm command on its own can’t remove any directory. You have to specify any of the options with it.

Following are some main options to use with the rm command while removing the directories.

OptionsDescription
rm -dRemove any empty directory
rm -rDelete a non-empty directory
rm -fForce removal of write-protected files without any confirmation
rm -rfForce removal of a write-protected non-empty folder without any confirmation prompt
rm -iOutput a prompt for confirmation before deleting any directory
rm -IOutput a prompt only once before deleting more than three files 
rm *Use the * wildcard to represent multiple characters in filenames
rm ?Use the ? Wildcard to represent a single character in filenames
rmdir -pRemove an empty subdirectory along with its parent directory
rmdir -vDisplay info that the specified directory is deleted

Removing an Empty Directory

To remove any empty directory using the rm command, you have to specify the -d option with the rm command. For example, to remove the test1 directory, which is an empty directory, you can run this command:

rm -d test1
Delete A Directory In Linux C

If you are deleting any other directory, remember to replace the actual directory name with the one in the above command.

Deleting a Non-Empty Directory

Now we will delete a nonempty directory using the -r flag with the rm command. Here we have a test2 directory, which further contains subdirectories and some text files.

You can see the hierarchy of the test2 directory by using the tree command:

tree -a test2
Delete A Directory In Linux D

Now to delete this nonempty directory, run this command:

rm -r -v test2

Here, the -r flag will delete all the content of the test2 directory, while the -v flag will print all deleted file details on the terminal.

Delete A Directory In Linux E

The above command deletes a single directory. However, multiple directory deletion is also possible with the rm command.

Deleting Multiple Directories at Once

To delete multiple directories using the rm command, separate each directory name with a space:

rm -r test1 test2 test3
Delete A Directory In Linux F

Here you can see we have used the -r flag instead of -d, this is because if you used the -d flag it will only remove the empty directories. The -r flag will remove both the empty and non-empty directories at once.

However, this method is too generic and requires time to delete multiple directories at once.

The alternate way of deleting multiple directories is using the rm command with a wildcard to find the specific or common pattern in directory names. 

You can use the wildcard (*) to match multiple directories. For example, to delete all directories with names beginning with test, you can run:

rm -r -v test*
Delete A Directory In Linux G

Precaution: It’s recommended to use the ls command first to list the directories and verify what you’re going to delete before executing the rm command.

ls test*
Delete A Directory In Linux H

You can also use the brace expansion to delete multiple directories at once. For example, to delete specific directories whose starting name is the same, but the second half of the name changes you can use this command:

rm -v -r test{1,2,3}
Delete A Directory In Linux I

Alternatively, you can use a character range to achieve the same result:

rm -r -v test[123]
Delete A Directory In Linux J

When deleting write-protected directories, you will be required to confirm it:

Delete A Directory In Linux K

You can also remove the write-protected directories by passing the -f (force) flag with the rm command:

rm -r -f test1

This way, you don’t need to confirm it.

Delete A Directory In Linux L

To display the confirmation prompt before deleting any directory, add the -i flag with the rm command:

rm -r -i test1
Delete A Directory In Linux M

Deleting Directories According to Their Time

To delete directories older than 20 days, you can run this command:

find /home/ilf/test -type d -mtime +20 -exec rm -rf {} \;

You can also customize the time in the above command according to the required output.

To remove directories between a specific date range, run this command:

find /home/ilf/test -type d -newermt '2024-05-27' ! -newermt '2024-05-29' -exec rm -rf {} \;

This command will delete all the directories between 2024/05/27 and 2024/05/29.

Deleting Directories According to Their Size

The rm command can also be combined with the find command to delete directories having specific sizes. For example, to delete directories greater than 10 MB, run this command:

find /home/ilf/test -type d -size +10M -exec rm -rf {} \;

To delete all the directories smaller than 10 MB, run this command:

find /home/ilf/test -type d -size -10M -exec rm -rf {} \;

Similarly, to delete all the directories having zero (0) bytes of size, run this command:

find /home/ilf/test -type d -size 0 -exec rm -rf {} \;

1.2. Using the rmdir command

The rmdir command in Linux is designed to delete empty directories. The general form for using this command is:

rmdir [options] [directory name]

Here are some options you can use with the rmdir:

  • –ignore-fail-on-non-empty: This option prevents error messages from appearing if you attempt to delete directories that aren’t empty.
  • -p: Deletes a directory and its parent directories.
  • -v: Outputs detailed information about the command’s execution.
  • –help: Shows instructions for using the command.
  • –version: Displays version of rmdir command.

If you try to use rmdir on a directory that contains files or other directories, it will result in an error. 

For example, if there’s a directory named test1 which includes a subdirectory called subtest, you’ll encounter an error if you try to remove the test1 directory:

rmdir test1
Delete A Directory In Linux N

You can use the tree command to get the hierarchy of the directory you want to delete:

tree -a test1
Delete A Directory In Linux O

To delete the non-empty directories, you should specify them in reverse order from sub to main or parent directory. Using the -v option will list each action it performs:

rmdir -v test1/subtest test1
Delete A Directory In Linux P

Alternatively, you can use the -p option with the name of the deepest subdirectory to delete it along with its parent directories in one go:

rmdir -p -v test1/subtest
Delete A Directory In Linux Q

The rmdir command also supports the use of wildcards to delete multiple similarly named directories at once. For example, to remove directories named test1, test2 and test3, you would use:

rmdir -v test*
Delete A Directory In Linux R

2. How to Delete Directories in Linux Using GUI

Another way to delete files is by using the GUI file manager. This method is easy to use because it gives us a backup of the files after deletion. Instead of directly deleting the files completely from the system, files are first sent to the trash, thereafter they get automatically deleted.

Right-click the selected directory to delete it. After that, select Move to Trash or you can press the Delete key also.

Delete A Directory In Linux S

Moved to trash message will appear.

Delete A Directory In Linux T

To delete them permanently, head over to the Trash section and delete them.

Delete A Directory In Linux U

Conclusion

In this article, various methods for deleting directories in Linux are covered. It covered using the rm command with different options for handling empty and non-empty directories. Additionally, it explained the rmdir command for removing empty directories and its functionalities. Finally, we covered a brief overview of deleting files and directories using the graphical user interface (GUI).

var authorName = "' . esc_js($post_author) . '";'; ?>