Empty the Contents of a File In Linux

As I work on my web apps, I find it helpful to keep a log file that I can output content to, so I can see what’s going on, especially when AJAX is involved, and I can’t simply output the results to the browser. There are times, however, that I need to truncate the file and start over. What to do?

Well, it’s actually pretty simple:

cat /dev/null > filename.txt

This will keep the file and it’s privileges (I have mine owned by myself and the group as apache), but truncate it to 0 length. Then I can `tail -f` the file, and all is well with the world.

I hope this may help someone else in the future.



Posted in Linux Tagged with: