Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need a unix command for the following:

Directory Structure

/project
    /images
        /products
            /cup.jpg
            /laptop.jpg
        /designs
            /alpha.jpg
            /beta.jpg
            /gamma.jpg
        /team
            /jeff.jpg
        /locations
            /new-york.jpg

I would like one command to copy all files* not folders out of the "images" directory recursively to something like this.

/assets
    /cup.jpg
    /laptop.jpg
    /alpha.jpg
    /beta.jpg
    /gamma.jpg
    /jeff.jpg
    /new-york.jpg       

I tried this but it just copied the folders recursively cp -r ./project/images/* ./assets/

share|improve this question
4  
I just found this and it works find project -type f -exec cp {} assets \; –  ThomasReggi Apr 23 '13 at 17:45
    
Please post your solution as an answer. Answering your own questions is not only accepted but encouraged. I would recommend, however, that you quote {} so you won't have trouble with file names with spaces: find project/images -type f -exec cp '{}' assets \; –  terdon Apr 23 '13 at 21:25
    
@terdon {} does not have to be quoted. –  ؘؘؘؘ Apr 24 '13 at 0:27
    
@LauriRanta I stand corrected, thanks. –  terdon Apr 24 '13 at 0:49

1 Answer 1

This executes cp for every file, so it can be noticeably slower if there are many small files (but not with typical images):

find project -name \*.jpg -exec cp {} assets \;

This takes multiple arguments at a time:

find project -name \*.jpg -print0 | xargs -0 -I% cp % assets

Without -0 filenames that contain single quotes or double quotes would result in an error like xargs: unterminated quote.

Bash 4 supports ** with shopt -s globstar:

cp project/**/*.jpg assets
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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