Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recursively copy directories starting with "abc" on Linux/Unix?

I have a directory ~/plugins/ and inside there are many sub-directories. If I wanted to create a backup somewhere else of just the sub-directories starting with abc could I do that with a one line copy command? I would assume something like this would work (but it doesn't):

 cp -R ~/plugins/abc* ~/destination/ 

I would rather use a one-line command, if possible, because I would also like to use the same syntax for rsync, and if I have to do something like

 find ~/plugins/ -type d -name "abc*" -exec cp -R {} ~/destination; 

then that works fine for the cp command but it would mean that I would have to run rsync once for each directory and that just doesn't seem efficient :(

like image 772
cwd Avatar asked Mar 11 '11 16:03

cwd


People also ask

How do you copy directories in Unix?

To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command. The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.

How do I copy a directory in Unix recursively?

In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character.

How can you copy a directory recursively with all of its contents including sub directories )?

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.


1 Answers

Not sure why what you're trying didn't work (but what is the "copy" command?), but this works on Linux at least:

cp -r ~/plugins/abc* ~/destination 
like image 103
David Gelhar Avatar answered Oct 13 '22 22:10

David Gelhar