Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from Jan 4 '10 at 22:02

This question came from our site for professional and enthusiast programmers.

You'll get a better response on But you don't have to do anything - if enough people agree the question will be moved automatically. – ChrisF Jan 4 '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 4 '10 at 22:01

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 \;
share|improve this answer

This will preserve the directory structure.

find photos/ -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -print0 |xargs -0 tar c |(cd /cpjpg ; tar x)
share|improve this answer

This preserves the directory structure:

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


share|improve this answer

You must log in to answer this question.