Last Updated: February 25, 2016
·
469
· broozer

linux : empty file from command line

Being faced with large log files that contains info you no longer need ?

cat reads the contents of /dev/null (which contains nothing) and > writes this into <filename>.

$ cat /dev/null > <filename>

see more : http://hackaddict.blogspot.be/2007/06/quick-tip-delete-contents-of-file.html

2 Responses
Add your response

$ echo > <filename>

and even shorter, but I don't like the aesthetic

$ > <filename>
over 1 year ago ·

"echo > foo" doesn't create an empty file, it creates a file of size 1

"> foo" does work correctly.

A traditional alternative is the bash builtin ":" which does nothing:

:> foo

over 1 year ago ·