Unix command to recursively copy JPG files in a complex directory tree I have a large directory tree structure that contains multiple JPG files in each directory root and their subsequent subdirectories. I need to recursively extract a single JPG file from each directory root and copy them all to another location whilst maintaining the same tree structure and ignoring the other files. The JPG filenames that I need to extract all are tagged with "RNav.jpg". I experimented with the cp command and wild card but I could not get it to work. I'm running Mavericks and I'm a novice when it comes to UNIX command-lines.

The existing structure is as follows: (up to 500 directories)


VNAME/PNAME1/PNAME1_RNav.jpg
VNAME/PNAME2/PNAME2_RNav.jpg
VNAME/PNAME3/PNAME3_RNav.jpg
VNAME/PNAME4/PNAME4_RNav.jpg
You can easily use rsync to accomplish this:

rsync --include "*/" --include "*RNav.jpg" --exclude "*" -am source destination


Based off this page. This includes folders and *RNav.jpg files and then excludes everything else. I've used -a instead of -r to preserve more information (my personal preference) and added -m to prevent rsync from creating any parts of the directory tree that wouldn't contain any files.