Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy multiple files from a different directory using cp?

Tags:

linux

bash

cp

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.

like image 832
ziulfer Avatar asked Mar 28 '12 21:03

ziulfer


People also ask

How do you copy multiple files from one folder to another?

To do this, click and hold your left mouse button on the top-left portion of where you want to start highlighting. Next, drag the box until the last file or folder is highlighted. Once the files are selected, they can be copied, cut, or dragged to another window to move them.

How do I copy multiple files using Linux cp?

To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.

How do you copy all the files from a directory to another directory?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.


2 Answers

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} . 
like image 95
Philipp Avatar answered Sep 27 '22 17:09

Philipp


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.

like image 23
ghoti Avatar answered Sep 27 '22 16:09

ghoti