Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.