LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-12-2005, 02:13 PM   #1
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Rep: Reputation: 31
trying to recursively copy jpegs


I want to copy all of the jpgs in dir1 and all dir1 subdirectories into dir2.

The command I tried to use is

cp -r dir1/*.jpg dir2

However this only copies jpgs from dir1 into dir2 and doesn't go into dir1's subdirectories. Any ideas?

Thanks,
 
Old 08-12-2005, 02:29 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Use find and xargs to construct the copy.
Code:
find dir1 -name '*.jpg' -print0 | xargs -i cp "{}" dir2
 
Old 08-12-2005, 03:23 PM   #3
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
Thanks a lot! That was a line in a little bash script I was writing myself. I credited you in the release notes. Thanks again.

Code:
#!/bin/bash

############################################################
#
# By Eric Mesa
#
# My second base script
# This script licensed under GNU GPL v2 www.linux.org/gnu.html
#
# This script is for copying all of the jpegs in a directory
# to any other directory the user wishes.
#
# I wrote this because I have many folders which contain
# TIFFs, RAW, and JPEG files.  I just wanted to copy the
# JPEGs over - to make a CD or DVD, for example.
# Or to make a backup of the final product of editing
#
# The script will descend through all subdirectories
# from the initial directory given
#
# Version History
# 0.02 - 12 Aug 2005
# -thanks to David (aka Matir) on Linuxquestions.org
#  who helped me figure out how to recursively copy
#  only jpeg files
#
# 0.01 - 12 Aug 2005
# -Created the script
############################################################

#ask the user for the directory they with to copy from
echo "Enter the full path of the directory you wish to copy JPEGS from"

#read in the answer
read jpegdirectory

#Ask which directory to copy to
echo "Enter the directory to copy to"

#read in answer
read finaldirectory

###############################################################
# At this point in time I'm not good enough at bash scripting
# to actually tell the user if the directories don't exist
# or to ask them if this is correct or not, but this lays the
# groundwork.  In any case, it's always good to let the 
# user know what's about to happen, especially when
# copying files!
#
# However, an unexpected consequence of using Dave's
# find command will cause it to fail if it can't
# find the directory.  What I'd like to do in the
# next version is ask the user if this is correct and
# exit on a "n" or "no"
##############################################################

echo "About to copy JPEGs from " $jpegdirectory " to " $finaldirectory

###########################################################
# 
# For the following cp command I used the options
# i to keep you from accidentally overwriting files
# v to put the command in verbose mode (names all files)
# u to only try to overwrite the file if the jpegdirectory
#   has a newer file than finaldirectory
#
# Special thanks to David (aka Matir) on Linuxquestions.org
# For pointing me in the right direction.  Instead of just
# using a copy, we will use the find command and pipe
# that through xargs to copy - like so:
###########################################################

find $jpegdirectory -name "*.jpg" -print0 | xargs -i cp -rvu "{}" $finaldirectory

#the command that didn't work
#cp -irvu "$jpegdirectory"/*.jpg "$finaldirectory"

##########################################################
# Now let's let the user know that the process completed
# Yeah, I know it's against UNIX philosophy of nothing
# reported if the command completed ok, but it's not
# MY philosophy
#########################################################

echo "Copy Process Complete.  You should now only have jpegs copied over from " $jpegdirectory
 
Old 08-12-2005, 03:27 PM   #4
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
Ah, but one thing I just realized, directory structure on the other side is not preserved. Is there a way to incorporate that into the script?

Thanks!
 
Old 08-12-2005, 03:35 PM   #5
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
it looks like maybe I can combine

http://www.linuxquestions.org/questi...hreadid=352079

with what you told me. I'll have to take a look and see if it's compatible.
 
Old 08-12-2005, 03:45 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Also, for cleanliness, (and I forgot this detail) xargs should also receive the -0 (that's the number 0) option. This just tells it only to split on nulls, which will help with any files that might have a space in the name.

As for the copying problem, check out the --parents option to cp.

And thanks for the credit in there.

If you ever copy files from, say, a digital camera, which has a habit of capitalizing names, you might want to use -iname instead of -name in find: it works in a case-insensitive manner.
 
Old 08-26-2005, 09:00 AM   #7
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
When I added the 0 to the xargs command I get the error -

cp: cannot stat '{}': No such file or directory

Code:
find $jpegdirectory -iname "*.jpg" -print0 | xargs -i0 cp --parents -rvu "{}" $finaldirectory
 
Old 08-26-2005, 09:06 AM   #8
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
Also, if I try the same line, but without the 0, parents doesn't seem to be giving me what I want. It copies all the parent directories of the jpg it finds. What I want it to do is find all the children directories of $jpegdirectory and preserver those.
 
Old 08-26-2005, 09:35 AM   #9
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
ok, the parents option seems to be the only way to do this, so I just need to get creative in which directory the program starts from, or something like that. I'm still thinking it through.

I am still getting the errors on xargs -i0 (a zero)

cp: cannot stat '{}': No such file or directory
 
Old 08-26-2005, 10:47 AM   #10
DJOtaku
Member
 
Registered: Oct 2004
Location: USA
Distribution: Fedora 20; Arch; Kubuntu; Debian
Posts: 711

Original Poster
Rep: Reputation: 31
Ok, I'll post new code later, but it seems to be working just fine. Even without the 0 in the xargs it copies over names with spaces. Of course, I also took the 0 off of print.

Again, thanks for all your help, I intend to try this out over the next few weeks on my big project - copying thousands of jpegs from my windows computer over to my Linux external hdd.
 
Old 08-26-2005, 02:09 PM   #11
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
the 0 should precede the -i, or xargs will think that '0' is an argument to -i. Try 'xargs -0 -i CMD'. If you want, of course.
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recursively copy php.ini to www directories osio Programming 3 07-01-2005 05:24 AM
Cannot read a CD of jpegs in Mandrake 10! SpartacusJones Linux - Newbie 22 09-04-2004 11:09 PM
Trying to mount a CD with .jpegs on it soujrnr Linux - Software 2 08-13-2004 10:11 PM
Mandrake 9.2, mozilla and jpegs f0rmula Linux - Software 1 02-15-2004 09:28 AM
Cannot delete locked JPEGs Riichard Linux - Software 10 11-27-2003 03:41 PM


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

Main Menu
Advertisement
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