Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive copy of specific files in Unix/Linux? [closed]

I need to copy all *.jar files from directory and all its subdirectories. How can I do it in UNIX/Linux terminal? Command cp -r *.jar /destination_dir doesn't work.

like image 770
Yury Pogrebnyak Avatar asked Mar 08 '12 18:03

Yury Pogrebnyak


People also ask

Does Linux have recursive copy?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied. As an example, let's say that you want to copy the “/etc” directory into a backup folder named “/etc_backup”.

Which command is used to recursively copy the files in Unix?

To copy files or directories in recursive manner, use -r or -R option.

What is the command to copy files recursively?

Use xcopy with /S switch to copy all files and directories recursively.


1 Answers

rsync is useful for local file copying as well as between machines. This will do what you want:

rsync -avm --include='*.jar' -f 'hide,! */' . /destination_dir

The entire directory structure from . is copied to /destination_dir, but only the .jar files are copied. The -a ensures all permissions and times on files are unchanged. The -m will omit empty directories. -v is for verbose output.

For a dry run add a -n, it will tell you what it would do but not actually copy anything.

like image 172
Sean Avatar answered Sep 20 '22 04:09

Sean