Easy Guide: Deleting a Directory in Linux

Easy Guide: Deleting a Directory in Linux

Linux is strikingly different from the Windows operating system. For example, if you want to delete a folder on Windows, you can simply right-click on it and delete it. While that’s also true for Linux, there are other ways to deleting folders which involve using the command line interface. If you’re new to Linux and don’t know how to delete a directory, here’s a simple guide to show you how to delete a directory in Linux via both GUI and Terminal.

Method 1: Delete a Directory in Linux Using GUI

This method of deleting files is easy and best suited for users who have newly migrated to Linux. It can work on any Linux distribution, provided a Desktop Environment and file manager is installed. We’ll be using Fedora with KDE Plasma for this example, but the process won’t be too different on other desktop environments.

  1. Launch the File Manager and navigate to the path which hosts the directory you want to delete.
  2. Right-click on the folder and select Move to Trash.
Also Read:

8 Best Examples of cp Command in Linux

  1. You can also click to highlight the directory and press the Delete key on your keyboard.
  2. If you want to permanently delete a file or a folder, use the Shift + Delete keyboard shortcut and click on Delete in the confirmation box.

Method 2: Delete a Directory in Linux Using Terminal

Linux enthusiasts would agree that carrying out tasks using the command line is faster than using GUI. It’s worth noting that Linux’s command line doesn’t ask for your confirmation before removing the directory, so tread with caution.

2.1 Using the rm Command

The rm command is one of the most common command line utilities to delete files and folders on Linux. Here’s the syntax of the same.

rm <options> <file_name/directory_name>

Some of the options which the command can take are:

Option Description
-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 with all its contents, use the -r flag with the command as shown below:

rm -r <directory_name>

Also Read:

Linux File Permissions Explained

RM command with r parameter Linux

The rm command gives a prompt, by default, when deleting the write-protected files and directories. Press either y or n depending upon your choice. To bypass the prompt, we use the -f parameter, which stands for Force.

rm -rf <directory_name>

RM Command in Fedora Konsole

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 {} +

Find command

In the above syntax, the find command looks for directories matching with the <directory_name> in the <path_to_search> and then the -exec flag will pass the searched items to the rm command, which will delete the directory using the -r flag.

So, if I have a directory “Desktop” in which I want to find and remove another directory called “Test,” I can use the following command to do so.

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

2.3 Using the rmdir Command

Rmdir stands for remove directory, but it can only be used for deleting empty directories. Which the rm and find directories can delete both empty and non-empty directories, most users will delete directories without using the flags so it doesn’t matter.

However, you can use the rmdir command to delete empty directories without needing to check each directory and whether it’s empty.

rmdir <name-of-the-directory>

So, for example, if I have a directory named test which is empty, I can remove it using the following rmdir command. First, I’d need to go to the location of the directory I want to delete, of course.

rmdir Test
Also Read:

6 Best Linux PDF Editors You Should Use

rmdir command in Konsole

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

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

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

Why I cannot remove a directory in Linux?

The main reason you cannot remove a directory on Linux is that you do not have appropriate permissions to make any changes in the directory. To delete a directory by bypassing missing permission, use the following command: sudo rm -rf <directory_name>

What is rm in Linux?

rm in Linux stands for remove. It’s a command line utility with different parameters that can be used to remove files and directories in Linux. One of the popular parameters in it is -rf which is a combination of two parameters -r (directories) and -f (force).