Re: [Hampshire] Shell help - copying files

Top Page
Author: Hugo Mills
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] Shell help - copying files

Reply to this message
gpg: failed to create temporary file '/var/lib/lurker/.#lk0x577ba100.hantslug.org.uk.21648': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Thu Apr 9 11:45:54 2009 BST
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
On Thu, Apr 09, 2009 at 11:27:16AM +0100, Rob Malpass wrote:
> Hi all
>
> I need a bit of help with some shell scripting please:
>
> 1) I need the syntax to recursively copy all files from a tree to one single directory so if I have
>
> /a/x.mp3
> /a/y.mp3
> /b/p.mp3


> then what I want is a.mp3, b.mp3 and p.mp3 to be in one folder -
> wherever I decide to put it e.g. ~/allmp3s. Any files with the same
> filename can be ignored - not overwritten. I know it's going to be
> something to do with cp -R but the rest is foxing me.


$ find /tree/root -type f -print0 | xargs -0 -I{} cp {} ~/allmp3s

That should find all files under /tree/root and copy them into ~/allmp3s.

> 2) Much more complicated (probably) I need then to take my one
> folder with 6000 mp3s in it and compare it to another folder which
> has basically the same files differently organised i.e. into
> different directories. I.e. I want it to search the new structure
> for a.mp3 and only copy it into the new structure if it can prove
> it's not there already. To be honest, I'd settle for a list of
> files that are not in the new structure.


$ find /existing/dir -type f -print0 | sed -e s:^.*/:: | sort >existing-files.txt
$ (cd /new/dir && ls | sort) >new-files.txt
$ join -t $'\0' -a 1 new-files.txt existing-files.txt >files-to-copy.txt

This should give you (1) a list of just the filenames in your
existing structure, (2) a list of just the filenames in the new
directory structure, and (3) a list of all the file names that appear
in the second list but not the first. That covers your "I'd settle
for", I believe.

For the rest, how will you decide where a file goes in the new
structure, given only its name? Regardless, the shell script will look
something like this:

$ cat files-to-copy | while read FILE; do
DESTDIR= # Something -- work out where to put $FILE
cp "/new/dir/$FILE" "$DESTDIR"
done

Hugo.

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
   --- You can get more with a kind word and a 2"x4" than you can ---    
                         with just a kind word.