How do I empty a directory in Linux?

13

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.

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220935 420 How do I empty a directory in Linux

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.

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220936 29 How do I empty a directory in Linux

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

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220936 12 How do I empty a directory in Linux

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.

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220937 952 How do I empty a directory in Linux

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.

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220938 303 How do I empty a directory in Linux

$ rm -r testfolder1 testfolder2 testfolder3

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

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220938 822 How do I empty a directory in Linux

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

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220939 876 How do I empty a directory in Linux

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 {} +

how do i empty a directory in linux? How do I empty a directory in Linux? 1623220940 706 How do I empty a directory in Linux

Conclusion

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More