Looking for some help in renaming files using a shell script.

Suppose I have a directory which contains several files, some of which are *.dat files. If there are 7 *.dat files, I would like to rename all 7 *.dat files to *.01, *.02, ... *.07

Here is what I have thus far:

ls -l *.dat $*| awk `

NF == 9 && /^-/{                    #can someone explain this?  this seems to loop through $
        ++filenum                      #increments to count number of *.dat files
        print filenum, "\t", $9       #prints filenumber and current *.dat filename
        newfile= $9 ".0" filenum   #produces *.dat.01, but want *.01, *.02, etc
        mv $9 $newfile               # seems to do nothing, why?

Any help is greatly appreciated!

Hey There,

Rather than mix it up with awk, since that's doing what you want, you could sneak a sed command in between your initial command and the awk:


ls -l *.dat $*| awk `


ls -l *.dat $*| sed 's/\.dat//'|awk `

I'm sure it's not the most elegant solution, but it should do the trick, since it will strip the ".dat" from the filename and then your awk loop will just add ".01", ".02", etc to the shortened filename instead of the full filename.

Hope the helps some :)

Best wishes,

, Mike