How do I empty a directory in Linux?

0 10

In Linux systems, various methods are used for removing directories. As a Linux user, you can remove files and folders using a graphical user interface or by Desktop file manager; for example, KDE Dolphin, Gnome’s Files can be used for the same purpose. However, if you want to remove several directories instantly and are operating on a headless server, the safest way is to use the command line for deleting the directories. We’ll show you the method for removing directories using rm, rmdir, and find commands.

rmdir command

The command-line utility “rmdir” is used to delete empty files or directories. Rather than checking a directory whether it is empty or not, you can only delete an empty directory. In the following example, we will delete the “testfolder” directory with the help of the “rmdir” command.

In the “rmdir” command, specify the name of the directory you want to remove from your file system. For example, we added “testfolder” in the following command.

Oh! The output states that the directory is not empty; therefore, “rmdir” is failed to remove it. In this case, we will move forward to use the “rm” command to remove it from the system.

rm command

Another most popular command-line tool used for removing files and directories is the “rm” command. Contrary to the “rmdir” command, the rm command can remove both empty and non-empty files and directories. “rm” provides various options.

-r or -R options removes a non-empty directory with all of its content, whereas the -d option enables you to delete an empty directory. For example, to remove a “testfolder” with all of its content, utilize the -r option in the “rm” command.

As you can see, now we do not have the “testfolder” in our home directory.

If you want to remove a write-protected directory or files with it, A prompt will appear for you to confirm the deletion. Use the -f option for deleting this type of directory without getting the prompt.

Invoke the “rm” utility, specify the name of multiple directories separated by space to remove them at once. For instance, the directories listed below “testfolder1”, “testfolder2”, ”testfolder3” can be removed instantly using the “rm” command, and we will show you how to do that.

$ rm -r testfolder1 testfolder2 testfolder3

List out the directories using “ls -l” and check out the files to confirm the deletion.

The-I option in the “rm” command instructs it to ask for confirmation before removing the file and sub-directories.

find command

find command permits a user to search for directories and files based on a specified expression and perform any action on each matched file. Using the find command to remove directories based on a pattern is considered the most common scenario. In the below-given example, we will delete all the directories that end with “_cache” in the current directory.

$ find . -type d -name ‘*_cache’ -exec rm -r {} +


Removing directories in Linux composed of simple and straightforward procedures, but you must be aware of your important data before deleting it.  In this article, we have shown you various methods for removing directories. These methods comprise “rmdir”, “rm”, “find” command. With “rmdir,” you can only delete an empty directory, whereas “find” and “rm” utilities provide different criteria for removing directories.

Leave A Reply

Your email address will not be published.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.