rename 
Author Message
 rename

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?

Thanks



Mon, 02 May 2005 09:11:48 GMT  
 rename

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



Mon, 02 May 2005 09:55:50 GMT  
 rename

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:

    /bin/ls | awk ' \
        (new = toupper(substr($0,1,1)) substr($0,2)) != $0 { \
            print "mv \"" $0 "\" \"" new "\"" | "/bin/sh" \
        } \
    '

(BTW, this fixes spaces in names, but not th other evil stuff.)

- Dan



Mon, 02 May 2005 15:13:29 GMT  
 rename

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



Mon, 02 May 2005 17:20:56 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. SmallDictionary to RBSmallDictionary renaming

2. net.lang.apl is being renamed comp.lang.apl

3. CHB Rename Temporary

4. Renaming datasets

5. Class Rename bug

6. methods move with the renamed class

7. Reference views and renamed classes

8. renaming methods

9. VW: renaming a method

10. Renaming a file

11. Win2000 Gawk Renaming a file with system() command, command line to long

12. Suggestion: rename OrderedCollection>>removeAllSuchThat:

 

 
Powered by phpBB® Forum Software