user3639557 user3639557 - 6 months ago 15
Linux Question

How to copy via wild card in linux/bash?

Here is what I am trying to do - assume a source directory:

/xyz/2/file.txt
/xyz/2/crapfile.txt
/xyz/3/file.txt
/xyz/3/crapfile.txt


and I want to copy file.txt into the following directory:

/z/2
/z/3


I like to do something like this, but this doesn't work:

cp -r /xyz/*/file.txt /z/*/


to avoid copying things separately - which I can do with the following:

cp -r /xyz/2/file.txt /z/2/
cp -r /xyz/3/file.txt /z/3/

Answer

This command copies from xyz to z excluding all files named crapfile.txt:

rsync -a --exclude=crapfile.txt xyz/ z

With the -v (verbose) option, we can see its work in progress:

$ rsync -va --exclude=crapfile.txt xyz/ z
sending incremental file list
./
2/
2/file.txt
3/
3/file.txt

As you can see, file.txt was copied but crapfile.txt was skipped.

rsync is a very power utility with many many options. For more info, see man rsync or any of the rsync tutorials on the web.

Comments