Quick Tip: Delete the Contents of a File without Removing and Recreating It

I had to clear out some log files on one of the iminlikewithyou servers yesterday, but I didn't want to delete and recreate the files because I thought that might screw up permissions. I'd been meaning to learn how to zero out a file to empty contents for a while, so I took this opportunity to figure it out.

Do all this from the command line or terminal.

This will create a new file named t whose contents is the word "test":
$ echo "test" > t

more will display the contents of the file:
$ more t
test

Here's the new trick I learned. cat reads the contents of /dev/null (which contains nothing) and > writes this into t
$ cat /dev/null > t

Now t contains nothing:
$ more t

Get rid of it (don't do this to your logfile)
$ rm t

Categories: ,

Copyright © hackaddict.net | family andrew kortina Top ↑

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net