Remove directory Linux

If you’re still new to Linux’s command line and want to learn basic commands, you’ve come to the right place.

If you’re looking for different ways on how to remove a directory in Linux, you’re in the right place.

Before we hop in, there are a few things you should understand first.

Before We Begin

You can delete a directory using two methods. The first method uses the graphical user interface or desktop environment, while the second method uses the command line.

While both methods carry the same function to delete directories, there are key differences worth mentioning.

  1. For those that have Gnome’s File, using the graphical user interface would be the standard method to remove a directory. On the other hand, using the command line is common when using a headless server. For example, computers found in data centers typically use the command line to remove a directory.
  2. Using the command line to delete a directory is more effective, especially when you have to delete multiple files or directories.

A Word of Caution

If you want to delete a directory using the graphical user interface, you’ll be doing so without permanently removing files or directories from your system.

When you remove a directory in Linux using the graphical interface, the files or directories move to your Trash, where you can easily recover it if you made a mistake deleting it.

With the command line, it’s gone permanently. Before you delete files or directories in Linux using the command line, you’ll have to be sure that you won’t be needing the file anymore.

Files or directories deleted through the command line function cannot be recovered, nor are they moved to the Trash, so you’ll have to extra careful when using the command line.

Before deleting files or directories, we recommend doing a backup first. Having a plan B always comes in handy.

Using GUI to Delete a Directory

Learning how to remove directory in Linux using GUI (graphical user interface) is straightforward and easily digested.

Follow these steps to delete a directory in Linux:

Step 1: Open your file manager and select the files and directories you want to remove.

Step 2: Highlight and right-click the directory you want to remove. You’ll be prompted with the option to move it to the Trash.

Some desktop environments provide you with the option to “Delete” or “Move to Trash,” while some simply have the option “Delete.” If you have the “Move to Trash” option, select “Move to Trash.”

If you have only the “Delete” option, this will typically move it to Trash once it’s been deleted. To be sure, try making a copy and deleting it.

Step 3: Once you’ve selected the “Delete” or “Move to Trash” option, the file manager will reaffirm if you want to remove the directory.

Step 4: Select “yes,” and your directory will move to the Trash.

Step 5: If you want to permanently delete a directory, head over to your Trash and right-click the directory you want to erase permanently and select the “Delete” option.

Using the Command Line to Delete a Directory

Deleting or removing files and directories using the GUI method is easy to do in Linux. How to remove a directory with the command line, though, requires learning a few commands first.

There are two commands used to delete directories: rm and rmdir command. The rmdir command is used to delete empty directories in the system.

The rm command can delete all files, including non-empty and empty directories. Before you learn how to use the rmdir and rm command, you should first learn how to use the find command.

How to Use the Find Command

The find command lets you to search for files and directories using specified search criteria. Once you’ve found the files and directories, you can progress into deleting these based on the specified criteria.

For example, if you have a directory named “directory1,” we’ll teach you how to find this directory and delete it.

How to remove “directory1” in Linux using the find command: 

Step 1: Open the command line

Step 2: Type the command 

sudo find . -type d -name "directory1" -exec rm -rf {} +

To understand what each command means, here’s a breakdown of each one:

-type d – Commands the search operation to search for directories exclusively without deleting any file

( . ) – This refers to the directory where you want to carry out the search operation. Using the ( . ) sign means you want to carry out the search in your current directory.

-name – Specifies the directory name

-exec rm -rf – Deletes all the directories and its contents

{} + – Adds all the files that are found at the end of the “rm” command

Take note that using the “rm” command signifies all files and directories will be deleted permanently.

Using the Rmdir Command

Unlike the rm command, using rmdir is useful if you want to delate an empty directory without having to worry about its contents.

If you try to delete all files in your current directory using this command, you will be prompted with an error message.

Examples:

In the command line of Linux, how to remove “directory 1”, for example, will require the command:

rmdir "directory 1"

If this is an empty directory, the deletion will proceed successfully. If there are files in that current directory, then you’ll be prompted with the message:

