Quote:
>> >I want to rename all files of a directory
>> >Example :
>> >amt -> Amt
>> >test -> Test
>> >prgm -> Prgm
>> >.xinitrc.linux -> .xinitrc.linux
>> >List -> List
>> >(i only want to change the first caracter)
>> >how do i do that?
>> /bin/ls \
>> | awk '{new = toupper(substr($0,1,1)) substr($0,2);
>> if($0 != new) print "mv", $0, new }' \
>> | /bin/sh
>> NB: this will break if file names have spaces, quotes or shell
>> metacharacters in them. Fixing this is an exercise for the reader...
>> -- don
>You could avoid the extra invokation of sh using system():
> /bin/ls | awk ' \
> (new = toupper(substr($0,1,1)) substr($0,2)) != $0 { \
> system("mv \"" $0 "\" \"" new "\"") \
> } \
> '
>(Or does system() start a new sh, anyway?) Well, the Gnu AWK docs say it is
>more efficient to pipe to a shell within AWK, thusly:
It does. I was trying to avoid lots of /bin/sh invocations by doing it
just once.
Quote:
> /bin/ls | awk ' \
> (new = toupper(substr($0,1,1)) substr($0,2)) != $0 { \
> print "mv \"" $0 "\" \"" new "\"" | "/bin/sh" \
> } \
> '
Yep. There's not a lot in it between doing the pipe to /bin/sh inside
or outside the awk script, except that doing it outside saves typing
a couple of double quotes. It's also a little easier to test -- you run
the script without the pipe on the end to check the output, then add the
pipe to the end of the command line when you;re ready to run for real.
Quote:
>(BTW, this fixes spaces in names, but not the other evil stuff.)
my usual "safe-ish" method is:
#
# Quote a shell param -- wrap in single quotes, wrap embedded
# single quotes in closing single quote, double quotes to enclose
# an actual single quote, then reopening the single quote
#
function shellquote(str) {
gsub(/'/, "'\"'\"'", str);
return "'" str "'"
}
(new = toupper(substr($0,1,1)) substr($0,2)) != $0 {
print "mv", shellquote($0), shellquote(new) | "/bin/sh"
}
which quotes a parameter to be passed to the shell pretty well. But
that's pretty ugly to code off the command line where single quotes
have to be quoted.
-- don