How to copy a file to all subfolders in a directory using a single command?
I'd like to copy a file, say widgets/water.txt, to all subfolders in the folder widgets using a single command. So if the folder widgets has 10 subfolders like widgets/blue, widgets/green, etc. I'd like to copy water.txt to all of them with one command.
I tried the commands Code:
cp water.txt ./*/water.txt |
depends on what you mean by "with a single command". It is possible with a single commandline, using the for command (in most shells a builtin):
Code:
for d in */; do cp water.txt "$d"; done The "for" command does the right thing by invoking cp individually for each target directory. |
Something like 'find ${PWD}/* -type d -print0 | xargs -0 -iD cp /path/to/water.txt 'D';'?
|
Quote:
Code:
for i in * # iterate over all files in current dir |
Quote:
|
But i don't think the OP wants water.txt in all the subdirectories and their subderectories, too.
|
What's this silly argument good for? I don't think the OP wanted to copy it recursively into a directory tree, so i didn't use find -- finding all directories in the current directory is done fine by the shell using the pattern '*/'.
But, in case you actually WANT all subdirectories recursively, find is the way to go, as posted by unSpawn. Matching all files with '*' and later checking wheter it is a directory doesn't make sense. And, as I already explained, it's not possible using a single command, there are always at least two commands involved, BUT it is very well possible using a single line. |
Thanks guys :)
Yes zirias is right, I didn't want to copy to the whole directory tree, just the direct subfolders. zirias' code works great: Code:
for d in */; do cp wp-salesengine-page.php "$d"; done |
All times are GMT -5. The time now is 11:58 AM. |