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

Copy a file to all subfolders of a folder with one command UNIX
Over the weekend, I was installing a demo of some web-based help desk software, and this particular package used encrypted PHP files. With my hosting company, I have to place a php.ini file in every directory that contains encrypted files, or the site won't work. This particular package had hundreds of directories, buried in folders and subfolders, and I was dreading the task of copying this one file into each of them.

A tip from my friend James pointed me to the solution, in the form of Smiling Dragon's reply in this thread over on the Unix/Linux forums. Assuming the file exists in the parent directory, then this command does the trick:
find . -type d -exec cp php.ini {}/ \;
This worked perfectly, and incredibly quickly -- at first I thought it had failed, but a quick sample of a deeply-buried folder showed that the file was now in each and every sub-directory on the site. Note that if something goes wrong when you try this, you may create quite a mess to clean up; I tried it first on my local machine, just to be sure it worked. Only then did I run the command on my web server.
    •    
  • Currently 3.32 / 5
  You rated: 2 / 5 (19 votes cast)
 
[16,655 views]  

Copy a file to all subfolders of a folder with one command | 8 comments | Create New Account
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: zeniam1 on Mar 02, '10 07:57:43AM
Via Bash for just the immediate children directories:
for d in *; do [[ -d "$d" ]] && cp php.ini "$d"; done
I knew about the -exec flag in find, but I'm more comfortable just writing quick Bash scripts. Note that the given find command will descend into subdirectories, whereas the above one-liner will not.

[ Reply to This | # ]
Copy a file to all subfolders of a folder with one command
Authored by: fracai on Mar 02, '10 10:59:22AM
Rather than: for d in *; do [[ -d "$d" ]] && cp php.ini "$d"; done
You can do: for d in */; do cp php.ini "$d"; done
---
i am jack's amusing sig file


[ Reply to This | # ]
Copy a file to all subfolders of a folder with one command
Authored by: zeniam1 on Mar 03, '10 12:33:57PM

Most excellent! Thanks for the tip!



[ Reply to This | # ]
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 | # ]
Copy a file to all subfolders of a folder with one command
Authored by: NumbuhOne on Mar 02, '10 10:05:39AM
to test the commands find's -exec command line argument will invoke, prepend the echo command to be invoked:

find . -type d -exec echo cp php.ini {}/ \;

that will display the commands without actually invoking them.

[ Reply to This | # ]
Copy a file to all subfolders of a folder with one command
Authored by: arcticmac on Mar 02, '10 01:35:05PM

an interesting alternative if you're literally creating thousands of copies of the same file would be to use links (symlinks if they work for your web server, or even hard links if symlinks aren't acceptable) by using 'ln' instead of 'cp'.



[ Reply to This | # ]