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

1. Using the rm Command: The rm command is the most common way to delete a directory in Linux. To delete a directory, simply type the following command in the terminal: rm -r .

2. Using the rmdir Command: The rmdir command is another way to delete a directory in Linux. To delete a directory, simply type the following command in the terminal: rmdir .

3. Using the find Command: The find command is a powerful tool for searching and deleting files and directories in Linux. To delete a directory, simply type the following command in the terminal: find -type d -exec rm -r {} \;.

4. Using the shred Command: The shred command is a powerful tool for securely deleting files and directories in Linux. To delete a directory, simply type the following command in the terminal: shred -u .

Linux is strikingly 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 that simple in Linux. Deleting a directory or folder in Linux can be accomplished via the graphical user interface as well as the command line interface. If you’re not sure how to delete a directory in Linux, we have prepared a simple yet efficient guide for you. In this article, we will show both the GUI and the CLI methods to delete directories in Linux.

Remove a Directory in Linux (2023)

Folders in Linux are known as directories. And in the world of Linux, everything is treated as a file, even a directory. Let us now see how to delete a directory in Linux using four different ways – one GUI and three CLI.

How to Delete a Directory in Linux (GUI Method)

This method of deleting files is easy and best suited for users who have newly migrated to Linux. It can work on any distribution, provided a “Desktop Environment” and file manager is 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.

select the directory

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

move to trash menu

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

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

Delete permanently prompt

Delete a Directory in Linux via the Command Line

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

Delete Directory Using the rmdir Command

The rmdir command is generally used to delete empty directories, but can also be used to delete non-empty ones. The command does not 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 command can take are:

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

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

rmdir <directory_name>

Delete empty directory using rmdir command

Here, in this example, since we get no output, this means the command got successfully executed and the directory has been deleted.

Delete Non-Empty Directory in Linux

When you will try 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 in Linux, use the following command:

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

Delete non-empty directory using rmdir command

Delete Directory Using the rm Command

The rm command is a powerful tool that can delete both files as well as directories while providing many great features. The basic syntax of the command is:

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.
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

When deleting non-empty contents, it can be very dangerous as some essential files may get deleted. So, be extra cautious 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>

delete non-empty directory using rm command
Force Delete Directories in 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 flag as shown below:

rm -rf <directory_name>

This command can prove to be quite disastrous if executed unintentionally in the root directory.

Force delete directory in Linux using rm command
Prompt 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>

Getting prompt when deleting using rm command

Delete Directories Using the find Command

Delete Empty Directory in Linux

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

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

This command will look for the empty directory specified by the parameter <directory_name> in the given path and delete them.

Delete empty folder using find command
Delete Non-Empty Directories

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

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

Understanding the above syntax:

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.

Delete non-empty directory using the 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, rm, and find commands to delete directories in Linux.

Why I Cannot remove a directory in Linux?

The main reason you cannot remove a directory in 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>

Delete Files and Directories Using Linux Commands

Deleting directories/files in 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 in 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.

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

Deleting a directory in Linux can be a tricky task, especially if you are new to the operating system. In this article, we will discuss four different methods to delete a directory in Linux in 2023.

Method 1: Using the rm Command

The rm command is the most common way to delete a directory in Linux. To delete a directory, you need to use the -r option with the rm command. This option will delete the directory and all its contents. To delete a directory, open the terminal and type the following command:

rm -r directory_name

Replace directory_name with the name of the directory you want to delete.

Method 2: Using the rmdir Command

The rmdir command is another way to delete a directory in Linux. This command will only delete an empty directory. To delete a directory, open the terminal and type the following command:

rmdir directory_name

Replace directory_name with the name of the directory you want to delete.

Method 3: Using the find Command

The find command is a powerful command-line tool that can be used to delete a directory in Linux. To delete a directory, open the terminal and type the following command:

find directory_name -type d -exec rm -r {} \;

Replace directory_name with the name of the directory you want to delete.

Method 4: Using the rm -rf Command

The rm -rf command is a powerful command that can be used to delete a directory in Linux. This command will delete the directory and all its contents. To delete a directory, open the terminal and type the following command:

rm -rf directory_name

Replace directory_name with the name of the directory you want to delete.

These are four different methods to delete a directory in Linux in 2023. Each method has its own advantages and disadvantages, so choose the one that best suits your needs.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?