LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2007, 06:16 PM   #1
mindfriction
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Rep: Reputation: 0
Lightbulb 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!
 
Old 12-13-2007, 06:26 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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 \;

Last edited by gilead; 12-13-2007 at 07:10 PM. Reason: Fixed a typo
 
Old 12-13-2007, 06:48 PM   #3
mindfriction
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for the reply Gilead, looks good but yes i need to keep the structure.
 
Old 12-13-2007, 08:26 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 12-13-2007, 09:42 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by gilead View Post
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
 
Old 12-13-2007, 09:49 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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).
 
Old 12-13-2007, 10:25 PM   #7
mindfriction
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
cpio, find, rsync


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cp to copy directory permissions recursively? SirTristan Linux - Newbie 3 11-25-2007 08:36 AM
bash code to recursively remove EXIF info leosgb Programming 4 10-25-2007 12:31 AM
bash: list recursively directories only kpachopoulos Programming 1 06-18-2007 02:00 AM
trying to recursively copy jpegs DJOtaku Linux - General 10 08-26-2005 01:09 PM
recursively copy php.ini to www directories osio Programming 3 07-01-2005 04:24 AM


All times are GMT -5. The time now is 07:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration