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
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
Great, but he will still want the slashes converted to underscores, will he not? I hope he doesn't have any underscores already in any directory or file names, else it will be non-reversible (and a little confusing).
Kevin
On Wed, Jul 7, 2010 at 2:09 PM, Adam Thompson athompso@athompso.net wrote:
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
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
On 2010-07-07 VE4ER / Andy wrote:
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
Use my "rename" script, evolved from the Programming Perl O'Reilly book example of the same name:
cat /usr/local/script/rename #!/usr/bin/perl -w # From page 313 of Programming Perl # Renames a group of files, ie *.c -> *.bak# Usage: rename perlexpr[files]
($op=shift) || die "Usage: rename perlexpr [filenames]\n"; if (!@ARGV){ @ARGV=<STDIN>; chop(@ARGV); } for (@ARGV){ $was=$_; eval $op; die $@ if $@; warn("file does not exist: $was\n"),next if ! -e $was; rename($was,$_) unless $was eq $_; print "$was -> $_".($was eq $_ and ' the same, no rename done')."\n"; }
and then call it like:
find . -type f -name "*.example" | rename 's#(?<!^.)/#_#g; s#^#/tmp/destdir/#'
I suppose that's the tricky bit. Comment out the "rename" call in the perl script to test it out first (outputs changes without making them).
That perl rename script is the handiest thing to keep around...