Results 1 to 10 of 20
Hi all,
I have a large file that a process writes to. I would like to empty that file. If I delete it the process will stop writing to it.
...
- 07-15-2010 #1Just Joined!
- Join Date
- Jun 2007
- Posts
- 13
How do I empty a file?
Hi all,
I have a large file that a process writes to. I would like to empty that file. If I delete it the process will stop writing to it.
Just flush the content but keep the file.
Regards
H
- 07-15-2010 #2Code:
cat /dev/null > BIGFILE
You must always face the curtain with a bow.
- 07-15-2010 #3
you can also use
> filename- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 07-15-2010 #4Just Joined!
- Join Date
- Apr 2005
- Location
- Clinton Township, MI
- Posts
- 84
> filename is a great answer. I knew about redirecting output and using /dev/null, but > filename is a much faster solution. I like it, and thanks for adding that to our arsenal of quick commands.
- 07-15-2010 #5
Just a technicality: if you delete it the process almost certainly continues writing to it, you just can't get to it any more.
- 07-16-2010 #6Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
More technicality. If you delete the file and the process is still writing to it, you have not released any space from the file system. The file continues to exist and take up space as long as any process is using it.
- 07-16-2010 #7I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 07-16-2010 #8forum.guy
- Join Date
- May 2004
- Location
- arch linux
- Posts
- 18,095
oz
→ new members/users: read this first | new member faq
→ no private messages requesting computer support - post them on the forums!
→ please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.
- 07-16-2010 #9Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
To give any better answer, it will be necessary to know some details of the process that is writing to the file.
I have been suffering mentally from the difference between removing (rm filename) a file and emptying it ( > filename).
To quote _Unix Power Tools_ (O'Reilly 2003):
Sometimes you don't want to remove a file completely -- you just want to empty it:
* If an active process has the file open (not uncommon for log files), removing the file and creating a new one will not affect the logging program; those messages will just keep going to the file that's no longer linked. Emptying the file doesn't break the association, and so it clears the file without affecting the logging program.
. . .
* In Bourne-type shells the most efficient way is to redirect the output of a null command:
$ > afile
If the file already exists, that command will truncate the file without needing a subprocess.
. . .
You can also "almost" empty the file, leaving just a few lines, this way:
$ tail afile > tmpfile
$ cat tmpfile > afile
$ rm tmpfile
That's especially good for log files that you never want to delete completely. Use cat and rm, not mv -- mv will break any other links to the original file.
Hope this helps, and you are less confused than before.Last edited by clowenstein; 07-16-2010 at 05:39 AM. Reason: better explanation.
- 07-16-2010 #10Just Joined!
- Join Date
- Jun 2007
- Posts
- 13
Thanks for everybodys help.
I have deleted this kinds of file before, and when using "du -sk *" the space seems to be reclaimed.
Regards
H


Reply With Quote
