Find and replace a file by another file, recursively in all subdirectories

I needed this when getting fed up of manually copying the favicons.ico file from my web root directory to Studiopress theme folders (there were many).

So I wrote a bash script to find all files names favicon.ico in all subdirectories under the current directory, and overwrite it by the file I wanted.

The following script lists the directories containing the file..

[email protected] [~/public_html/droidzone.in/wp-content/themes/studiopress]# find . -type f -name "favic*" -pr                                                       intf "%h\n" | xargs -i echo Found in "{}"'/'
Found in ./landscape/images/
Found in ./serenity/images/
Found in ./socialeyes/images/
Found in ./sleek/images/dark/
Found in ./sleek/images/
Found in ./magazine/images/
Found in ./streamline/images/
Found in ./education/images/
Found in ./focus/images/
Found in ./pixelhappy/images/
Found in ./scribble/images/
Found in ./fabric/images/
Found in ./crystal/images/
Found in ./prose/images/
Found in ./amped/images/
Found in ./metric/images/
Found in ./agency/images/
Found in ./tapestry/images/
Found in ./news/images/
Found in ./corporate/images/
Found in ./pretty/images/
Found in ./church/images/blue/
Found in ./church/images/
Found in ./church/images/brown/
Found in ./church/images/gray/
Found in ./outreach/images/
Found in ./familytree/images/
Found in ./platinum/images/
Found in ./minimum/images/
Found in ./enterprise/images/
Found in ./blissful/images/
Found in ./manhattan/images/
Found in ./eleven40/images/
Found in ./lexicon/images/
Found in ./agentpress-two/images/
Found in ./generate/images/
Found in ./midnight/images/
Found in ./expose/images/
Found in ./goinggreen/images/
Found in ./delicious/images/
Found in ./beecrafty/images/
Found in ./nitrous/images/
Found in ./lifestyle/images/
Found in ./venture/images/
Found in ./associate/images/
Found in ./freelance/images/
Found in ./genesis/images/
[email protected] [~/public_html/droidzone.in/wp-content/themes/studiopress]#

The next script copies the file favicon.ico from /home/joelgmat/public_html/droidzone.in/favicon.ico to the subdirectories which contain favicon.ico, residing under the current directory

find . -type f -name "favic*" -printf "%h\n" | xargs -i cp /home/joelgmat/public_html/droidzone.in/favicon.ico "{}"'/'

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.