0

In a Mac Terminal, I want to find all directories that contain at least one file with the specified extension and copy them somewhere else. I found find . -iname '*.jpg' -exec dirname {} \; which seems to find all directories containing a *.jpg file, but I'm not sure how to copy them. I tried combining it with rsync but couldn't get it to work. What's the best way to do this?

0

Here's a bash friendly one liner that copies any folders that have a jpg in them into a folder called backup while maintaining directory structure

mkdir backup; for folder in $(find . -type f -name '*.jpg' | sed -r 's|/[^/]+$||' |sort |uniq); do cp -r --parents $folder backup; done

First it creates an empty backup folder then the find command looks for all the files in the current directory that end with .jpg. The sed, sort, and uniq commands trim the find output to just the directory names and remove repeats. Finally the cp -r --parents in a loop copies the folders over recursively while creating any files that are missing.

you can get around using sed -r by using the -printf flag with find like so

mkdir backup; for folder in $(find . -type f -name '*.jpg' -printf "%h\n" |sort |uniq); do cp -r --parents $folder backup; done

if your find doesn't support -printf you can try using grep

mkdir backup; for folder in $(find . -type f -name '*.jpg' | grep -o "\(.*\)/" |sort |uniq); do cp -r --parents $folder backup; done
  • This looks prefect but doesn’t work in Mac Terminal. Can you please edit it to work in Terminal? Thanks – Dominik Novák Feb 22 '18 at 22:53
  • Can you describe the error you're getting? I don't have a mac to test on. Those commands should be cross platform between unix based systems. – imbuedHope Feb 23 '18 at 13:44
  • This is what i get: sed: illegal option -- r usage: sed script [-Ealn] [-i extension] [file ...] sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...] – Dominik Novák Feb 23 '18 at 13:51
  • added a solution that doesn't depend on sed -r see if that works – imbuedHope Feb 23 '18 at 18:04
  • This is what I get now: find: -printf: unknown primary or operator – Dominik Novák Feb 23 '18 at 18:25
0

To find all files with the extension .py, this should work on OS X shells:

find . *.py -print0 | xargs -0 -I{} cp -r {} /some/dir/name/

Note:

  • find -print0 prints files terminated with a null char instead of a newline, and xargs -0 is used for input in that format. This is to better handle filenames and paths that have spaces or other whitespace, which is otherwise often a headache.
  • OS X uses different versions of find and cp than Linux/GNU does, so while this example would work with GNU utilities, many examples or answers for similar questions that use GNU utilities do not work on OS X.
  • Works only for files, not for the directories and all content. – Dominik Novák Feb 22 '18 at 22:52
  • Maybe I misunderstood the question. You want to find files by extension, and then copy those and everything else in their folders they are in? – Christopher Hunter Feb 22 '18 at 23:13
  • Yes, that was my question – Dominik Novák Feb 22 '18 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.