In one of our previous articles, we explained how to find out top directories and files consuming the most disk space on file system in Linux. If you notice that such directories no longer contain important files and subdirectories (such as old backups, downloads etc..), then you can delete them to free up space on your disk.

Read Also: 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

This short tutorial describes how to find and delete directories recursively in the Linux file system.

To achieve the above purpose, you can employ the find command together with rm command using the syntax below. Here, the + sign at the end enables multiple directories to be read simultaneously.

$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -exec /bin/rm -rf {} + 

Attention: You must use rm command carefully because it is one of the most dangerous commands to use in Linux: you may accidentally delete critical system directories, thus resulting to system failure.

In the example below, we will search for a directory called files_2008 and delete it recursively:

$ $find ~/Downloads/software -name "files_2008" -type d -exec /bin/rm -rf {} + 

You can also use find and xargs; in the following syntax, -print0 action enables printing of the full directory path on the standard output, followed by a null character:

$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -print0 | xargs -0 /bin/rm -rf "{}"

Using the same example above, we have:

$ find ~/Downloads/software -name "files_2008" -type d -print0 | xargs -0 /bin/rm -rf "{}"

Last but not least, if you are concerned about the security of your data, then you may want to learn 3 ways of permanently and securely deleting ‘Files and Directories’ in Linux.

Do not forget to read more useful articles about file and directory management in Linux:

  1. fdupes – A Command Line Tool to Find and Delete Duplicate Files in Linux
  2. How to Find and Remove Duplicate/Unwanted Files in Linux Using ‘FSlint’ Tool
  3. 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions

In this article, we showed you how to find and remove directories recursively on Linux. If you have any question or extra ideas you want to add to this topic, use the comment section below.

Similar Posts