So I was messing around with getting a few logging systems setup for a web app I’m writing and after a while these log files started to grow in size to the point that it was difficult to find the relevant data quickly. I needed a quick way to empty a file without deleting it. Here’s the trick I learned:

// First let's put some text into a file
$ echo "test" > filename.log

// Let's display it
$ cat filename.log

// and empty it
$ cat /dev/null > filename.log

Basically ‘cat’ reads the file contents of whatever you specify, in this case /dev/null which is empty. It then uses the ‘>’ character to push the contents of /dev/null (empty) to filename.log, overwriting anything that currently exists in it.

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

3 Comments

  1. An even quicker way to clear a file:

    $ > filename.log

    😉 Just thought I’d share.

    Reply

  2. Greetings! I know this is kinda off topic nevertheless I’d
    figured I’d ask. Would you be interested in trading links or maybe
    guest authoring a blog article or vice-versa? My site discusses a lot of the same subjects as yours
    and I believe we could greatly benefit from each other. If you might be interested
    feel free to send me an email. I look forward to hearing from you!
    Great blog by the way!

    Reply

  3. When someone writes an post he/she maintains the idea of a user in his/her
    brain that how a user can be aware of it. Thus that’s why this post is amazing.
    Thanks!

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *