how-delete-directory-linux

How to Delete a Directory in Linux in 2023 (4 Methods)

Linux is very different from the Windows operating system. For example, if you want to delete a folder in Windows, you can simply right-click on it and delete it. However, things are not so simple under Linux. Deleting a directory or folder in Linux can be done through the GUI as well as the command line interface. If you don’t know how to delete a directory in Linux, we have prepared a simple but effective guide for you. In this article, we will show both GUI and CLI methods to delete directories in Linux.

Delete a directory in Linux (2023)

Folders in Linux are called directories. And in the Linux world, everything is treated as a file, even a directory. Now let’s see how to delete a directory in Linux in four different ways – one GUI and three CLIs.

How to delete a directory in Linux (GUI method)

This method of deleting files is simple and best suited for users who have recently migrated to Linux. It can work on any distro, as long as a “desktop environment” and a file manager are installed on the computer. In this article, we are using Ubuntu 20.04 LTS with Gnome desktop environment and Nautilus file manager. Here’s how it works:

1. First, open any file manager of your choice and navigate to the path where you want to delete the directory.

2. Select the folder(s) you want to delete and press the “DELETE” on the keyboard. Or, you can also right-click on the selected folder and select “Trash” in the context menu.

go to trash menu

3. All files and directories deleted in Linux are not permanently deleted but moved to a special location called Recycle Bin, which is similar to Windows Recycle Bin.

4. To permanently delete a directory in Linux, select it and press “Shift + Delete” on the keyboard. It will open a prompt indicating whether you want to delete it permanently or not. Click “Delete” again in the dialog box.

Permanently delete the prompt

Delete a directory in Linux via command line

Doing any task using command line is faster with many options than GUI method. Also, the CLI method permanently deletes files and folders. Here we will show three commands to delete a directory in Linux which comes preinstalled in every Linux distribution.

Delete directory using rmdir Order

THE rmdir The command is typically used to remove empty directories, but can also be used to remove non-empty ones. The command doesn’t have a ton of features and options, but it gets the job done. The general syntax of the command is as follows:

rmdir <options> <directory_name>

Some of the options that the rmdir order can take are:

Option Description
--ignore-fail-on-non-empty used to remove non-empty directories
-p, --parents used to delete the directory with its specified children
-v, --verbose used to get a diagnostic message for each directory
Delete empty directory in Linux

To delete an empty directory on Linux, use the following command:

rmdir <directory_name>

Delete empty directory using rmdir command

Here in this example, since we are not getting any output, it means the command was executed successfully and the directory was deleted.

Delete non-empty directory in Linux

When you attempt to delete a non-empty directory using the rmdir command, you will get the following error:

rmdir: failed to remove '<directory_name>': Directory not empty

To delete a non-empty directory on Linux, use the following command:

rmdir --ignore-fail-on-non-empty <directory_name>

Delete non-empty directory using rmdir command

Delete directory using rm Order

THE rm The command is a powerful tool that can delete both files and directories while providing many great features. The basic command syntax is:

rm <options> <file_name/directory_name>

Some of the options the command can take are:

Option Description
-f When this flag is used, the confirmation prompt will not appear and all non-existing files and directories will be skipped
-i When this flag is used, the command will ask the user for confirmation for each deletion.
-r When this flag is used, the command will delete all contents of the specified directory.
-d This flag is used to remove empty directories.
-v This flag is used to get an explanation of what is currently being done.
Delete empty directories

To delete empty directories in Linux, use the -d flag with the command as shown below:

rm -d <directory_name>

Delete empty directory using rm command
Delete non-empty directory

While deleting non-empty contents, it can be very dangerous as some essential files may get deleted. So be very careful when deleting non-empty directories. To delete a directory with all its contents, use the -r flag with the command as shown below:

rm -r <directory_name>

remove non-empty directory using rm command
Force deletion of directories in Linux

The rm command gives a prompt, by default, when deleting write-protected files and directories. Press either 'y' Or 'n' depending on your choice. To bypass the prompt, we use the -f flag as shown below:

rm -rf <directory_name>

This command can be quite disastrous if run inadvertently in the root directory.

Force delete directory in Linux using rm command
Ask before deleting folders in Linux

When deleting multiple files, use the -i flag with the rm command to get a prompt before each file as shown below:

rm -ri <directory_name>

Get prompt when deleting using rm command

Delete directories using find Order

Delete empty directory in Linux

You can also delete folders using the find order with the -delete flag as shown below:

find <path_to_search> -type d -name "directory_name" -delete

This command will search for the empty directory specified by the parameter in the given path and will delete them.

Delete empty folder using find command
Delete non-empty directories

To remove non-empty directories using the find command, use the following syntax:

find <path_to_search> -type d -name "directory_name" -exec rm -r {} +

Understand the above syntax:

In the syntax above, the find The command searches for directories matching the in the then the -exec flag will pass the searched items to the rm command, which will delete the directory using the -r flag.

Delete non-empty directory using find command

Frequently Asked Questions

What is the fastest way to delete a directory in Linux?

The command line method is the fastest way to delete directories. You can use rmdir, rmAnd find commands to delete directories in Linux.

Why can’t I delete a directory in Linux?

The main reason you can’t delete a directory in Linux is that you don’t have the proper permissions to make changes to the directory. To delete a directory bypassing the missing permission, use the following command: sudo rm -rf <directory_name>

Delete Files and Directories Using Linux Commands

Deleting directories/files in Linux is a very simple but very important task for users of all kinds. Here we have shown two ways to delete folders in Linux and we hope this article has given you a good understanding of both methods and commands. Let us know in the comments if you have any issues.