mkdir test_dir test_dir1 test_dir2 test_dir3
touch file1 file2 file3
cp -v file* test_dir*/
This copies only to test_dir3 and not in test_dir, test_dir1, test_dir2. Why?
For that matter it copies only to latest directory say test_dir4 and not into any other direcotry (it is omitting them).
You have these three commands
Assuming no other files or directories in
.
before the example is started, the wildcards in that last line get expanded thus:(You can see this by changing
cp
toecho cp
and observing the result.) What you didn't tell us is the diagnostic messages produced bycp -v
, which show what it is trying to do, i.e. copy every item on the command line but the last into the last item, which must therefore be a directory:Because the
cp
commands works so. If it has more as 2 argument, the last argument must be a directory, and every argument before that will be copied into.I think you want a
cp
which behaves much more like the windowscopy
command, and yes, there are such tools, although they aren't widely used. Even I had to google for that.Your actual goal could be easily reached by a simple loop:
It is much more simple as it seems on the first spot. Bash is actually a basic-like programming language, and doing loops or even more complex operations in single-line commands in a unix environment is quite common.