Let's say I have 10 subdirectories in a main-directory named d1, d2, d3, ..., d10. How can I copy a file into all these subdirectories using cp?
I'm looking for something like
cp ./my_file.txt ./d*/
It doesn't work this way, for multiple reasons: Firstly, cp
only accepts one single destination argument. For example, if you execute the following command
cp file1.txt file2.txt something_else.txt my_destination/
from the list of arguments to cp
, file1.txt dir7/ file2.txt my_destination/
, only the very last one will be interpreted as the destination where to copy the others to.
Secondly, wildcards get handled differently in Bash than you seem to think. Bash does a lot of so called expansions beforehand, before the command in question even gets called. One of those expansions concerns the *
wildcard.
Let's say you have the directories d1
, d2
and d3
. In this case, Bash would expand d*
to d1 d2 d3
. So the following
cp my_file.txt d*
would become
cp my_file.txt d1 d2 d3
all before cp
even gets called. And this would mean something like "copy myfile.txt
, d1
and d2
to d3
". Remember, only the very last parameter gets interpreted as the destination.
What you can do is a simple for
loop over the directories. To give you the general idea:
for DIR in d*; do
cp my_file.txt "$DIR"
done
In case you also have files that would match d*
, in addition to the directories, things would get a bit more involved. But that would be a seperate question ;)