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.

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)