LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2023, 07:18 PM   #1
james000
Member
 
Registered: Sep 2018
Posts: 143

Rep: Reputation: 2
Can I delete first few lines from a running log file?


Hello,
This is RedHat 7 and I have a big log file of 12gb. Logs are constantly writing to it, so I can't rename file. This filesystem have only 4 gb space left, so I can't use logrotate also, for rotating 12gb file.
One solution I can think is, delete first half of lines (approx 32,231,354) and that would make this file size to 6gb.
Can someone suggest, how can I truncate older lines from this log file?
Code:
[root@linux-7 ~]# cat /var/log/drtmessages | wc -l
64462709
[root@linux-7 ~]#
Thanks
 
Old 09-29-2023, 08:16 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,328
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
I think you'll find this article quite helpful.

Do you have logrotate installed? If not, I'd look at the first.
 
1 members found this post helpful.
Old 09-29-2023, 08:30 PM   #3
james000
Member
 
Registered: Sep 2018
Posts: 143

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by frankbell View Post
I think you'll find this article quite helpful.

Do you have logrotate installed? If not, I'd look at the first.
logrotate will not work, because file size is 12gb and free space in this file-system is only 4gb. Not enough space to complete operation.
I don't want to truncate whole file, but only older half.
 
Old 09-29-2023, 08:42 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Can you USB-attach an external disk drive to this system, to give you the space that you need? Or install another internal one, then use LVM to add it to the storage pool to give you the additional space that you need?
 
Old 09-29-2023, 08:44 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,328
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
Off the top of my head, I don't see any reason why you couldn't edit log file in the way you suggest, but I have no personal knowledge or experience doing so, so I hope that someone who knows more than I do joins this thread.

Sorry I can't help more.
 
Old 09-30-2023, 12:06 AM   #6
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,579
Blog Entries: 19

Rep: Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453
Get yourself a SystemRescue image on CD or usb stick. Boot with it, mount your normal root partition and do your edit. Then, when you reboot your main system, install and configure logrotate.
 
Old 09-30-2023, 01:56 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You cannot delete the initial part of a text file without creating another file, needing extra space.
Even compressing it would temporarily need space for both uncompressed and compressed files.
A truncation from the end is possible, but not desired here.

I would try another filesystem.
Say a
Code:
df /opt /var/log
shows that /opt is a different file system and has 7G avail. Then do
Code:
sed '1,32231354 d' /var/log/drtmessages > /opt/drtmessages.tmp
Directly copying this back is not recommended: concurrent writes can cause data corruption.
Better truncate the busy file:
Code:
: > /var/log/drtmessages
and check after a pause that its size is reduced.
Then move the tmp file back, but to a new name:
Code:
mv /opt/drtmessages.tmp /var/log/drtmessages.old
And finally configure logrotate!
 
1 members found this post helpful.
Old 10-01-2023, 12:24 AM   #8
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 779

Rep: Reputation: 246Reputation: 246Reputation: 246
I'd just whack the whole thing. It can't be that important if you're not tending it properly. From there on out, tend it properly with logrotate, etc.
Code:
cat /dev/null > /var/log/drtmessages
 
Old 10-01-2023, 08:12 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,603

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by james000 View Post
This filesystem have only 4 gb space left, so I can't use logrotate also, for rotating 12gb file.
You can use logrotate, since it can be configured to compress the old version, and can reduce log files by 80-90%, so - depending on what's in the log - could easily produce a file under 2gb.

Also logrotate defaults to same directory, but can be configured to put files elsewhere - i.e. a different disk, or perhaps to RAM - again, assuming the contents of the logs are actually needed.


Though...
Quote:
Originally Posted by james000 View Post
This is RedHat 7
Red Hat 7 is over twenty years old. You presumably mean RHEL 7, and if so why aren't you making use of the support contract you're paying for?

(Also, given RHEL 7 is EOL in under 9 months time, there should be a plan/process in place for migrating away from it...)

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] chroot: Can't see first few lines when using vi aikempshall Slackware 11 01-26-2020 11:09 AM
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 12:30 PM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 02:58 PM
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 08:55 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration