Sunday, March 09, 2008

Emptying a File in the Terminal

Here is a handy trick that I find useful from time to time:

$ echo "here is some jibberish" > somefile.txt
$ cat somefile.txt
here is some jibberish
$ cat /dev/null > somefile.txt
$ cat somefile.txt
$

Essentially by calling "cat /dev/null > somefile.txt" we redirect nothing (/dev/null) to the file 'somefile.txt', which in effect empties it. I like to use this for emptying log files as it saves time recreating files and possibly resetting the right permissions.

If interested, you can read more on I/O redirection.

No comments: