How to delete or remove a directory on Linux

Overview

In this tutorial, you will learn the different methods of how to delete or remove a directory on a Unix or Linux filesystem.

There are a few utilities provided by Linux for deleting things on a Linux filesystem. Each simplistic and with their own strengths, but extremely effective. You will learn about the rm and the rmdir commands and their differences.

You will also learn how to securely wipe data from a Linux filesystem using the rm command. An important and crucial understanding when dealing with Personal Identifiable Information (PII) or passwords.

How to use the rmdir command

The rmdir command removes a directory or list of directories from the file system, provided that they are empty. The command is very simplistic and does not offer much in the way of features.

Deleting a single, empty directory.

To delete a single empty directory rmdir enter the directory path as an argument. For example, to delete a directory /tmp/logs, you would use the following command.

rmdir /tmp/logs

Deleting Multiple Empty Directories with rmdir

A list of directories can be given to the rmdir command. Each directory will be removed, provided it is empty.

To delete multiple directory you will need to use the -p flag. When set, each directory argument is treated as a pathname of which all components will be removed.

For example, to delete directories /tmp/logs1, /tmp/logs2, and /tmp/downloads, you would run the following rmdir command.

rmdir -p /tmp/logs1 /tmp/logs2 /tmp/downloads

Deleting non-empty directories with rm

The rm command is typically used for removing non-directory type files. However, it does support removing directories and it is the most common method for removing them.

Unlike the rmdir command, the rm command will remove empty and non-empty directories. If a directory is not empty, its contents will be deleted along with the directory. This includes nested directories.

Removing directories and other file types with rm

To remove directories with the rm command, you can use -d flag. This instructs rm to include directories when deleting filesystem objects, which are excluded by default.

rm -d ./temp/data

Deleting nested directory hierarchy with rm

The rm command can also delete nested directories. In order to delete a directory hierarchy on a Linux filesystem, you use the -r or -R flags.

The rm command will prompt you to confirm the nested directory deletion, as this can be a very destructive action.

rm -r ./tmp/data

Forcefully delete nested directories with the rm command

The most commonly used way of deleting directories and nested directories on a Linux file system is to do so forcefully. This a very dangerous way of deleting an entire directory hierarchy, as there is no confirmation prompt.

All files and directories in the path given will be removed from the filesystem, provided the user has permission to those files and directories. Use this command with extremely caution.

To forcefully remove all files and directories, you use the -r and the -f flags. Flags can be combined with a single hyphen to simply the command.

For example, to delete all files and directories under the path /tmp/app, you would use the following command.

rm -rf /tmp/app

Secure privacy deletes with the rm command

A typical file deletion on nearly all filesystems only deletes the file’s entry in the filesystem database. The bytes will remain on disk until they are eventually overwritten.

When dealing with highly sensitive data, a simple file or directory deletion will not securely remove the data. The rm command is capable of overwriting data on file deletion, and it does this by written a pattern of 0xff, 0x00, and 0xff over the files.

This approach effectively makes it impossible to recover the data.

To securely delete files and directories from a Linux filesystem, use the -P flag.

rm -rf -P /tmp/sensitive-data

Conclusion

When deleting a file from a Linux or Unux filesystem, there are several ways the task can be accomplished. You can use either the rmdir command or the rm command, depending on your needs.

The rm command is the most common method for deleting files, as it is the only methods to delete non-empty directories. However, it can also be the most dangerous tool to use when used inappropriately.

We’ve also learned that the rm command provides a method for secure file and directory deletion. Anytime you are handling sensitive data, such as passwords or Personal Identifiable Information (PII), it should also be deleted in a secure way to protect individual privacy and to prevent data leaks.