rmdir: failed to remove "directory 1" : no such file or directory

When this happens, if you want to learn how to remove directory in Linux, you’ll have to use the rm command, or you can remove the contents in the directory and delete it.

You can also remove multiple directories using the rmdir command.

To delete directories, type the following command:

rmdir "directory 1" "directory 2" "directory 3"

To remove a folder that isn’t found in the current directory, you can specify the path to that directory and delete it.

rmdir /path/to/directory a

Take note that rmdir works sequentially. If you delete several directories and the first directory prompts an error message, the whole process will be terminated.

However, you can force the system to delete the folder and ignore the directory that has files. Let’s say that “directory a” has files, type the command:

rmdir --ignore-fail-on-non-empty directory a

The rmdir command is useful for removing empty directories, and it’s also a safer command to use in case you forget the directory has contents in it.

Using the Rm Command

Although the rm command can be used to delete empty directories in Linux, its primary purpose is to delete non-empty directories and files as well.

One should be extra careful when using the rm command. Remember, you cannot take these files back or undo the process.

The rm command is also different from rmdir in the sense that using rm will not automatically delete both empty and non-empty directories.

You have to specify whether you want to remove an empty or non-empty directory. If you use the “-d” option, it means you want to remove an empty directory. If you use the “-r” option, you’ll be deleting the non-empty directories.

Examples:

To delete a directory in Linux along with all its contents, simply type:

rm -r directory 1

To delete multiple directories:

rm - r directory 1 directory 2 directory 3

If the directory has write-protection, you will see a prompt asking you to reaffirm the deletion. If you don’t want to see the prompt that will reaffirm the deletion, you can use the -f option:

rm -rf directory 1

What if you want to learn how to remove all subdirectories and files that have write-protection? Rather than individually reaffirm the hundreds of files contained in the directory, you can use the -i option to reaffirm once for all files:

rm -rI directory 1 

Once the reaffirmation prompt appears, type “Y” and hit Enter.

What if you want to erase files that have something in common contained in your directory? If, for example, you want to erase all the files in Linux that contain _abcd, simply type:

rm -r * _abcd

If you aren’t sure or confident of which files specifically you want to remove, you can use the Is command to provide you with the complete list of directories in your Linux system.

Deleting files with the rm command is a lot simpler than if you want to remove directories.

If you have the file name “sample.txt” simply type:

rm sample.txt

If the file is located in a separate directory, simply type:

rm /path/to/the/file/sample.txt

To delete several files:

rm sample.txt 1 sample.txt 2 sample.txt 3

To delete specific files like “.jpg” or “.png” you can do so by simply typing:

rm * .jpg 
rm * .png

If you have hundreds of .png and .jpg folders, you can also use the -i option and -f option similar to when removing directories:

rm -i * .jpg
rm -f sample.txt

Using the -f option forces the deletion without any reaffirmation, so before using this in the Linux command line, make sure you’re ready to let this go.

The -d option also works similar to -r, for example:

rm -d directory 1
rm -d directory 1 directory 2

Removing all empty directories

To remove all empty directories from the directory tree in the Linux system, type:

find /dir -type d -empty -delete 

/dir – recursively searches the /dir directory

-type d – limits the search to directories

-empty – limits the search to empty directories

-delete – deletes all the empty directories found in the subtree

Take precautions using the -delete function. If you accidentally replace the “find” expression with -delete, you’ll erase everything empty from the specified location /dir.

Linux remove directory: Error/Argument list too long

If you want to delete a folder with hundreds of files, it’s possible to get the error /bin/rm. This happens when the data size you’re attempting to delete exceeds the size limit in the command line argument.

To solve this error, you’ll have to use the find command to delete all contents in the directory then delete the directory afterward:

find /dir -type f -delete && rm - r /dir

Final Thoughts

We hope this article helped you learn how to effectively remove directories in Linux! For any comments and suggestions, feel free to leave them in the section below!