Slightly stumped on what CP command would copy files numbered 14 trough 39 into a directory called Volume2 from the following example:

01.mp3  04.mp3  07.mp3  10.mp3  13.mp3  16.mp3  19.mp3  22.mp3  25.mp3  28.mp3  31.mp3  34.mp3  37.mp3
02.mp3  05.mp3  08.mp3  11.mp3  14.mp3  17.mp3  20.mp3  23.mp3  26.mp3  29.mp3  32.mp3  35.mp3  38.mp3
03.mp3  06.mp3  09.mp3  12.mp3  15.mp3  18.mp3  21.mp3  24.mp3  27.mp3  30.mp3  33.mp3  36.mp3  39.mp3 
  • No cp command would do that. Jul 17 '12 at 2:48
  • cp {14..39}.mp3 Volume2 does it on my system...assuming the directory exists, of course.
    – user55325
    Jul 17 '12 at 2:50

You could just do

cp {14..39}.mp3 Volume2

But the for-loop is handy for other things:

for f in {14..39}.mp3; do cp "$f" Volume2; done
  • Not homework, though I am an on again/off again student focusing on Linux Admin.
    – user98496
    Jul 17 '12 at 2:53
  • Awesome. Will give cp {14..39}.mp3 Volume2 a try.
    – user98496
    Jul 17 '12 at 2:56
  • 1
    Just for completion: the for-loop will have N cp invocations where N is the number of songs, as compared to the single invocation of the first example. Fewer invocations means faster and less resource intensive (in this simple case it doesn't matter in practice, though). A caveat is that if the expanded list exceeds ARG_MAX-1 (check value with getconf ARG_MAX, it is much higher than 39-14) number of entries, it will fail. Utilities such as xargs and find can take care of this caveat by splitting on ARG_MAX. Jul 17 '12 at 8:37
  • for the above example this wouldn't work for ranges including numbers below 10. For those you could do something like cp 0{1..9}.mp3 {10..39}.mp3 Volume2
    – georg
    Oct 30 '15 at 13:00
  • @georg It works fine for me, in bash version 4.3.42 - what shell are you using?
    – user55325
    Nov 22 '15 at 3:06

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.