Shell wildcard expansion
Tom Christiansen
tchrist at convex.COM
Tue Jun 25 05:54:25 AEST 1991
>From the keyboard of ars at cs.brown.edu (Adam Stauffer):
:
:}I'm looking for a wildcard expansion mechanism, where for example I could
:}do mv *.c *.o.
:}E.g. an expansion where I only get the * part.
:
:There is a really nifty program called "mmv" that allows for all sorts
:of fun expansions to occur. It is available at uunet.uu.net in vol 21
:of the comp.sources.unix archive...
Maybe you didn't look much at rename script that was posted. It
allows things like:
# strip .bak from files
rename 's/\.bak$//' *.bak
# call all fortran files nasty
rename 's/\.f$/.EVIL/' *.f
# make uppercase files be lowercase
rename 'tr/A-Z/a-z/' *
# make lowercase unless the filename starts with "Make"
rename 'tr/A-Z/a-z/ unless /^Make/' *
# change all files in entire system of form foo~ into .#foo
find / -name '*~' -print | rename 's/(.*)~/.#$1/'
# change foo to bar if the user types yes after seeing the name
rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' *
# make xxx44 into 02C-xxx
rename '/^(\D*)(\d+)$/ && $_ = sprintf("%03X-%s", $2, $1)' *
The possibilities are literaly limitless. Ok, the last two are admittedly
a trifle baroque, but you get the idea. The version posted was a bit
elaborate (reads files from stdin, groks -i option, error checking, etc.)
The real guts are just:
#!/usr/bin/perl
$op = shift;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
So if you ever misplace it, you can always rewrite it.
--tom
--
Tom Christiansen tchrist at convex.com convex!tchrist
"So much mail, so little time."
More information about the Comp.unix.shell
mailing list