7

I wish to recursively go through the folders of a directory structure and copy any .jpg I find into another directory.

I think I had the wrong idea with:

cp -R photos/*.jpg /cpjpg

How can I do this from the command line in Ubuntu?

Excellll
  • 12,627
  • 11
  • 51
  • 78
  • You'll get a better response on http://superuser.com. But you don't have to do anything - if enough people agree the question will be moved automatically. – ChrisF Jan 04 '10 at 22:00
  • Do you want them all to end up in one directory (flatten) or do you want to preserve the stucture? – gbarry Jan 04 '10 at 22:01

3 Answers3

13

This will copy all files ending in .jpg or .jpeg (case insensitive as well) in the current directory and all its subdirectories to the directory /cpjpg. The directory structure is not copied.

find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec cp '{}' /cpjpg \;
the
  • 2,751
  • 1
  • 26
  • 35
Vinko Vrsalovic
  • 2,416
  • 1
  • 21
  • 20
10

This preserves the directory structure:

rsync -av --include='*.jpg' --include='*/' --exclude='*' SRC DST

see http://logbuffer.wordpress.com/2011/03/24/linux-copy-only-certain-filetypes-with-rsync-from-foldertree/

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
akira
  • 61,009
  • 17
  • 135
  • 165
5

This will preserve the directory structure.

find photos/ -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -print0 |xargs -0 tar c |(cd /cpjpg ; tar x)
the
  • 2,751
  • 1
  • 26
  • 35
amphetamachine
  • 1,668
  • 2
  • 12
  • 15