-1

So if I can do this:

$ mkdir ./blah_{007,011,090}

And end up with three directories...

Why can't I do this...

cp ../blah_{007,011,090}.swf ./blah_{007,011,090}_src

And copy the files into 3 directories?

  • 1
    why should it? man cp shows only a single attribute for DEST compared to SOURCE... (see the ...). How should cp know when the SOURCES end and when the DEST begins? – pLumo Jul 26 at 15:02
  • I more interesting question would be how do I copy 3 files into 3 directories that share a name with the files? – ctrl-alt-delor Jul 26 at 15:45
  • @ctrl-alt-delor wasn’t that my question? – leeand00 Jul 26 at 15:51
  • 1
    No: you asked, what is the reason that the way that I demonstrated does not work (and this is how everyone interpreted it). – ctrl-alt-delor Jul 26 at 15:53
  • Once could interpret the two brace expansions in your example as examples instead of being accidentally identical. What if you wanted to copy all *a* files into the 007 directory and all *qqq* files to the 011 directory? If your use-case is identically-named files, it would be good to put that into your question. – Jeff Schaller Jul 26 at 16:52
3

You're effectively running:

cp ../blah_007.swf ../blah_011.swf  ../blah_090.swf ./blah_007_src ./blah_011_src ./blah_090_src

I trust it is obvious why the above command is not the same as:

cp ../blah_007.swf ./blah_007_swc
cp ../blah_011.swf ./blah_011_swc
cp ../blah_090.swf ./blah_090_swc

If you want the later, loop over cp:

for f in blah_{007,011,090}
do
  cp ../"$f.swf" "$f"_swc
done
  • Yeah, I was trying to get around writing an entire loop to do that. – leeand00 Jul 26 at 15:35
  • As I don’t have to when I use the mkdir command. – leeand00 Jul 26 at 15:36
  • Can you pass functions in bash? Cause if you can then I could write a map function. – leeand00 Jul 26 at 15:37
  • Pass functions to? If you want to mv and not cp, you could use rename instead. – muru Jul 26 at 15:44
2

See man cp:

   cp [OPTION]... [-T] SOURCE DEST
   cp [OPTION]... SOURCE... DIRECTORY
   cp [OPTION]... -t DIRECTORY SOURCE...

It shows only a single attribute for DEST compared to SOURCE... (see the ...). How should cp know when the SOURCES end and when the DEST begins.

Use a for-loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.