0

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!

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by eggi
0

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:

e.g.

ls -l *.dat $*| awk `

to

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.