How to Search and Remove Directories Recursively on Linux

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply
  1. Hi,

    The below code deletes all the directories that starts with/contains the name “files_2008” or it deletes only one directory which matches the name “files_2008“.

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

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.