LinuxQuestions.org
Help answer threads with 0 replies.
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 11-22-2012, 05:24 AM   #1
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
how to truncate a file to specific size?


Hi,


I'm having a log which is of 90G size.
I want to shrink the file size to 45G, i tried using the below command:

truncate -s 40G <filename>

but it is deleting the file content from the end(which is not recommended). I want to delete the file content from the beginning.

Note:**As it is a 90G file..opening this file itself takes a lot of time in my dump system**.


Thanks,
Ravi
 
Old 11-22-2012, 05:49 AM   #2
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
Adding.

Quote:
Originally Posted by RaviTezu View Post
I'm having a log which is of 90G size.
I mean a single log file.
 
Old 11-22-2012, 05:53 AM   #3
Kevinpieterson
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
i dont see any command other than this to truncate to a specified size.
 
Old 11-22-2012, 05:54 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
I'm not aware of an elegant way, but this should work:
Code:
$ tac filename > filename.tmp
$ truncate -s 40G filename.tmp
$ tac filename.tmp > filename
$ rm filename.tmp
This does assume that you have enough disk space....
 
Old 11-22-2012, 06:04 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
won't dd do it?
 
Old 11-22-2012, 06:11 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
dd if=oldfile of=newfile ibs=1 skip=5
Copies oldfile to newfile, leaving off the first 5 bytes.

ibs can be set in various units--following is snipped from the dd man page:
Quote:
BLOCKS and BYTES may be followed by the following multiplicative suf‐
fixes: c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M
=1024*1024, xM =M GB =1000*1000*1000, G =1024*1024*1024, and so on for
T, P, E, Z, Y.
 
Old 11-22-2012, 06:13 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by pixellany View Post
won't dd do it?
That was my first thought, but you need to determine the starting position (the skip=BLOCKS option). You would need to calculate this by hand (assuming that the infile can have various sizes).
 
Old 11-22-2012, 06:19 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
taking OP's original statement literally, we would do:
dd if=oldfile of=newfile ibs=1G skip=45

So---Ravi*; what IS the exact problem statement??
 
Old 11-22-2012, 06:45 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Ok, since I started to learn Perl today, and I am eager to practice, how about something like this?

Code:
#!/usr/bin/perl

use warnings;
use strict;

my $size=45_000_000_000;
open FH, $ARGV[0];
seek(FH, -$size, 2);
while ($_ = <FH>) { print $_; }
Add any modifications and error checks as necessary.
 
Old 11-22-2012, 07:11 AM   #10
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Original Poster
Rep: Reputation: 24
Thanks everyone
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] truncate to end of line after a specific string threezerous Linux - Newbie 2 09-21-2012 04:49 PM
New open file size limit is getting reflected in the specific user srajeshkumar Linux - Server 6 05-18-2011 08:32 AM
scp truncate text file busy - copying file is not a running binary jetberrocal Linux - Server 3 06-24-2010 03:56 PM
how to create file with a specific file size markraem Linux - Software 7 08-22-2007 06:38 PM
How to limit the size of a specific file ericthyred Linux - Software 1 04-15-2004 11:13 AM


All times are GMT -5. The time now is 08:17 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration