52

I want to copy multiple files from a specific directory once I am in another directory. To clarify I want to do the following, at once (one command):

cp ../dir5/dir4/dir3/dir2/file1 .
cp ../dir5/dir4/dir3/dir2/file2 .
cp ../dir5/dir4/dir3/dir2/file3 .
cp ../dir5/dir4/dir3/dir2/file4 .

I can't use cp ../dir5/dir4/dir3/dir2/* . because in dir2 there are n files (n>4)

By the way, I'm using bash.

Thanks.

torrential coding
  • 1,755
  • 2
  • 24
  • 34
ziulfer
  • 1,339
  • 5
  • 18
  • 30
  • 1
    cp -t Directory source is an option This can be used to copy all/selected files from one dir to another.... – Xander Mar 29 '12 at 05:48

3 Answers3

84
cp ../dir5/dir4/dir3/dir2/file[1234] .

or (in Bash)

cp ../dir5/dir4/dir3/dir2/file{1..4} .

If the file names are non-contiguous, you can use

cp ../dir5/dir4/dir3/dir2/{march,april,may} .
Philipp
  • 48,066
  • 12
  • 84
  • 109
  • thanks actually in the real case the name of the files are completely different. `cp ../dir5/dir4/dir3/dir2/[march april may] .` will not work – ziulfer Mar 28 '12 at 21:24
  • 2
    Why don't you tell us the real case then? – Philipp Mar 28 '12 at 21:25
  • 1
    @ziulfer: OK, that one is also possible with the brace syntax. – Philipp Mar 28 '12 at 21:40
  • Thank you once again. This brace syntax has been very useful to me. However, one stuff that I really miss from the default, file-by-file, cp command is the possibility to have auto-completion. Is there a possibility to have this with the brace syntax? – ziulfer May 20 '14 at 15:37
  • in case someone runs in to this, it is important there are no spaces between commas and filenames in the `{alpha.a,beta.b,gamma.g,delta.d}` type list – Piotr May 01 '15 at 16:47
5

If all the files you want to copy are in the pattern of file{number}{othertext}, you could use something like:

cp ../dir5/dir4/dir3/dir2/file[0-9]* .

Note that this will copy file5, but it will also copy file0abc.

If you would like to copy ONLY those four files (and not the {othertext} ones), you can use:

cp ../dir5/dir4/dir3/dir2/file[1-4] .

Note that while this looks like part of a regular expression, it is not.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • thanks actually in the real case the name of the files are completely different. `cp ../dir5/dir4/dir3/dir2/[march april may] .` will not work – ziulfer Mar 28 '12 at 21:31
  • But that wasn't your question. – ghoti Mar 29 '12 at 11:14
  • Also want to mention that `cp ../dir5/dir4/dir3/dir2/F* .` works. This is for the case where only the starting letter in the filenames is the same. And only those files starting with `F` are desired to be copied – smac89 Dec 25 '14 at 07:22
3

Try this one:

 cp ../dir5/dir4/dir3/dir2/file{1..4}
zbyszek26104
  • 437
  • 1
  • 5
  • 15