How to Remove (Delete) Directory in Linux

Updated on

5 min read

Linux Remove (Delete) Directory

Linux offers several different methods for removing directories. If you are using a desktop file manager such as Gnome’s Files or KDE’s Dolphin, you can quickly delete files and directories using the manager’s graphical user interface. Locate the file or directory you wish to remove, right-click on it, and select the “Delete” option. But, if you are working on a headless server or want to remove multiple directories at once, your best option is to delete the directories (folders) from the command line.

In this article, we will explain how to delete directories in Linux using the rmdir, rm, and find commands.

Before You Begin

When you use a desktop file manager to delete a directory, it is actually moved to the Trash and can be easily recovered. However, be very cautious when deleting directories or files through the command line because once the directory is deleted using the commands explained in this article, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get an “Operation not permitted” error.

Directories with spaces in their names must be escaped with a backslash (/) character.

Removing Directories with rmdir

rmdir is a command-line utility that enables you to delete empty directories. It comes in handy when you need to delete a directory, but you only want to do it if it’s empty, without having to check its contents.

To delete a directory using rmdir, enter the command followed by the name of the directory you want to remove. For instance, if you want to delete a directory named dir1, you would type:

rmdir dir1

If the directory is not empty, you will get the following error:

rmdir: failed to remove 'dir1': No such file or directory

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm

rm is a command-line utility for deleting files and directories. Unlike rmdir, the rm command allows you to delete empty and non-empty directories.

By default, when used without any option, rm does not remove directories. To delete an empty directory, use the -d (--dir) option, and to delete a non-empty directory and all of its contents, use the -r (--recursive or -R) option.

For example, to delete a directory named dir1 along with all of its contents, you would type:

rm -r dir1

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

rm -rf dir1

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove the specified directories and their contents:

rm -r dir1 dir2 dir3

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. However, if the directory contains a large number of files, this can become tedious. In such cases, you can use the -I option, and rm will prompt you only once before proceeding with the deletion.

rm -rI dir1

To remove the directory, type y and hit Enter.

rm: remove 1 argument recursively? y

You can also use regular expansions to match and delete multiple directories. For instance, to remove all first-level directories in the current directory that ends with _bak, you would use the following command:

rm -r *_bak

Using regular expansions when removing directories may be risky. It is recommended to use the ls command to list the directories before running the rm command, so you can see which directories will be deleted.

Removing Directories with find

find is a command-line utility that allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run:

find . -type d -name '*_cache' -exec rm -r {} +

Let’s analyze the command above:

  • /dir - recursively search in the current working directory (.).
  • -type d - restricts the search to directories.
  • -name '*_cache' - search only directories that end with _cache
  • -exec - executes an external command with optional arguments; in this case, that is rm -r.
  • {} + - appends the found files to the end of the rm command.

Removing all empty directories

To remove all empty directories in a directory tree, you would run:

find /dir -type d -empty -delete

Here is an explanation of the options used:

  • /dir - recursively search in the /dir directory.
  • -type d - restricts the search to directories.
  • -empty - restricts the search only to empty directories.
  • -delete - delete all found empty directories in the subtree. -delete can delete only empty directories.

Use the -delete option with extreme caution. The find command line is evaluated as an expression, and if you add the -delete option first, the command will delete everything below the starting points you specified.

Always test the command first without the -delete option and use -delete as the last option.

/bin/rm: Argument list too long

This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on the size of the command line argument.

There are several different solutions to this problem. For example, you can cd to the directory and remove sub-directories one by one, either manually or using a loop .

The easiest solution is first to delete all files within the directory with the find command and then delete the directory:

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

Conclusion

With rm and find you can delete directories based on different criteria quickly and efficiently.

Deleting directories is a simple and easy process, but you must be cautious not to delete important data.

If you have any questions or feedback about using these commands, please feel free to comment.