Script to copy log files with today's date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to copy log files with today's date
# 1  
Old 12-30-2008
Script to copy log files with today's date

I am a newbie to scripting.

I need a korn shell script to copy log files of current day to archive folder and rename with current days date stamp.

I would really appreciate your help.

File structure is as follows. Everyday files get overwritten, so I need copy to a archive directory and the renmae with current day.

-rw-r--r-- 1 dmin kin 268609720 Dec 28 04:04 onnector_Arc_01.log
-rw-r--r-- 1 dmin kin 269803781 Dec 29 00:10 dice_Arc_02.log
-rw-r--r-- 1 dmin bin 153013428 Dec 29 08:03 GPS.log
-rw-r--r-- 1 red bin 269804396 Dec 30 08:39 EMC_Arc_01.log
# 2  
Old 12-30-2008
Try logrotate or (if not available / not useable)
Code:
find /logdir -type f -name '*.log' -print | while read FILE ; do
    BASENAME=`basename ${FILE} '.log'`
    cp ${FILE} /archive/${BASENAME}-`date +%Y-%m-%d`.log
    cat > ${FILE} << EOF
EOF # Not indenting this line is intentional
done

# 3  
Old 12-30-2008
Thanks for your reply.

I am getting a syntax error:

"syntax error at line 10 : `<<' unmatched"


Can you please let me know, just to copy today's files only, what should be added?

==============
find /opt/logs/ -type f -name '*.log' -print | while read FILE ; do
BASENAME=`basename ${FILE} '.log'`
cp ${FILE} /tmplog/WBIPRD02/${BASENAME}-`date +%Y-%m-%d`.log
cat > ${FILE} << EOF
EOF # Not indenting this line is intentional
done
========
# 4  
Old 12-30-2008
Quote:
Originally Posted by mdncan
File structure is as follows. Everyday files get overwritten, so I need copy to a archive directory and the renmae with current day.

-rw-r--r-- 1 dmin kin 268609720 Dec 28 04:04 onnector_Arc_01.log
-rw-r--r-- 1 dmin kin 269803781 Dec 29 00:10 dice_Arc_02.log
-rw-r--r-- 1 dmin bin 153013428 Dec 29 08:03 GPS.log
-rw-r--r-- 1 red bin 269804396 Dec 30 08:39 EMC_Arc_01.log

Code:
arcdir=/path/to/archive_directory
today=$( date +%Y-%m-%d )

for file in *.log
do
  mv "$file" "$arcdir/${file%.log}-$today.log
done

#####

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. UNIX for Beginners Questions & Answers

Find and copy .zip file based on today's date

Hi Team, I'm new to unix and i have a requirement to copy or move files from one directory to another based on current date mentioned in the .zip file name. Note that i need to copy only the recent zip file. please help me with the code i tried the code as: #! /usr/bin/sh find... (3 Replies)
Discussion started by: midhun3108
3 Replies

3. Shell Programming and Scripting

Search files with today date and files come anytime in between 10 pm to 1 am

Hi, i want to make script. In a directory everyday( exclude sat and sun) in between 10 pm to 1 am there are 2 files comes and when file comes it will mail us. Format for files is mentioned below. please help me on making this, and also have year end consider and if files come after 12 am it... (6 Replies)
Discussion started by: pallvi_mahajan
6 Replies

4. Shell Programming and Scripting

Move all files except sys date (today) files in Solaris 10

I want to move all files from one directory to another directory excluding today (sysdate files) on daily basis. file name is in pattern file_2013031801, file_2013031802 etc (2 Replies)
Discussion started by: khattak
2 Replies

5. Shell Programming and Scripting

Shell Scripting: Copy Files with Today's date

I was wondering the best way about finding files that were created today and copy them to a directory (grep ?). There can be multiple files for todays date or none. I am looking to copy all of the .lis files for todays date. I may need to modify the filename to include todays date but for the... (4 Replies)
Discussion started by: smkremer
4 Replies

6. UNIX for Dummies Questions & Answers

Script to copy files from a certain date

I need to copy files from a directory that has a lot of files in it. However I only want to copy them from a certain date. My thoughts so far are to use ls -l and to pipe this into awk and print out tokens 6 (month)and 7 (day). $ ls -l -rw-r--r-- 1 prodqual tst 681883 Jun 12... (2 Replies)
Discussion started by: millsy5
2 Replies

7. Shell Programming and Scripting

Help with script to copy/rename files, then delete by date

Hi All, I am new to scripting and am looking for some assistance setting up a script. Basically I need the script to scan a folder for the newest files and make a copy of those files, adding a month to the date stamp. I also need this script to delete the previously copied files to save space.... (4 Replies)
Discussion started by: Lucid13
4 Replies

8. UNIX for Dummies Questions & Answers

Sort files by date, not showing files from today

Hi all, i'm new here in this forum. I really like the helpful answers in this forum. Here a short question. For a script i have to sort files by date and exclude the files of the actual date. Sorting the files by date and preparing the output for awk is done by this line: ls -l... (3 Replies)
Discussion started by: carlosdivega
3 Replies

9. UNIX for Dummies Questions & Answers

script to rename files with current date and copy it.

I have few webservers logs like access.log. which would be growing everyday. what i do everyday is, take the backup of access.log as access.log_(currentdate) and nullify the access.log. So thought of writing a script... but stuck up in middle. My requirement: to take the backup and nullify... (6 Replies)
Discussion started by: logic0
6 Replies
Login or Register to Ask a Question