[ale] Shell Scripting -> find files *.<ext> and move them to /backup

Byron A Jeff byron at cc.gatech.edu
Thu May 6 19:00:24 EDT 2004


On Thu, May 06, 2004 at 05:53:09PM -0400, Bridges, Doug wrote:
> Close but no cigar. 
>  
> find / -name "*.jpg" -exec mv {} /mnt/mymachine/ \;
>  

> You will want to replace the first / with the directory where the current
> hard drive is located.  Also note that this will overwrite any files that are
> named the same.

This will work, but it'll be slow and have the overlap problem outlined above.
I spent 5 minutes drawing up a script that will solve the problem:
-------------- movefiles ---------------------------
treeroot=/tmp/target
for i in "$@" ; do
   echo $i
   target=$(dirname "$i")
   mkdir -p "$treeroot/$target"
   cp -a "$i" "$treeroot/$target"
done
----------------------------------------------------

Set treeroot to the target directory. This script will transfer the directory
structure of the source as it transfers file to the target.

Now once you have this script, you can use xargs instead of find's builtin
exec which runs a process for each file, slowing things down:

find . -name '*.jpg' -print0 | xargs -0 movefiles

Will invoke movefiles for every 32768 bytes of filename.

BTW the "" in the script along with the print0 and -0 in the find/xargs
line ensures that spaces transfer properly. You did say this was a Windows
box right?

Hope this helps,

BAJ

> 
> -----Original Message-----
> From: ale-bounces at ale.org [mailto:ale-bounces at ale.org]On Behalf Of Robert Heaven
> Sent: Thursday, May 06, 2004 5:47 PM
> To: Atlanta Linux Enthusiasts
> Subject: Re: [ale] Shell Scripting -> find files *.<ext> and move them to /backup
> 
> 
> I think might work:
> 
> find / -name "*.jpg" -exec mv /mnt/mymachine/ {} \;
> 
> 
> On Thu, 2004-05-06 at 17:06, Nathan J. Underwood wrote: 
> 
> Ok, this should be an easy one, but I can't seem to remember how to do 
> 
> it.  I have a seriously dorked up Win98 workstation (won't boot) that a 
> 
> friend has given me to 'turn into linux'.  Catch is, though, he wants to 
> 
> keep all of his pictures (*.jpg, *.gif), movies (*.mpg), and documents 
> 
> (*.doc, *.xls).  I've done it before (thanks to the help of someone 
> 
> here, if I'm not terribly mistaken), and am currently perusing through 
> 
> the archives trying to find it, but need a script that'll do something 
> 
> like the following:
> 
> 
> 
> find all of the .jpg files, and move them to /mnt/mymachine (samba mount)
> 
> find all of the .gif files, and move them to /mnt/mymachine (samba mount)
> 
> etc.
> 
> 
> 
> The box is currently booted with knoppix, and I've mounted a directory 
> 
> on my box to copy the files to.  Now, I've just gotta find a relatively 
> 
> painless way to move 9,000+ files.
> 
> 
> 
> As a testament to how bad I am at shell scripting, I'm going to put what 
> 
> I have so far (tab #2 in my browser is at linuxdoc.org trying to get all 
> 
> the parameters right for find, exec, and copy).  As always, any help 
> 
> would be greatly appreciated.
> 
> 
> 
> find *.jpg -exec copy $1 /backup
>   _____  
> 
> _______________________________________________
> 
> Ale mailing list
> 
> Ale at ale.org
> 
>  <http://www.ale.org/mailman/listinfo/ale> http://www.ale.org/mailman/listinfo/ale
> 
> 
> 
> 
> _______________________________________________________
> 
> NOTICE:  This e-mail message and all attachments transmitted with it 
> may contain legally privileged and confidential information intended 
> solely for the use of the addressee.  If the reader of this message is not 
> the intended recipient, you are hereby notified that any reading, 
> dissemination, distribution, copying, or other use of this message or its 
> attachments is strictly prohibited.  If you have received this message in 
> error, please notify the sender immediately by telephone 
> (404-881-7000) or by electronic mail (postmaster at alston.com), and 
> delete this message and all copies and backups thereof.  Thank you.
> _______________________________________________________

> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list