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...