recursive name based delete

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:

<code class="plain plain">find <directory_to_start_the_recursion_in> -name <file_name</code>>

Keep in mind that if you’re gonna have asterisks (*) in the <file_name> you need to escape them like so:

find /var/www -name *.jpg

make sure that the result only lists the files/directories that you indeed want to obliterate. Then improve that last command by adding:

find <directory_to_start_the_recursion_in> -name <file_name> -exec rm -rf {} ;

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.

find <directory_to_start_the_recursion_in> -name <file_name> -ok rm -rf {} ;

This is of course not limited to rm 🙂

recursive type based chmod

Here’s a cool little script that will recursively chmod, giving a permission based on whether it’s dealing with a file or a directory. This is very convenient when you want to add that +x to directories but not files.

find $1 -type f -exec chmod $2 {} ;
find $1 -type d -exec chmod $3 {} ;

Go ahead and edit /usr/bin/chmod_script, copy paste these 2 lines in there, then issue a chmod 755 /usr/bin/chmod_script as root, that’s it!

Usage syntax is as follows:

chmod_script <directory_to_start_the_recursion_in> <permissions_for_files> <permissions_for+directories>

so if I want to use it on /var/www do:

chmod_script /var/www 644 755

Enjoy!