Shell wildcard expansion

Paul Falstad pfalstad at phoenix.princeton.edu
Tue Jun 25 17:34:15 AEST 1991


Warning!  zsh plug ahead.  Sensitive readers hit 'n'.

$ /usr/local/bin/zsh
% rename () { eval 'shift; for i; mv $i $i:'$1 }

tchrist at convex.COM (Tom Christiansen) wrote:

># strip .bak from files
>    rename 's/\.bak$//' *.bak

% rename s/.bak// *.bak

># call all fortran files nasty
>    rename 's/\.f$/.EVIL/' *.f

% rename s/.f/.EVIL/ *.f

># make uppercase files be lowercase
>    rename 'tr/A-Z/a-z/' *

% rename l *

># make lowercase unless the filename starts with "Make"
>    rename 'tr/A-Z/a-z/ unless /^Make/' *

% set -o extendedglob
% rename l ^Make*

># change all files in entire system of form foo~ into .#foo
>    find / -name '*~' -print | rename 's/(.*)~/.#$1/' 

Afraid my rename can't tackle this one.  Try this though:

% for i in /..../*~; mv $i ".#${i%~}"

># change foo to bar if the user types yes after seeing the name
>    rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'  *

% for i in *; if read "j?$i: "; [[ $j = y* ]]; then mv $i $i:s/foo/bar/; fi

># make xxx44 into 02C-xxx  
>    rename '/^(\D*)(\d+)$/ && $_ = sprintf("%03X-%s", $2, $1)' *

Oh well.

>So if you ever misplace it, you can always rewrite it.

Since my rename function is so simple, I don't bother to define it;
I usually just type out the zsh commands to do what I want:

% for i in *.bak; mv $i $i:s/.bak//

However, if you have a filename like "foo.bak.bar.bak",
then $i:s/.bak// will give "foo.bar.bak" instead of
the desired "foo.bak.bar"; ${i%.bak} does what you want.

--
Paul Falstad                     | 10 PRINT "PRINCETON CS"
pfalstad at phoenix.princeton.edu   | 20 GOTO 10



More information about the Comp.unix.shell mailing list