user3639557 user3639557 - 4 months ago 8x
Linux Question

How to copy via wild card in linux/bash?

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


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


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/


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

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.