How to Remove (Delete) Directory Using Linux Command Line

If you are using a Desktop version of Linux then deleting directories is rather straightforward but if you are using a Linux server that has no GUI then you will need to know the terminal commands for deleting directories.

 

Fortunately, Linux provides a number of ways to delete files and folders and they are reasonably straightforward to use. Let's take a look at how we might remove a directory using the command line.

 

A Warning

Before deleting directories you should be aware that if you make a mistake and delete the wrong thing then you will not be able to recover it. Items are not put into a trash bin when using the command line.

 

Deleting Empty Directories

If you need to delete an empty directory you can use the rmdir command which is very basic and works like this:

 

rmdir directory_name

 

If the directory is not empty you will get an error telling you the directory is not empty.

 

rmdir: test/: Directory not empty

 

Remove Directories Using RM

The rm utility offers functionality for deleting both files and directories in Linux. If you want to delete an empty directory with rm you can pass in the -d flag like so:

 

rm -d dir1

 

To remove directories that contain files and other directories you have to pass in the -r flag. This will recursively delete the dir1 directory and everything inside it:

 

rm -r dir1

 

You can also pass multiple directories to be deleted into the rm command like so:

 

rm -r dir1 dir2 dir3 dir4

 

If some of the directories within the directory you are deleting have restricted write permissions you will have to use the -rf flag to force deletion:

 

rm -rf dir1

 

Using RM with Regular Expressions

Like most command utilities in Linux you can use regular expressions to match certain directories using rm. In the following example we will remove all directories within the current working directory that end with "_old".

 

rm -rf *_old

 

You can also delete all directories within the current working directory by only supplying the * regular expression.

 

rm -rf *

 

Warning - this command is very powerful and can cause a lot of damage if you run it from an unintended location.

 

Deleting Directories in Linux Using Find

The find command, as the name suggests, is a way to find files and directories on a Linux system according to a set of criteria. With find you can specify a command to run after the search is complete and append the results into it.

 

The basic syntax of find looks like this:

 

find dir-name criteria action

 

Let's search for some directories and delete them using find:

 

find /var/www/example.com/logs -type d -name "*_old" -exec rm -r {} +;

 

Here is what each part of the command is doing:

 

  • /var/www/example.com/logs - tells find to search in the logs directory
  • -type d - look for directories
  • -name "*_old" - find directories that end in _old
  • -exec the command to run after completion; in our case it is rm -r
  • {}+ appends the find results into the rm -r command

 

 

Ask Before Deleting Each directory

Using the above command can be a bit scary. If you want to be asked to confirm deletion of each directory you can add the -i flag to rm.

 

find /var/www/example.com/logs -type d -name "*_old" -exec rm -r -i {} +;

 

Conclusion

Now you can use a set of powerful tools for deleting directories in the Linux command line. You can delete empty directories, delete multiple directories recursively and search for directories to be removed in a specified location using regular expressions.

 

Once again use these commands with caution and double-check you are running them from the intended working directory before committing.

terminal command line file