How to Delete a Directory in Linux

how-to-remove-files-and-folders-using-linux-azmotech

Linux is noticeably different from the Windows operating system. For instance, deleting a folder on Windows is as simple as right-clicking and selecting delete. While you can do the same on Linux using the graphical interface, there are also command-line methods to remove directories. If you’re new to Linux and unsure how to delete a directory, here’s a straightforward guide to help you delete a folder using both the GUI and Terminal.

Method 1: Delete a Directory in Linux Using GUI

This method of deleting folders is simple and ideal for users who are new to Linux. It works on any Linux distribution, as long as a Desktop Environment and file manager are installed. For this example, we’ll be using Fedora with KDE Plasma, but the steps will be quite similar across other desktop environments.

  • Open the File Manager and navigate to the location of the directory you want to delete.
  • Right-click on the folder and select Move to Trash.
move-to-trash-Linux-azmotech
  • Alternatively, click to highlight the folder and press the Delete key on your keyboard.
  • To permanently delete the folder, use the Shift + Delete keyboard shortcut and confirm by clicking Delete in the confirmation dialog.

Method 2: Delete a Directory in Linux Using Terminal

Linux enthusiasts often agree that using the command line is faster and more efficient than relying on the GUI. However, keep in mind that when using the terminal to delete directories, Linux typically does not ask for confirmation, so proceed with caution.

2.1 Using the rm Command

The rm command is one of the most commonly used command-line tools for deleting files and directories in Linux. Here’s the basic syntax:

rm <options> <file_name/directory_name>

Here are some common options that the rm command can take:

-f

When this flag is used, the confirmation prompt will not appear, and all the nonexistent files and directories will be ignored

-i

When this flag is used, the command will ask for confirmation from the user for each deletion.

-r

When this flag is used, the command will remove all the contents of the directory specified.

-d

This flag is used to remove empty directories.

-v

This flag is used to get an explanation of what is being done currently.

To delete a directory along with all its contents, use the -r (recursive) flag with the rm command as shown below:

rm -r <directory_name>
Using-the-Rm-command-with-r-parameter-azmotech

By default, the rm command prompts you when attempting to delete write-protected files or directories. You can respond by pressing y (yes) to confirm or n (no) to cancel. To bypass this prompt and delete files without any confirmation, use the -f flag, which stands for force. For example:

rm -rf <directory_name>
Using-the-rm-command-to-delte-directories-e1744086477658-azmotech

2.2 Using the find Command

To delete a directory using the find command, use the following syntax:

find <path_to_search> -type d -name "directory_name" -exec rm -r {} +
Using-the-Find-command-azmotech

In the above syntax, the find command searches for directories matching the <directory_name> within the <path_to_search>. The -exec flag then passes the found directories to the rm command, which deletes them using the -r flag.

So, if you have a directory named “Desktop” and want to find and delete another directory called “Test” within it, you can use the following command:

find ./test/ -type d -name "test1" -exec rm -r {} +

2.3 Using the rmdir Command

rmdir stands for remove directory, and it’s specifically designed to delete empty directories only. Unlike the rm and find commands, which can delete both empty and non-empty directories (with the appropriate flags), rmdir will throw an error if the directory contains any files or subdirectories.

That said, if you know the directory is empty, rmdir is a clean and simple option—you don’t need to manually check its contents. For example:

rmdir <name-of-the-directory>

So, for example, if you have an empty directory named test, you can remove it using the rmdir command. First, navigate to the location where the directory exists, then run:

rmdir Test
using-the-rmdir-command-e1744087719962-azmotech

Deleting directories or files on Linux is a straightforward yet essential task for users at all levels. In this guide, we’ve covered two main methods to delete folders on Linux: using the GUI and the command line. We hope this article helped you gain a clear understanding of both approaches and the relevant commands. If you run into any issues or have questions, feel free to let us know in the comments below!

Share this article
Shareable URL
Leave a Reply

Your email address will not be published. Required fields are marked *

Read next
0
Share