LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-09-2008, 06:20 AM   #1
bobbera
Member
 
Registered: Jun 2007
Posts: 39

Rep: Reputation: 0
Truncating a file in linux


I have a large file which has gotten quite large & want to truncate. I used the command:

> file

After this ls -l shows size 0. Once the process that's appending to the file writes again then ls -l shows the large size again. This worked in HP. Any idea how to accomplish this in Linux?
 
Old 09-09-2008, 06:25 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Exactly the same should work.

EDIT: I re-read your post and I am not sure I understood you ok. Are you emptying the file while it's still opened by the other application?

In that case it's totally normal that the file size is restored.

Last edited by i92guboj; 09-09-2008 at 06:34 AM.
 
Old 09-09-2008, 08:51 AM   #3
otheus
LQ Newbie
 
Registered: Jun 2006
Location: Austria
Distribution: RHEL AS 4
Posts: 26

Rep: Reputation: 16
Quote:
Originally Posted by bobbera View Post
I have a large file which has gotten quite large & want to truncate. I used the command:

> file

After this ls -l shows size 0. Once the process that's appending to the file writes again then ls -l shows the large size again. This worked in HP. Any idea how to accomplish this in Linux?
In short, I would not count on this behavior in Linux.

That sounds like a bug in the HP/UX, but it might also depend on the shell. See, the redirect directive you quote above is performed by the shell. If when it "clobbers" a file, it creates a new "inode", then the old file still exists as long as the other application continues to have it open for writing. (Theoretically, you could link this inode back to an existing filename in a directory; however normally, once that application finishes, the file is closed and the data disappears forever, because the inode no longer has any links to it.)

On the other hand, if the shell uses the same inode, then it changes the inodes data length to 0 and closes the file again. The application that's writing to it, however, may have a copy of the inode in its buffers somehow (which it shouldn't) and the next time there's a write, the inode gets re-clobbered and the data magically re-appears (except for the first block, which might now be empty).

To "accomplish" this, you could "truncate" the file by moving it instead, and creating a new empty file in its place. The moved file will continue to grow and you can move it back later:

Code:
truncate() {
 mv "$1" .bak."$1"
 touch "$1"
}
The moved file is now ".bak.<original-filename>" in that directory.
 
  


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
Truncating Script blu_alchemist Linux - Software 4 09-13-2007 07:40 PM
Mail File Sizes and Truncating Them mcupples Linux - Server 6 04-03-2007 09:59 AM
truncating output of ls command chucker8 Linux - Newbie 2 03-02-2006 11:01 AM
Truncating trailing characters liguorir Programming 0 05-23-2004 05:00 AM
Truncating a string nkendrick Linux - General 5 11-29-2003 10:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:02 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