Umm... I think I misunderstood the original question. You want to flatten a directory structure into a single directory while preserving a record of the original path encoded in the filename.
I think this would do what you want:
find "${SRC}" -type f -print | while read F1; do F2=$( echo "${F1}" | tr '/' '_' ) mv "${F1}" "${F2}" done
-Adam
-----Original Message----- From: Adam Thompson [mailto:athompso@athompso.net] Sent: Wednesday, July 07, 2010 2:09 PM To: 'Continuation of Round Table discussion' Subject: RE: [RndTbl] script for file copy
I've had to do this before... there are some tools that'll do it for you, essentially you want the UNIX version of MS-DOS's "UPDATE" command. I would look at "rsync", used in local mode, use something like
$ rsync -a --existing SRCDIR DSTDIR
It is of course possible to do this in a shell script, but it's tricky to handle the recursion correctly unless you use an iterative find/grep series, which gets very expensive in terms of CPU time.
The best non-recursive technique I can think of would be something like: #!/bin/sh SRC="$1" DST="$2" T1=$(mktemp) T2=$(mktemp) find "${SRC}" -type f -print | sed -e "s/^${SRC}//" | sort > "${T1}" find "${DST}" -type f -print | sed -e "s/^${DST}//" | sort > "${T1}" comm -12 "$T1" "$T2" | while read F ; do cp "${SRCDIR}${F}" "${DSTDIR}${F}" done
-Adam
-----Original Message----- From: roundtable-bounces@muug.mb.ca [mailto:roundtable- bounces@muug.mb.ca] On Behalf Of VE4ER / Andy Sent: Wednesday, July 07, 2010 12:10 PM To: MUUG Roundtable Subject: [RndTbl] script for file copy
Can anybody suggest a script to copy files from one directory structure to another changing the filename in the process to include the original folder names, but only if an actual file with extension exists in the bottom child folder :
Go from :
Master Folder ------> Sub Folder1 -----> Sub Sub Folder1 ---> filename.example ------> Sub Folder2 -----> Sub Sub Folder1 ---> filename.example ------> Sub Folder2 -----> Sub Sub Folder2 ---> filename.example
To:
/Var/MainFolder/Master_Sub1_SubSub1_filename.example /Var/MainFolder/Master_Sub2_SubSub1_filename.example /Var/MainFolder/Master_Sub2_SubSub2_filename.example
....etc
Thanks Andy
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
On 2010-07-07 Adam Thompson wrote:
Umm... I think I misunderstood the original question. You want to flatten a directory structure into a single directory while preserving a record of the original path encoded in the filename.
I think this would do what you want:
find "${SRC}" -type f -print | while read F1; do F2=$( echo "${F1}" | tr '/' '_' ) mv "${F1}" "${F2}" done
Has the same problem I ran into (before using perl negative look-behinds) of converting the leading slash to _. (ie if SRC = ".")
So? If SRC=. then the filesnames all become ._whatever. Which is precisely the designed behaviour, in my mind. Admittedly, that's a classic anti-example of the famous DWIM design principle... -Adam
-----Original Message----- From: Trevor Cordes trevor@tecnopolis.ca Sender: roundtable-bounces@muug.mb.ca Date: Wed, 7 Jul 2010 14:53:31 To: roundtable@muug.mb.ca Reply-To: Continuation of Round Table discussion roundtable@muug.mb.ca Subject: Re: [RndTbl] script for file copy
On 2010-07-07 Adam Thompson wrote:
Umm... I think I misunderstood the original question. You want to flatten a directory structure into a single directory while preserving a record of the original path encoded in the filename.
I think this would do what you want:
find "${SRC}" -type f -print | while read F1; do F2=$( echo "${F1}" | tr '/' '_' ) mv "${F1}" "${F2}" done
Has the same problem I ran into (before using perl negative look-behinds) of converting the leading slash to _. (ie if SRC = ".") _______________________________________________ Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable