Truncate Log files

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate Log files
# 1  
Old 10-21-2007
Question Truncate Log files

Hi Gurus,

I have several log files running in real time and needs to be truncated 50% or all but has to keep the logs piling up. Any ideas?

For example: /var/adm/messages and others apps log files

Thanks in advance!
# 2  
Old 10-21-2007
tail -b number_of_ blocks_to_save /var/adm/messages > /var/tmp/messages
mv /var/tmp/messages /var/adm/messages
# 3  
Old 06-26-2009
Originally Posted by anonymous1
Hi Gurus,

I have several log files running in real time and needs to be truncated 50% or all but has to keep the logs piling up. Any ideas?

For example: /var/adm/messages and others apps log files

Thanks in advance!
cat /dev/null > $logfile
# 4  
Old 11-11-2009
Data almost same issue

when I do rm or mv .. in this case the process stops writing the log.
now when I am using > xyz.log or cat /dev/null > xyz.log or cp -f /dev/null > xyz.log ..

what happens is the file is truncated to zero ..
du -sh xyz.log and ls -lh xyz.log display zero size but when the process again writes to the log .. du -sh displays the actual size but ls -lh displays the previous size + data written ..
# 5  
Old 11-11-2009
I've done this little script to delete lines from a file without moving it to a temporary file then moving back, it will start deleting line from line 1 to whatever % you choose.
name: truncade
# sysrenan -
# delete a percent of line within a file

if [ "$#" -eq 1 ]

        TOTAL=$(cat $SRC | wc -l)

        if [ "$?" -eq 0 ]
            	echo "This log has $TOTAL lines";
                echo "Enter the % of lines you want to delete:";
                read linestodelete;

                DEL=$(($TOTAL * $linestodelete / 100));
                /bin/sed -i 1,$DEL\d $SRC;
            	echo "Invalid file";
                exit 1;
    	echo "Incorrect number of arguments";
        echo "Usage: ./truncade <logfile>";

hope it helps, let me know
# 6  
Old 11-12-2009
/bin/sed: illegal option -- i

I m getting the above error message ..

m using solaris ...

[JDK1.4.2_15/Sun Microsystems Inc.]
# 7  
Old 11-14-2009
Hey, I don't have a Solaris system to test my script sorry, I usually run RedHat OS.

But, you can try looking at:
sed --help

the purpose of this script was to not use temporary file so it will just delete within the file you mention. But you can try and modify the following:
/bin/sed -i 1,$DEL\d $SRC;

Try using:
/bin/sed 1,$DEL\d $SRC;

/bin/sed -e 1,$DEL\d $SRC;

But again, I can't test since I don't have a solaris system, maybe we can wait to someone else help a little on this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting log files to null writing junk into log files

Redirecting log files to null writing junk into log files. i have log files which created from below command exec <processname> >$logfile but when it reaches some size i am redirecting to null while process is running like >$logfile manually but after that it writes some junk into... (7 Replies)
Discussion started by: greenworld123
7 Replies

2. Shell Programming and Scripting

Script to truncate wtmp files

Hi, Does anyone have a script to truncate the wtmp file. I want to move older entries in the wtmp to a new file and move it out of var/adm and shrink the size. (4 Replies)
Discussion started by: ElizabethPJ
4 Replies

3. Shell Programming and Scripting

Truncate table

Hi In unix able to connect to oracle database and create table ,when rerun ,if table exist ,truncate that table.Any idea how to do that ---- sqlplus -s datadmin/password <<EOF create table xx(col1 number, col2... ); exit; EOF I... (1 Reply)
Discussion started by: mohan705
1 Replies

4. UNIX for Dummies Questions & Answers

How can i truncate filenmes?

I am using FC6 just in case it matters, though i hope it doesn't. If i have a file or some files that i want to truncate the filename of, so that it is only a certain number of characters in length, how would i do that on the command line? Also, just to make it more interesting, say i... (11 Replies)
Discussion started by: Calum
11 Replies

5. Shell Programming and Scripting

ksh - truncate a log in place, sed -i not available captain!

Hi there:) Because of security requirements, It would be much better if I could truncate my logs in place using sed -i (or ?). I cant use the -i option on sed in my environment. Can anyone help a DBA? (5 Replies)
Discussion started by: quigley007
5 Replies

6. UNIX for Advanced & Expert Users

*** Truncate certain field ***

I have a file in which I need to truncate 15th field to have only one character like Put --> P and if i have no value in 15th field, it should be "O" (Other) would really appreciate the reponses, thnx in advance:b: (2 Replies)
Discussion started by: sannmayaz
2 Replies

7. Shell Programming and Scripting

Truncate File contain

I have one file which first line is blank and second line has some data. $cat filename output: 30-MAY-07 I want to store 30-MAY-07 value in one variable. for that I wrote var="`head -2 filename`" It will give that result but I want to truncate the first line which is blank. plz help. (2 Replies)
Discussion started by: rinku
2 Replies

8. Shell Programming and Scripting

How to truncate as filesize?

Hello everybody it's me again. I have a procces that is writing in a 'file1' automatically but i want to truncate 'file1' to a filesize 'x' that mean if the 'file1' size is 'x' i want to delete the first lines while the last lines are being writed, that have sence? in the process are an... (1 Reply)
Discussion started by: Lestat
1 Replies

9. UNIX for Dummies Questions & Answers

truncate wtmp

I have AIX5.1 I have been trying to learn how to truncate the /var/adm/wtmp file. I have seen several things on google actually but don't quite understand. I also searched your forums but couldn't find it. one says this ">/var/adm/wtmp Is that all I do? I have a seperate question also. I was... (1 Reply)
Discussion started by: rocker40
1 Replies

10. UNIX for Dummies Questions & Answers

Truncate what is It?

what does this command do ? as in does this command just make sure everything in the file is executed? or does it flush the file? Actually this is used on a file in a progress database but I believe it is a unix command? (2 Replies)
Discussion started by: rocker40
2 Replies
Login or Register to Ask a Question