Remove directory linux command

Delete a Directory in Linux: Practice with the Terminal

delete-files-and-directories-linux

How can you delete a directory in Linux using the terminal? Which command allows you to delete a directory?

Using the rm or rmdir commands you can remove a directory on Linux systems. The rmdir command can be used to delete an empty directory. If the Linux 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 working with it.

This tutorial will help you get familiar with the commands to remove a directory in Linux. It will also give you more confidence in working with Linux systems.

Let’s learn how to delete a directory in Linux!

Delete an Empty Directory in Linux Using the rmdir Command

Here is a simple example, first 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

You can use the rmdir command to delete this directory:

To delete empty directories in Linux you can use the rmdir command.

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 for 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 only deletes an empty directory.

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

How to Remove a Directory in Linux When It is Not Empty

To delete non-empty directories in the Linux operating system you can use the rm command.

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

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

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 (you can use the rm command to delete files and directories in Linux).

Читайте также:  Astra linux совместимые мфу

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

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

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

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

To remove a directory and all the files and subdirectories in it in a recursive way in the Linux command line, you can pass the -r flag to the rm command.

Finally! We have deleted the directory.

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 a 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 because the file is write-protected.

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.

Delete Multiple Directories in Linux Using the rm Command

The rm command can delete multiple Linux directories with a single command.

You can do that by specifying the directories 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:

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

Let’s see another example of a 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.

Remove Directories in Linux Using the Find Command

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

To delete a Linux 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.

Читайте также:  Установка 2 операционных систем windows linux

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

Let’s say we have the following subdirectories in the current working 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 that identifies directories with a name that starts with “tmp_” is:

The flag -type with value d restricts the search to directories, and the -name flag allows passing a pattern we want the name of the directory to match.

The output of the command is:

Now, if we want to delete the directories we have found you 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 you run the command below the two directories get removed as expected but you also see the following error:

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

Let’s take as an example tmp_dir1…

The find command identifies these two directories based on the -type and -name flags. It deletes the directories and after that, it tries to access them to look for anything else inside those directories that match the same pattern.

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

Delete a Directory in Linux If It is 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.

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 than 7 days:

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

More recent versions of the find command also support the plus ( + ) sign instead of the semicolon ( ; ) so you can also write the previous command as below:

find . -type d -name 'tmp_*' -ctime +7 -exec rm -r <> +

The version of the command with the + at the end optimizes 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 <> +

Why Can’t I Remove a Directory in Linux?

If there is a directory you cannot remove in Linux there are two possible options:

  1. Your user has filesystem permissions to remove the directory: in this case, make sure you are using the correct command to delete the directory. If a directory is not empty, to delete it you have to use the rm command and pass the -r flag to it for recursive deletion.
  2. Your user doesn’t have filesystem permissions to remove the directory: it’s either something you shouldn’t be allowed to do or the filesystem permissions are not configured properly and need to be modified to allow your user to delete the directory.

To learn about Linux filesystem permissions have a look at the Codefather tutorial about the chmod 755 command.

How Can You Delete a Parent Directory in Linux?

A parent directory in Linux is the directory that contains the current working directory. To delete a parent directory you can use the Linux rm command as long as your user has filesystem permissions to delete that directory. There is no difference between deleting a parent directory and deleting any other directory.

Let’s open the Linux terminal and execute the pwd command to check the current working directory:

(localhost) $ pwd /opt/tutorial/test_dir

In this case, /opt/tutorial/test_dir is the current working directory, and /opt/tutorial is the parent directory.

To see the content of the parent directory you can use the command ls -al ../ that shows the contents of the directory one level above the current one (the parent directory).

(localhost) $ ls -al ../ total 0 drwxr-xr-x 3 codefather codefather 96 21 Apr 18:58 . drwxr-xr-x 169 codefather codefather 5408 21 Apr 18:58 .. drwxr-xr-x 2 codefather codefather 64 21 Apr 18:58 test_dir

Note: make sure not to delete directories that are needed by the Linux operating system to function (some of those are /etc, /opt, /var, /bin, /home, /tmp, /usr).

Conclusion

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

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

Let me know if you have any questions 🙂

Related article: let’s cover more Linux basics with a Codefather tutorial on how to rename files and directories in Linux.

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

Источник

Оцените статью
Adblock
detector