Saturday, December 27, 2008

Renaming multiple files in Linux or unix boxes

I thought it was simple to do this. But I couldn't get rename command to work. I tried several methods found in google, quoted below, but at last it was the for loop and the regexes which came to help.


To rename all the files in a folder to .bak extension

for i in `ls -l | awk '{print $9}'`; do mv $i $i.bak; done

Simpler,

for i in `ls -1A` do mv $i $i.bak ; done

rename 's/\.bak$//' *.bak

Is said to remove the .bak extensions , though it didnt work for me.

To change all upper case file name to lowercase try this

rename 'y/A-Z/a-z/' *

Below command did help me to remove the .bak extensions on a series of files in a folder.

for f in *.bak ; do mv "$f" "${f%.bak}"; done

No comments: