delete-files-and-directories-linux

Delete a Directory in Linux: Practice with the Terminal

How can you delete a directory in Linux using the terminal?

Which command do you use?

The rmdir and rm commands are used to remove a directory on Linux systems. The rmdir command can be used to delete an empty directory and if the directory contains files or subdirectories then the right command to use is the rm command.

Directories are one of the main concepts to understand about the Linux filesystem.

And I know it can be uncomfortable deleting directories on Linux, especially if you are just getting used to work with it.

This tutorial will help you get familiar with the commands to remove directories in Linux and it will give you more confidence in working with Linux systems.

Let’s get started!

Delete an Empty Directory in Linux

Here is a simple example, I create a directory called testdir using the mkdir command:

(localhost)$ mkdir testdir
(localhost)$ ls -al
total 0
drwxr-xr-x   3 myuser  mygroup   96 15 Jul 00:10 .
drwxr-xr-x  13 myuser  mygroup  416 15 Jul 00:10 ..
drwxr-xr-x   2 myuser  mygroup   64 15 Jul 00:10 testdir

I can use the rmdir command to delete this directory:

rmdir testdir

Now, let’s recreate the same directory, but this time, using the touch command, we also create a file inside the testdir directory.

We then try to delete that directory using the rmdir command…

(localhost)$ mkdir testdir
(localhost)$ touch testdir/testfile
(localhost)$ rmdir testdir
rmdir: testdir: Directory not empty

…Linux returns the error “Directory not empty”.

The reason of this error becomes clear when we check the manual for the rmdir command.

delete directory on Linux with rmdir

As you can see from the man page the rmdir command can only be used if the directory is empty.

So, how do we delete a directory that is not empty?

How to Delete a Linux Directory that is Not Empty

You can use the rm command to delete directories that are not empty.

I will use the rm command with the same directory that we haven’t managed to delete in the previous section using the rmdir command.

Let’s see what happens…

(localhost) $ rm testdir/
rm: testdir/: is a directory

It still doesn’t work, why?

For some reason we get back an error message that complains about the fact that testdir is a directory.

That’s because we need to use the -d flag that according to the rm manual allows the rm command to delete directories as well as other types of files (the rm command can also be used to delete files).

Now, we try to delete the directory passing the -d flag:

(localhost) $ rm -d testdir/
rm: testdir/: Directory not empty

Still, no luck!

This time the problem is that the directory is not empty. We are seeing the same behaviour as the rmdir command.

A different flag for the rm command can help us here…

To recursively remove a directory and all the files and subdirectories in it, we have to use the -r flag:

rm -r testdir

Finally! The directory has been deleted.

Now, if a directory is write-protected you will be asked to confirm the deletion.

To avoid this, especially if you are deleting a directory that contains and big number of files, you can specify the -f flag, that stands for force.

In the example below I’m using the chmod command to remove write permissions from testfile1.

(localhost) $ mkdir testdir
(localhost) $ touch testdir/testfile1
(localhost) $ touch testdir/testfile2
(localhost) $ chmod -w testdir/testfile1 
(localhost) $ rm -r testdir
rm: remove write-protected regular empty file ‘testdir/testfile1’? y 

As you can see, if we don’t use the -f flag the command asks for confirmation.

Let’s try to remove the directory using the -f flag as well:

(localhost) $ mkdir testdir
(localhost) $ touch testdir/testfile1
(localhost) $ touch testdir/testfile2
(localhost) $ chmod -w testdir/testfile1 
(localhost) $ rm -fr testdir 

As expected, in the last example I didn’t have to confirm the deletion of the write-protected file.

Makes sense?

Delete Multiple Directories in Linux

The rm command can be used to delete multiple directories with one command.

You can do that specifying them in the rm command and separating them by space:

rm -fr testdir1 testdir2 testdir3

You can also use a wildcard if you don’t want to specify the name of all the directories and if you want to delete all of them:

rm -fr testdir*

In the example above the beginning of the name of the directories was the same.

Let’s see another example of wildcard when the last part of the name of the directories is the same:

mkdir test1_dir test2_dir test3_dir
rm -fr *_dir

Make sure you are 100% sure of what you are doing when you use wildcards to avoid deleting by mistake directories that you are not planning to delete.

Delete Directories Using the Find Command

Sometimes we want to delete directories that match a specific pattern.

To delete a directory based on a specific pattern or search criteria you can use the Linux find command.

The find command allows you to search for files or directories in the filesystem and execute an operation against those files or directories that match your search criteria.

Let’s start by showing a generic example on how to use find to delete directories whose name matches a specific pattern, for example directories that have a name that starts with “tmp_”.

Let’s say I have the following subdirectories in the current directory:

(localhost) $ ls -al
total 0
drwxr-xr-x   7 myuser  mygroup  224 16 Jul 08:18 .
drwxr-xr-x  13 myuser  mygroup  416 15 Jul 00:10 ..
drwxr-xr-x   2 myuser  mygroup   64 16 Jul 08:18 config
drwxr-xr-x   2 myuser  mygroup   64 16 Jul 08:18 logs
drwxr-xr-x   2 myuser  mygroup   64 16 Jul 08:18 tmp_dir1
drwxr-xr-x   2 myuser  mygroup   64 16 Jul 08:18 tmp_dir2

The find command to identify directories with a name that starts with “tmp_” is:

find . -type d -name 'tmp_*'

The flag -type with value d is used to restrict the search to directories, the -name flag allows to pass a pattern we want the name of the directory to match.

The output of the command is:

./tmp_dir2
./tmp_dir1

Now, if I want to delete the directories we have found I can use the -exec flag:

find . -type d -name 'tmp_*' -exec rm -r {} \;

For each result found by the find command the command after -exec is executed.

The curly brackets ( {} ) get replaced by the result of the find command.

When I run the command below the two directories get removed as expected but I also see the following error:

find: ./tmp_dir2: No such file or directory
find: ./tmp_dir1: No such file or directory

Why?

Let’s take as an example tmp_dir1…

The find command identifies that directory based on the -type and -name flags, it deletes the directory and after that it tries to enter the directory to look for anything else inside that directory that matches the same pattern.

That’s why we see this error, find tries to enter the directory after it has been removed.

Delete a Directory Older Than X Days

A common requirement is to delete a directory based on its age.

This happens, for example, in scripts used for backups that when executed check if there are old directories to delete once the backup is complete.

To do that we can still use the find command and we can start from the command explained in the previous section.

Let’s see how…

The find command supports the -ctime flag followed by the number of days.

The following command will delete directories for which the modification time is older that 7 days:

find . -type d -name 'tmp_*' -ctime +7 -exec rm -r {} \;

More recent versions of the find command also support the + sign instead of ; so the previous command can also be written as below:

find . -type d -name 'tmp_*' -ctime +7 -exec rm -r {} +

The version of the command with the + at the end optimises the performance of the exec Linux system call.

You can simply update the value of the -ctime flag depending on your requirements, for example to delete directories older than 30 days the command becomes:

find . -type d -name 'tmp_*' -ctime +30 -exec rm -r {} +

Conclusion

You should now have a good understanding on how to use the Linux rmdir and rm commands to delete a directory in Linux.

We have also seen how to delete multiple directories with a single command and how to delete directories using the find command (based on a search criteria).

Everything clear?

Let me know if you have any questions 🙂

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *