LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to copy images recursively via bash (https://www.linuxquestions.org/questions/programming-9/how-to-copy-images-recursively-via-bash-606632/)

mindfriction 12-13-2007 06:16 PM

how to copy images recursively via bash
 
Hi guys, was wondering how i would go about recursively copying jpg's and gif's from one directory to another. The cp command doesnt seem to work with file types i.e $ cp -r src/*.jpg dest/, and also i'd like to be able to copy all jpgs and gifs at once.

Thanks!

gilead 12-13-2007 06:26 PM

Do you need to keep the directory structure? If not, the find command will do what you need:
Code:

find /src -regextype posix-extended -regex '.*(jpg|gif)' -exec cp {} /dest \;

mindfriction 12-13-2007 06:48 PM

Thanks for the reply Gilead, looks good but yes i need to keep the structure.

matthewg42 12-13-2007 08:26 PM

You can do it with rsync like this:
Code:

rsync -a --include "*/" --include "*.jpg" --exclude "*" src/ dest/
If you want to add more file types, just include extra --include "*.whatever" before the --exclude.

If rsync is not installed (it should be because it's so über useful), you can do it using cpio in "copy pass" mode, using find to locate the files:
Code:

cd src
find . -depth -name \*.jpg -print0 | cpio --null -pvd /path/to/dest/dir

See the cpio info page for a tutorial and explanation of options etc.

ghostdog74 12-13-2007 09:42 PM

Quote:

Originally Posted by gilead (Post 2989948)
Do you need to keep the directory structure? If not, the find command will do what you need:
Code:

find /src -regextype posix-extended -regex '.*(jpg|gif)' -exec cp {} /dest \;

Code:

# ls -1
test wsdfs
test.ggif
test.jjpg
test1.jpg
test2.gif
# find . -regextype posix-extended -regex '.*(jpg|gif)$' -print
./test1.jpg
./test2.gif
./test.ggif
./test.jjpg
# find . -type f \( -name "*.jpg" -o -name "*.gif" \) -print
./test1.jpg
./test2.gif


gilead 12-13-2007 09:49 PM

And since TMTOWTDI:
Code:

find . -regextype posix-extended -regex '.*\.(jpg|gif)$' -print
./test1.jpg
./test2.gif

matthewg42's rsync solution is the one I'll be using here for a similar problem since it gives the directory structure that was needed by the OP (and now needed by me).

mindfriction 12-13-2007 10:25 PM

Ok thanks gilead,
the rsync seems to work. Mind you the first time i ran i got "rsync reports some files could not be transferred (code 23)".. The second time i ran it (after i deleted what was created) no errors. Weird.


All times are GMT -5. The time now is 08:54 AM.