Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Copy a file to all subfolders of a folder with one command' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Copy a file to all subfolders of a folder with one command
Authored by: ctwise on Mar 02, '10 08:17:43AM

The find -exec command is a very useful one for more then just this. It's also a generic Unix tip, not just OS/X. The most common usage tends to be removing files. For example, if you have a set of directories that are in Subversion and you need to get rid of the .svn directories scattered throughout the directory structure:

find . -name .svn -exec rm -rf \{\} \;

In the tip, the -type d was used to only target directories ('d'). You can use any of the find options to target specific items. The -exec can run pretty much any command, anywhere you have {} it will be replaced with the found filename. The -exec command will be run over and over again, once for each found file. You have to end the whole thing with a semi-colon. To get this to work from the command line, you have to escape the braces and semi-colon with a slash '\'.



[ Reply to This | # ]
Copy a file to all subfolders of a folder with one command
Authored by: robg on Mar 02, '10 09:45:54AM

When I did it, from Terminal, I didn't have to escape the brackets -- it worked as shown (bash, 10.5 and 10.6).

-rob.



[ Reply to This | # ]
Copy a file to all subfolders of a folder with one command
Authored by: fracai on Mar 02, '10 11:05:27AM
For files, I skip exec, which seems messy unless necessary, and use: find -name blah -print -delete
I also usually prefer to use xargs when possible, ie. find . -type d -print0 | xargs -0 -n1 cp php.ini
(-n1 tells xargs to execute the command once for each entry, rather than simply appending each to the end of the command)
Or: find -name .svn -print0 | xargs -0 rm -rf

All these "rm -rf" commands are going to give me nightmares.
---
i am jack's amusing sig file
Edited on Mar 02, '10 11:07:18AM by fracai


[ Reply to This | # ]