Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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. ...
  1. #1
    Just 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

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    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,568
    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
    -------------------

  4. #4
    Just 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.

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

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

  7. #7
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    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.

  8. #8
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    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

    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.

  9. #9
    Just Joined!
    Join Date
    Jul 2008
    Posts
    81
    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.

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

Page 1 of 2 1 2 LastLast

Posting Permissions

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