Sunday, June 29, 2008

Bulk replace file extension in Linux

Recently need to do a mass rename of files extension to another in Linux environment. I rename them one by one using "mv" command, which is super slow if talking about 200 over files. Thanks to Sunny who provide me the following solution:

Replace all '.txt' file extension to '.log' extension and display "rename" command in console:
ls *.txt |awk ' {s=substr($1, 1, length($1) - 4); print "mv " s ".txt " s ".log"}'

Executable mode of bulk replace file extension from '.txt' to '.log':
ls *.txt |awk ' {s=substr($0, 1, length($0) - 4); system("mv " s ".txt " s ".log")}'

This save me lots of time, probably yours too. :)

Thanks again, Sunny!

3 comments:

Anonymous said...

Thanks!

Anonymous said...

thank u :)

Juan said...

Shell script recipe of the day that saved the time :) Thank you.