2

example I have 100 folders each with JPG and MOV files in them I want to copy all the jpg into one folder and all the mov files into another

example

  • folder1 has one.jpg and two.jpg and one.mov

  • folder2 has two2.jpg and two2.mov

  • folder3 has xxx.jpg and yyy.mov

wanted result

  • folder jpgfiles containing one.jpg two.jpg two2.jpg xxx.jpg

  • folder movfiles containing one.mov two2.mov yyy.mov

  • Are the 100 files are direct subdirs of one parent? (in this case cp */*.jpg jpgfiles/ ; cp */*.mov movfiles/ ) – JJoao Apr 14 '15 at 16:08
  • 1
    This worked a treat i used the -p flag to preserve the creation date of the pictures so I can get them in the correct order for video processing later using cinelerra – phil smart Apr 15 '15 at 18:44
3

You can use find for this. You use the folder above the folder1, folder 2 etc and execute these commands there.

find /folder/ -type f -name "*.jpg" -exec cp {} "jpgfiles/" +
find /folder/ -type f -name "*.mov" -exec cp {} "movfiles/" +

/folder/ is the parent folder here. you need to modify it to your parent folder.

EDIT: Thanks to @godlygeek for pointing it out. If you'd like to copy only unique files, you can use the below

find /folder/ -type f -name "*.jpg" -exec bash -c 'test ! -f jpgfiles/${0##*/} && { cp $0 jpgfiles/${0##*/}; }' {} +
find /folder/ -type f -name "*.mov" -exec bash -c 'test ! -f movfiles/${0##*/} && { cp $0 movfiles/${0##*/}; }' {} +

To copy the conflicting files with a unique file suffix:

find /folder/ -type f -name "*.jpg" -exec bash -c 'test -f jpgfiles/${0##*/} && { cp $0 jpgfiles/${0##*/}.$RANDOM; }' {} +
find /folder/ -type f -name "*.mov" -exec bash -c 'test -f movfiles/${0##*/} && { cp $0 movfiles/${0##*/}.$RANDOM; }' {} +
  • You should use + instead of \;, and leave out the double quotes around {}, find handles spaces etc in file names, it doesn't create a shell instance when it invokes mv that might need the double quotes – Anthon Apr 14 '15 at 15:21
  • @Anthon thanks. I`ve updated the answer. – rahul Apr 14 '15 at 15:31
  • 1
    I'd argue that you should quote {} - it's necessary in some shells (like zsh) and harmless in all others. Also, it's worth noting that if there's a "foo.jpg" in two different subdirectories, only one of them is going to wind up in "jpgfiles" (obvious in retrospect, but probably worth noting explicitly - some files may be missing from "jpgfiles" afterwards). – godlygeek Apr 14 '15 at 15:41
  • thanks Guys for the rapid answer went with the cp solution as its easier for a newcomer!! – phil smart Apr 15 '15 at 18:46

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.