How do I rename list of files with dateformat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I rename list of files with dateformat
# 1  
Old 08-14-2012
How do I rename list of files with dateformat

Hello,

I have a list of files like

Code:
-rw-rw-r-- 1 rails rails  8463005 Jul 27 04:02 find_or.log.3.gz
-rw-rw-r-- 1 rails rails 33786339 Jul 27 04:02 pro.log.10.gz
-rw-rw-r-- 1 rails rails 44815467 Aug  3 04:02 pro.log.9.gz
-rw-rw-r-- 1 rails rails 81562896 Aug  4 04:02 pro.log.8.gz
-rw-rw-r-- 1 rails rails 80073289 Aug  5 04:03 blap.log.7.gz
-rw-rw-r-- 1 rails rails  8867231 Aug  6 04:02 fea_or.log.2.gz
-rw-rw-r-- 1 rails rails 81441609 Aug  6 04:03 pro.log.6.gz
-rw-rw-r-- 1 rails rails 72789502 Aug  7 04:03 pro.log.5.gz

I need to rename these like

pro.log.5.gz to pro.log-20120807.gz
blap.log.7.gz to blap.log-20120805.gz

Basically using date of last entry of each log

Please advise the best way.

Thanks
Ashok
# 2  
Old 08-14-2012
What is the format of your log files? How do you determine the date of the last entry in the log? What do you want to do if the date of the last entry in two log files is the same?
# 3  
Old 08-15-2012
Instead of using last log entry date

Say when I list ls -l
Code:
-rw-rw-r-- 1 rails rails 44815467 Aug  3 04:02 pro.log.9.gz

I need to rename using "Aug 3" like pro.log.20120803.gz

My idea is to use a script like

Code:
for i in `ls *.gz`;

do
DAT=`ls -l $i  | awk '{print $6 $7}'`

Here I need a logic on how to convert $DAT values to equivalent 20120803
into say $NDAT

FN=`ls $i | cut -d'.' -f1`

mv $FN $FN.log-$NDAT.gz
done

Thanks
Ashok
# 4  
Old 08-15-2012
The following script should do what you want. It limits itself to features specified by POSIX, except for its use of the m[] associative array variable. This is a common feature in ksh on most available systems. If your ksh doesn't support associative array variables, it could easily be replaced by a function that would return a two digit string representing the abbreviated month name that would be passed in as an argument.
Code:
#!/bin/ksh
cm=$(date +%m)	# current month as 2 deciml digits
cy=$(date +%Y)	# current year as 4 or more decimal digits
ec=0		# final exit code
ly=$((cy - 1))	# last year as 4 or more decimal digits
typeset -A m	# translation table abbreviated month name -> 2 digit month
m[Jan]=01; m[Feb]=02; m[Mar]=03; m[Apr]=04; m[May]=05; m[Jun]=06
m[Jul]=07; m[Aug]=08; m[Sep]=09; m[Oct]=10; m[Nov]=11; m[Dec]=12
for i in *.log.*.gz
do
	$ According to POSIX, the last command in a pipeline MAY be run
	# in a separate shell execution environment.  It is forced into
	# a separate execution environment here by the $(...) to make it
	# portable to any system.  The $(...) returns the value of $ec
	# it isn't exported to the execution environment of the rest of
	# this script otherwise.  If any errors are reported in the
	# subshell, their diagnostics are written to stderr (file
	# descriptor 2).  No output from the subshell is written to
	# stdout (file descriptor 1).  Note that if the move command is
	# commented out and replaced by an echo, the echo output must be
	# written to stderr!  POSIX allows shells to run the last
	# command in a mult-command pipeline in the current shell
	# execution environment as an extension.
	ls -l "$i" | ec=$( read x x x x x mnth day time file
	if [ "$file" == "" ] # only true if ls failed
	then 	printf "%s: ls failed: \"%s\"\n" "$0" "$i" >&2
		if [ $ec -eq 0 ] # preserve exit code from a failed mv command
		then	ec=100
		fi
		echo $ec
		continue
	fi
	mon=${m[$mnth]}
	if [ "${time%:+([0-9])}" == "$time" ]
	then	# No <colon> in $time means $time is a year instead of a time
		# because the date is in the future or more than six months old.
		year=$time
	elif [ "$mon" -gt $cm ]
	then	# $mon > $cm in the current year, assume it is last year
		year=$ly
	else	year=$cy # otherwise, year is assumed to be current year
	fi
	new="$(printf "%s.log-%d%02d%02d.gz" "${file%%.log.*.gz}" \
	    $year $mon $day)"
	if [ -e "$new" ] # true if $new already exists
	then	# print diagnostic; could concatenate log files instead
		printf "%s: target exists: mv \"%s\" \"%s\" not executed\n" \
		    "$0" "$file" "$new" >&2
		if [ $ec -eq 0 ] # preserve exit code from a failed mv command
		then	ec=101
		fi
		echo $ec
		continue
	fi
	# exactly one of the next two lines must be a comment (# in column 1)
#	mv "$file" "$new"
	echo mv "$file" "$new" >&2
	save=$?
	if [ $save -ne 0 ]
	then	printf "%s: mv failed: mv \"%s\" \"%s\"\n" /
		    "$0" "$file" "$new" >&2
		ec=$save
	fi
	echo $ec)
done
exit $ec

Note that the logic for setting the year to the previous year for entries
where ls -l displays the time of day instead of the year (for entries
less than six months old) hasn't been tested since this code was written
in the 2nd half of the year (and I didn't want to reset my system clock
to test this case).

Last edited by Don Cragun; 08-15-2012 at 07:34 AM..
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-15-2012
Given the command stat exists on your system and it accepts the --printf option,
Code:
stat --printf "%x %n" filename

will output sth. like
Code:
2012-08-14 15:58:24.895007883 +0200 filename

So reassembling the fields with cut, sed, or awk will do what you require.

Last edited by RudiC; 08-15-2012 at 07:56 AM..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-15-2012
Quote:
Originally Posted by RudiC
Given the command stat exists on your system and it accepts the --printf option,
Code:
stat --printf "%x %n" filename

will output sth. like
Code:
2012-08-14 15:58:24.895007883 +0200 filename

So reassembling the fields with cut, sed, or awk will do what you require.
No. On my system (Mac OS X version 10.7.4) the command:
Code:
stat --printf "%x %n" filename

produces the following message on stderr:
Code:
stat: illegal option -- -
usage: stat [-FlLnqrsx] [-f format] [-t timefmt] [file ...]

Some systems will produce the output you specified.
Some systems don't provide a stat utility at all. I try to stick to POSIX standard behavior in the examples I post on this forum. I did use non-standard ksh array variables, but I documented that fact and don't know of any recent system that has a ksh that doesn't support them.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-15-2012
Quote:
Originally Posted by Don Cragun
No.
Code:
stat: illegal option -- -
usage: stat [-FlLnqrsx] [-f format] [-t timefmt] [file ...]

Then you are in the lucky position to tell stat how to output the date using your desired timefmt.
Quote:
I try to stick to POSIX standard behavior in the examples I post on this forum. I did use non-standard ksh array variables, but I documented that fact and don't know of any recent system that has a ksh that doesn't support them.
Accepted. Good point. Still my intention was to give ideas to the requestor on how to achieve the result so he/she can dream up a best fit solution.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

3. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

4. Shell Programming and Scripting

Is better way copy list of multiple files, rename and gzip

Is better way to write the script loop through one by one "Old_File_1: & New_File_1:" to copy 100 files to /staging/test folder then re-name & gzip all those files? I wrote this code below and don't like that much. Thanks I have a control_file under /tmp/test folder like below 100 files and... (10 Replies)
Discussion started by: dotran
10 Replies

5. Shell Programming and Scripting

Datatype,structure and dateformat checking.

I have a sourcefile which contains data as below.I want to check whether datatype,structure and date format looks good as mentioned. Data is delemited by cydila Ç. Source file-Emp.txt snoÇnameÇphonenoÇdeptÇjoineddate 1ÇvivekÇ0861ÇCSEÇ2013-05-29 00:00:00 2ÇdineshÇ123456ÇECEÇ2013-05-29 00:00:00... (8 Replies)
Discussion started by: katakamvivek
8 Replies

6. Shell Programming and Scripting

How to Rename List of files in a directory

How can i rename list of files in a directory? (4 Replies)
Discussion started by: knip
4 Replies

7. Shell Programming and Scripting

Oracle DataBase DateFormat

I am currently writing a script in AIX I connected to the database using SQL *PLUS I need to retrive data of Number of rows inserted today in the table(TABLE_NAME) which has column DATETIME(DDMMYYYY) I tried using select TO_CHAR(sysdate, 'DDMMYYYY') CHRDATE from dual select count (*)... (2 Replies)
Discussion started by: PhAnT0M
2 Replies

8. Shell Programming and Scripting

Need to move and rename a list of files

Hi, I need to do something easy but I can't seem to figure out how to do this. Let's say I have 6 files in the directory below: /ebsbeta_f/flash/EBSUATQB/onlinelog o1_mf_6_55klt7nr_.log o1_mf_3_55klskj4_.log o1_mf_4_55klsrl1_.log o1_mf_5_55klt09p_.log o1_mf_2_55klv1ts_.log... (10 Replies)
Discussion started by: exm
10 Replies

9. Shell Programming and Scripting

please help - script to list and rename

Hi Friend, I have a small script to list all file FFAAAAABBBBB00001 and FFAAAAABBBBB00001.repaired (when I run another script, the orginal file will output another *.repaired file) in my unix directory, and reaname the output file FFAAAAABBBBB00001.repaired back to FFAAAABBBBB00001. However, it... (2 Replies)
Discussion started by: happyv
2 Replies

10. UNIX for Dummies Questions & Answers

copy and rename list of files

Hi all, I am a newbie in writng unix..I am using ksh shell..Does anyone know how to copy a list o files from directory A to directory B with differnt names? i.e in Dir A, I have RPT101.555.TXT RPT102.666.TXT and I want to copy those files to dir B with new naming convention.. in Dir B,... (7 Replies)
Discussion started by: kinmak
7 Replies
Login or Register to Ask a Question