Use awk to rename non-pattern files

I have some files that don’t have any unique naming sequence or convention.  Well, the only thing common is the extension, say .log.  I would like to rename the files in a sequence based on time stamp or on ls output.  Here is a sample list of files:

auditlog.20130123.log
db.Wed_Jan_23_15_31_47_GMT_2013.log
defaultlog_agent.log
receiver.log
receiver.Wed_Jan_23_12_01_48_GMT_2013.log
runner_os_component.log
runner_plugin_transmitter_queue.log
runnerplugin.Wed_Jan_23_14_33_02_GMT_2013.log
transmitter.log
transmitter_queue_spool_esp_prod.Wed_Jan_23_11_45_09_GMT_2013.log
transmitter.Wed_Jan_23_12_00_26_GMT_2013.log

I would prefer to use awk to rename these files.  Here is one simple method:

ls  *.log | awk -F '{ print "mv "$0"  mylogs_"NR".log "}' >rename_files.sh

This awk statement will create script which will rename all the files as mylogs_1.log, mylogs_2.log….
One can even keep part of the original file and rename the files.

ls  *.log | awk -F. '{ print "mv "$0"  $1_"NR".log "}' >rename_files.sh

I used “.” as the delimiter.  Now, files will look something like original_name_1.log etc.

😉

Leave a comment

Your comment