Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    4,028
    Code:
    cat /dev/null > BIGFILE
    You must always face the curtain with a bow.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,847
    you can also use
    > filename
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Apr 2005
    Location
    Clinton Township, MI
    Posts
    104
    > 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.
    Brian Masinick
    masinick AT yahoo DOT com

  6. #5
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    Just a technicality: if you delete it the process almost certainly continues writing to it, you just can't get to it any more.

  7. #6
    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.

  8. #7
    Administrator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,874
    Quote Originally Posted by clowenstein View Post
    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.
    Oh noes!
    A file system blackhole, coming for my data?!

    What does > filename do? You can't leave us hanging like that, sorry.
    I 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.

    I'd rather be lost at the lake than found at home.

  9. #8
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,732
    Quote Originally Posted by MikeTbob View Post
    What does > filename do? You can't leave us hanging like that, sorry.
    It will create an empty file with the same permissions that the file you are replacing had. Replace "filename" with the name of the file you want to be cleared. An example might be like this:

    Code:
    > /var/log/dmesg.log
    oz

  10. #9
    Quote Originally Posted by hristo77 View Post
    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
    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.

  11. #10
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •