LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-20-2006, 09:42 AM   #1
ScottReed
Member
 
Registered: Dec 2005
Location: Montana
Distribution: Debian "squeeze"
Posts: 157

Rep: Reputation: 30
BASH script to copy specific files


I'm looking to copy only files of a specified type from a device to a specified location, also preserving the directory structure.

My usual course of action is:

Code:
cp -r -v /mnt/customer_hd /customer_backups/date_lastname
Which works fine 99.9% of the time because people want the entire drive backed up and archived. However, in this case we're looking at a drive that has almost 100gb of data on it, and she only wants the images.

So how would I go about writing a script to parse through the device and grab only the following:

.jpg
.JPG
.tif
.TIF
.bmp
.BMP
.psd
.PSD
.pdd
.PDD
.gif
.GIF

...and then copy them to my destination and preserve the structure?

Thanks,
Scott
 
Old 07-20-2006, 10:28 AM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Off the top of my head:

Code:
cp $(find /src/dir -type f | grep -iE '\.(jpg|tif|bmp|psd|pdd|gif)$' /dest/dir
The only problem is that if there are a large number of files, you might hit the maximum command line length.

The obvious solution is extremely inefficient as it will spawn an instance of cp for each file:

Code:
find /src/dir | grep -iE ... -exec cp {} /dest/dir/$(basename {}) \;
Instead you could try

Code:
find /src/dir | grep -iE ... | perl -wpe 'END{print "/dest/dir"}' | xargs cp
which will append the /dest/dir to the file list. Without this, xargs is no use with cp.
 
Old 07-20-2006, 10:34 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
this script really took me some time so i hope it can help you

Code:
#!/bin/bash

EXTENSIONS="jpg JPG tif TIF bmp BMP psd PSD pdd PDD gif GIF"
SOURCEDIR=/mnt/customer_hd
TARGETDIR=/customer_backups/date_lastname

error() {
   echo "$@"
   exit 1
}

[ "$EXTENSIONS" ] || error "no extensions specified"
[ "${SOURCEDIR}" ] || error "source directory not specified"
[ -d "${SOURCEDIR}" ] || error "source directory not found"
[ "${TARGETDIR}" ] || error "target directory not specified"
[ ! -d $"{TARGETDIR}" ] && mkdir -p ${TARGETDIR}
[ ! -d "${TARGETDIR}" ] || error "target directory creation failed"

pushd "${SOURCEDIR}" || error "can't change directory to source directory"

IFS=$' '
cmd="find -type f | grep"
for a in ${EXTENSIONS}; do cmd="${cmd} -e \.${a}$"; done

IFS=$'\n'
#echo "${cmd}"
for a in $(eval "${cmd}"); do
	cp --parent -v "$a" "${TARGETDIR}"
done

IFS=$' \t\n'
popd
exit 0
sword bash! hehe
 
Old 07-20-2006, 10:51 AM   #4
ScottReed
Member
 
Registered: Dec 2005
Location: Montana
Distribution: Debian "squeeze"
Posts: 157

Original Poster
Rep: Reputation: 30
thanks!

all ideas worked, thanks for the bash script code too.

slight code error in the script... see below:

Code:
[ ! -d $"{TARGETDIR}" ] && mkdir -p ${TARGETDIR}
should be...

Code:
[ ! -d "${TARGETDIR}" ] && mkdir -p ${TARGETDIR}
and...

Code:
[ ! -d "${TARGETDIR}" ] || error "target directory creation failed"
should be...

Code:
[ -d "${TARGETDIR}" ] || error "target directory creation failed"
Thanks again
Scott
 
Old 07-20-2006, 10:58 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
yah no prob

next time you might as well turn it to a really easy script that can do this:
Code:
script.sh /sourcedir /targetdir exta extb extc bla bla
and knowing getopt can help you too. do man bash. it's an enough read.

just a suggestion. prefecting my scripts is what i do most
 
Old 07-20-2006, 11:08 AM   #6
ScottReed
Member
 
Registered: Dec 2005
Location: Montana
Distribution: Debian "squeeze"
Posts: 157

Original Poster
Rep: Reputation: 30
BASH scripting is a lot like NT Script. I have a vast knowledge of NT Script, but it's underneath layers of dust in my brain seeing that I haven't had a need to do it for the past 5 years since I left a job that required it.

Scott
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-07-2013 11:34 PM
copy specific files with directory stucture rincewind Linux - Software 1 06-22-2006 07:58 AM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM
Using bash to copy files from the CD-ROM polyspaston Linux - General 6 01-13-2004 09:06 PM


All times are GMT -5. The time now is 05:56 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