LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2008, 05:57 AM   #1
lvprabhu
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Rep: Reputation: 0
Truncate from beginning of the file


Hi,

Is there any file utility in Linux to truncate from beginning of the file. I have a growing binary file and I need to delete part of it whenever it reaches pre-specified limit. I need to delete the old data. Please let me know how can I handle it.

Thanks,
lvp
 
Old 06-09-2008, 06:44 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
what do you mean, "truncate from the beginning". Truncating just mean making th file empty, right?

Do you mean to remove data from the beginning of the file, but leave the last [some portion of data] in the file?

Not sure about that. Sounds like you want a special file which acts like a ring buffer.

It's more usual to write your program to accept a signal to re-open the log file, and use this to start a new log file periodically.

I'm not sure how you'd handle it with a binary file. I would expect you'd need to do this in the application itself.

Last edited by matthewg42; 06-09-2008 at 06:45 AM.
 
Old 06-09-2008, 07:43 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
There's a command to truncate a file by keeping the start of it only (and keeping the same inode): truncate.
There's a command to put the start/end of a file into another file: head/tail.

But I don't think there is any command to keep only the end of a file into the same inode…

Yves.
 
Old 06-10-2008, 03:06 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this isnt elegant but try reversing the file , cutting off the end , and then revering it back:

Code:
cat file.txt | rev | cut -b -1024 | rev > file.out
edit:
oh wait i think cut can do it on its own
Code:
cut -b 1025- file.txt
will delete the first kilobyte of the file.

Last edited by schneidz; 06-10-2008 at 03:08 PM.
 
Old 06-10-2008, 06:12 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yet another alternative, if you know how big each field/rec is, is to overwrite that many bytes in binary mode.
 
Old 06-10-2008, 11:37 PM   #6
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Wouldn't you need to start your new file from some kind of record boundary in the original file? Anyway, how about tail -c xx >new file
where xx is the number of bytes you want to keep.
 
Old 06-11-2008, 01:26 AM   #7
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Rep: Reputation: 0
I have been using-- echo | cat fileName | command.....(what ever code) > samefilename. this will overwrite the same file will what ever altercations you made.
 
Old 06-11-2008, 07:51 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
"altercations"; that just has to be a subconscious slip http://dictionary.reference.com/sear...cation&x=0&y=0
 
Old 05-16-2010, 10:39 PM   #9
weebeng
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by schneidz View Post
this isnt elegant but try reversing the file , cutting off the end , and then revering it back:

Code:
cat file.txt | rev | cut -b -1024 | rev > file.out
edit:
oh wait i think cut can do it on its own
Code:
cut -b 1025- file.txt
will delete the first kilobyte of the file.
Hi..I am in Linux.
I'd like to ask I got a file with following content

1234567890
ABCDEFGHIJKLMN

If I used cut command
cut -b 5- file.txt
It will deleted the 1st 4 bytes for 1st & 2nd line.

If I just want to delete the 1st line? How to do that?
Thank you
 
Old 05-16-2010, 11:42 PM   #10
AdnanShaheen
Member
 
Registered: Aug 2009
Distribution: Suse, RedHat, CentOS, Solaris, Windows
Posts: 38

Rep: Reputation: 16
Use tail command
 
1 members found this post helpful.
Old 05-16-2010, 11:53 PM   #11
mac.tieu
Member
 
Registered: Jan 2010
Location: Vietnam
Distribution: Arch
Posts: 65

Rep: Reputation: 22
Quote:
Originally Posted by weebeng View Post
...
If I just want to delete the 1st line? How to do that?
Thank you
Have a look at 'tail'
Code:
$tail -n +2 file.txt > out.txt
MT
 
1 members found this post helpful.
Old 05-17-2010, 02:30 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about raising your own question instead of bringing up one from the past.
 
Old 05-25-2010, 09:43 PM   #13
weebeng
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by mac.tieu View Post
Have a look at 'tail'
Code:
$tail -n +2 file.txt > out.txt
MT
It is work. I used tail with -c to solve my problem. Thank you so much.
I'd like to ask, let said I have few Mbytes data in the file.txt, the 'tail' still can work to redirect output to out.txt (up to how many bytes tail can support)?
 
  


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
how to backup & truncate the log file while the process is running mvenkat_in Programming 13 10-30-2007 07:03 AM
Does K3B automatically truncate file names? General Linux - Software 2 05-08-2007 05:37 PM
Truncate A File Dovetails Linux - Newbie 2 10-22-2005 06:58 PM
truncate a text file in CLI? hottdogg Linux - General 2 09-06-2005 09:32 PM
Zipslack 9.1 long file names truncate jimk Slackware - Installation 5 05-16-2004 06:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 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
Open Source Consulting | Domain Registration