DISQUS

Tech-Recipes: How to empty or clear the contents of an existing UNIX file | UNIX | Tech-Recipes

  • MACMac · 1 year ago
    The touch
    command may also be used to create a new file that is empty.
  • Anonymous · 1 year ago
    Instead of emptying it you can simply fill it with nothing
    echo "" > file

    Cheers,
    Philip
  • Bruce · 6 months ago
    Wrong! This inserts a newline. `echo -n >file` is correct. Use `ls -l file` to show the difference.
    These tips are important, as they do not close the file handle; useful for logs.
  • Anonymous · 1 year ago
    Using echo you actually creates a file with 1byte (a single end of line character) and not a truly empty file as with the /dev/null method does.
    And just as a curiosity both methods also work on the OS X.
  • Anonymous · 1 year ago
    Not sure you have read the question, but the real answer is to execute the following:

    cat /dev/null >/Filaname

    If the file is in use this will affectively zero out the contents, the only time I found this to be a problem is with Java programming. It appears the java program reads everything in to memroy and then writes to cache. If you hit the timing right, you can zero out the file, and all of a sound the full contents will be back. Like I said, I have only seen this in a java program writing to the disk. In all other cases, like zero'ing mail boxes, or large log files, it works welll.

    touch only creates an empty file if none are present, echo "" adds the unix delimiter LF.

    Later+
    Mike
  • MACMac · 1 year ago
    You are correct about the touch command not zeroing out an existing file which was the point of the recipe.
  • Jesus E. Aneiros · 1 year ago
    cp /dev/null blah.txt

    It will save you a t and a >.
  • Quinn McHenry · 1 year ago
    Nice.
  • Jacob · 1 year ago
    echo -n > file.txt
  • Aleq · 1 year ago
    i was using grep a > bla.txt o_O
    it does empty the file, but you need to Ctrl+c after...
  • Tech Blog · 7 months ago
    I use a lot of

    echo " " > blah.txt

    But whatever works will do :)
  • dinesh · 6 months ago
    very helpful
  • aesha · 2 months ago
    Thank you .This thing had worked out well for me
  • MNS · 1 month ago
    Please follow the step to empty all the files. Here there can be some change of ";". It can be used or omitted according to the shell.

    for i in 'ls';
    do
    echo "" >$i;
    done