Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Truncating file based on date Post 302478346 by methyl on Tuesday 7th of December 2010 06:03:28 PM
Old 12-07-2010
Create a cron which runs daily at midnight and rotates the logs.
Note to "Parm0073" (as explained by "achenle") we never issue "mv logfile" (or "rm logfile") because this will create ghost files if the log is open by a process.

This is the longhand method. It should work whether or not the logfile exists each day and will correctly deal with a logfile which is open by a process.
If you currently have a large logfile, the issue will take 8 days to roll off that logfile. Be patient.
Code:
if [ -f logfile-7 ]
then
    rm logfile-7
fi
if [ -f logfile-6 ]
then
    mv logfile-6 logfile-7
fi
if [ -f logfile-5 ]
then
    mv logfile-5 logfile-6
fi
if [ -f logfile-4 ]
then
    mv logfile-4 logfile-5
fi
if [ -f logfile-3 ]
then
    mv logfile-3 logfile-4
fi
if [ -f logfile-2 ]
then
    mv logfile-2 logfile-3
fi
if [ -f logfile-1 ]
then
    mv logfile-1 logfile-2
fi
cp -p logfile logfile-1
>logfile


Just for interest. Personally I rotate system logs at midday. Though you get two half-days in each log it gives you chance to read what happened after you went home at some time before lunch the next day.

Last edited by methyl; 12-07-2010 at 07:17 PM.. Reason: Assorsted corrections and acknowledgements.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Delete File Based On Date

Hi Xpert Out There I have a lots of file in this path : -rw-r----- 1 oracle dba 3954176 Dec 21 2006 1_2008.dbf -rw-r----- 1 oracle dba 887808 Dec 21 2006 1_2009.dbf -rw-r----- 1 oracle dba 143872 Dec 21 2006 1_2010.dbf -rw-r----- 1 oracle dba ... (3 Replies)
Discussion started by: adzuanamir
3 Replies

2. Shell Programming and Scripting

Truncating FILE data BASED ON A PATTERN

HI I HAVE A PROBLEM,MY SOURCE FILE IS OF PATTERN S1,E-Certified,29,29,2.7,Certified,4,3,2.7,,0,0,0 S2,Certified,4,3,2.7,,0,0,0,,0 S3,E-Certified,29,29,2.7,,0,0,0 S4,,0,0,0,,0,0,0,,0,0,0,,0,0,0 AND THE EXPECTED OUTPUT IS S1,E-Certified,29,29,2.7 S1,Certified,4,3,2.7... (1 Reply)
Discussion started by: pkumar3
1 Replies

3. Shell Programming and Scripting

Split the file based on date value

Hi frnds, I have flat file as . Say : output-file1.txt Output-file2.txt (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

4. Shell Programming and Scripting

creating a file name based on date

I need to automate a weekly process of piping a directory list to a csv file. Normally I do ls -l > files_04182010.csv (04182010 being the date..) Can someome show me how I would script this, so that when the script is ran it grabs the current date and formats it and allows me to use that... (8 Replies)
Discussion started by: jeffs42885
8 Replies

5. Shell Programming and Scripting

Truncating a mail file

Hi, I have a Unix mail file that I need to truncate, based on the date of the messages. For those not familiar with the format, it is a single file for each user, with the first line of the mail message looking like the following: From user@sitename.com Thu Apr 21 05:40:33 2011 Each... (3 Replies)
Discussion started by: joed
3 Replies

6. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

7. Shell Programming and Scripting

File Move based on date

how to move file based on date in linux (1 Reply)
Discussion started by: syedzoniac
1 Replies

8. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

9. Post Here to Contact Site Administrators and Moderators

Read file name based on date

Hi, I have file name as Example extract_ces_v3_p044444rlt_20160514045705.txt.pgp extract_ces_v3_p044444rlt_20160614049705.txt.pgp extract_ces_v3_p044444rlt_20160714046705.txt.pgp I have to read file name based on date(i.e) files with same date and copy to another directory in shell script.... (3 Replies)
Discussion started by: caba_jones
3 Replies

10. Shell Programming and Scripting

To take file based on date passed

Hi Guys, I have certain files in my directory which gets appended with dates something like this T1_aug17.txt T1_Aug17.txt T1_Sep17.txt config.txt T1 T2 my code: curr_date=`date -d "$date" +%Y-%m-%d` path=mydir for file in `cat config.txt` do final_file=$(ls $path/ | grep -i... (12 Replies)
Discussion started by: rohit_shinez
12 Replies
explain_truncate_or_die(3)				     Library Functions Manual					explain_truncate_or_die(3)

NAME
explain_truncate_or_die - truncate a file and report errors SYNOPSIS
#include <libexplain/truncate.h> void explain_truncate_or_die(const char *pathname, long long length); DESCRIPTION
The explain_truncate_or_die function is used to call the truncate(2) system call. On failure an explanation will be printed to stderr, obtained from explain_truncate(3), and then the process terminates by calling exit(EXIT_FAILURE). This function is intended to be used in a fashion similar to the following example: explain_truncate_or_die(pathname, length); pathname The pathname, exactly as to be passed to the truncate(2) system call. length The length, exactly as to be passed to the truncate(2) system call. Returns: This function only returns on success. On failure, prints an explanation and exits. SEE ALSO
truncate(2) truncate a file to a specified length explain_truncate(3) explain truncate(2) errors exit(2) terminate the calling process COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_truncate_or_die(3)
All times are GMT -4. The time now is 03:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy