Rename the Linux log file to the rotation date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the Linux log file to the rotation date
# 1  
Old 12-15-2014
Rename the Linux log file to the rotation date

Hi all,

could any provide me a solution for the below requirement.

I have two files namely abc.log.1 and abc.log.2
The above files have time stamp as Dec 08 and Dec 09

I need to rename the files as abc.log.1_20141208 and abc.log.2_20141209 and move to another bkp directory.

Thanks in advance

/Bhaskar T
# 2  
Old 12-15-2014
How about
Code:
echo mv output.txt $(printf "/other/bcp/directory/%s.%(%Y%m%d)T" $(stat -c"%n %Y" output.txt ))
mv output.txt /other/bcp/directory/output.txt.20141214

# 3  
Old 12-15-2014
If it's only 2 files, why not doing that manually?

Variant 1:
Code:
mv abc.log.1 abc.log.1_20141208
mv abc.log.2 abc.log.2_20141209
mv abc.log.1_20141208 abc.log.2_20141209 /path/to/another/bkp/dir

Variant 2 (rename + move at the same time):
Code:
mv abc.log.1 /path/to/another/bkp/dir/abc.log.1_20141208
mv abc.log.2 /path/to/another/bkp/dir/abc.log.2_20141209

Anyways, below you will find some code to do that automatically (utilizing variant 2).

With help from stat from GNU coreutils:
Code:
ls *.log.[0-9]* | while read ofile; do
 nfile=$(stat -c '%y' "$ofile")
 nfile=${nfile%% *}
 nfile=${nfile//-}
 echo mv "$ofile" /path/to/another/bkp/dir/"$ofile"_"$nfile"
done

With help from find from GNU findutils:
Code:
find . -type f -name '*.log.[0-9]*' -printf 'mv "%f" "/path/to/another/bkp/dir/%f_%TY%Tm%Td"\n'

If you run the first approach and the output looks reasonable, remove echo from the code to perform the actual renaming/moving process.

If you run the second approach and the output looks reasonable, add | sh at the end of that long find command to perform the actual renaming/moving process.

Important: No matter which solution you might use, make sure you change /path/to/another/bkp/dir/ accordingly to reflect the your real backup directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH function to rename file to last mondays date

I'm trying to create a bash function that will allow me to rename a file name emails.txt to last monday's date renem () { d=`date -d last-monday` mv ~/emails.txt ~/myemails/$d } but i en up wit this error any suggestion mv: target `2014' is not a directory (3 Replies)
Discussion started by: robshal
3 Replies

2. UNIX for Advanced & Expert Users

Script to rename file that was generated today and which starts with date

hello, can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file. here is example. # find . -type f -mtime -1 ./20130529_4995733057260357019.xml # this finename should be renamed to this format.... (6 Replies)
Discussion started by: bobby320
6 Replies

3. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

4. Shell Programming and Scripting

If(Condition) Rename a file with (Date+Time) Stamp

Hi! Please see our current script: #!/usr/bin/ksh if (egrep "This string is found in the log" /a01/bpm.log) then mailx -s "Error from log" me@email.com, him@email.com </a01/bpm.log fi To the above existing script, we need to add the following change: 1) After finding the string,... (7 Replies)
Discussion started by: atechcorp
7 Replies

5. Shell Programming and Scripting

Check File Size For Log Rotation

Hi Can anyone assist me, I am trying to compate the size of a logfile to a maximum size 1000 and delete if exceeds the limit. The problem I am getting is the command not found for the line if ( $LOGNAME_SIZE >= $MAXSIZE); then Appreciate your response. Script: LOGDIR="/home/... (6 Replies)
Discussion started by: sureshcisco
6 Replies

6. Shell Programming and Scripting

Log file rotation

Hi I am trying to create a simple function that implements log rotation rotatelogs () { file_name=$1 archive_dir=$2 retention_period=$3 find $archive_dir -name $file_name -mtime +$retention_period -exec rm -f {} \; } Issue i am facing is the file_name would be something like my... (3 Replies)
Discussion started by: xiamin
3 Replies

7. Shell Programming and Scripting

Rename File Based on Created Date

I am trying to rename files based on the created/born date of the file. I Have a total of 4000 files that i am trying to do this with and would like it to be log_yyyymmddhh.gz right now the files are maillog.???.gz. Can anyone point me in the right direction of how to get this done via scipt? ... (4 Replies)
Discussion started by: Paulb
4 Replies

8. AIX

audit.log file rotation

Hi guys, I've googled this quite a bit, and tried searching on these forums, but haven't found a solution to my problem. I wanted to inquire about AIX's audit subsystem - more specifically, how to rotate its log file. So far I've been able to find how to rotate AIX syslog log files, and I... (2 Replies)
Discussion started by: w1r3d
2 Replies

9. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

10. Shell Programming and Scripting

Rename a file to have Date and Line Count

Hi, I am trying to come with one line command which will rename a file to have Date (MMDDYYYY) and Line Count in the file. Any lead will be appreciated rgds bmk (2 Replies)
Discussion started by: bmkux
2 Replies
Login or Register to Ask a Question