Here’s a neat little command that will let you delete specified files/directories recursively and based on their names.
Let’s do a dry run first to make sure that the command will go through the right files. Run:
[bash]<code class="plain plain">find <directory_to_start_the_recursion_in> -name <file_name</code>>[/bash]
Keep in mind that if you’re gonna have asterisks (*) in the <file_name> you need to escape them like so:
[bash]find /var/www -name *.jpg[/bash]
make sure that the result only lists the files/directories that you indeed want to obliterate. Then improve that last command by adding:
[bash]find <directory_to_start_the_recursion_in> -name <file_name> -exec rm -rf {} ;[/bash]
Since this is a pretty dangerous command even after a dry run, you can use -ok instead of -exec which will prompt you for approval everytime the command it executed.
[bash]find <directory_to_start_the_recursion_in> -name <file_name> -ok rm -rf {} ;[/bash]
This is of course not limited to rm 🙂
Leave a Comment