2

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
1

You can also use rsync -R (--relative) or --parents in GNU cp:

rsync -R */*/*.RNav.jpg /path/to/target
gcp --parents */*/*RNav.jpg /path/to/target
find . -name \*RNav.jpg -exec rsync -R {} /path/to/target \;
find . -name \*RNav.jpg -exec gcp --parents {} /path/to/target \;
  • This one works!!! – user64184 Dec 7 '13 at 19:15
3

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.

  • Gracias! Worked perfectly! – user64184 Dec 6 '13 at 18:53
  • After further testing, I realized that it's including the other JPG files that aren't tagged with RNav.jpg. I tried varying the wildcard as follows to no avail: --exclude "*.jpg" – user64184 Dec 6 '13 at 22:18
  • Huh, are you sure? I don't get that result here. Maybe the other files already exist in the destination directory? rsync won't remove them unless you add --delete-excluded (to be really clear, that would delete all files from the destination folder, except the ones ending in RNav.jpg). – gabedwrds Dec 7 '13 at 0:13
  • The destination directory is completely empty before running the command line so I'm not sure why it's extracting and copying those other JPG files. – user64184 Dec 7 '13 at 2:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .