Truncate a file in bash

Posted by & filed under Linux.

Thanks to Tom Henningam for documenting these options. I’ve only tried the first one and it worked fine. Should work from a bash shell.

: > file
$ echo 'hello world' > somefile.txt
$ cat somefile.txt
hello world
$ : > somefile.txt
$ cat somefile.txt
$ du -h somefile.txt
0       somefile.txt

cat < /dev/null > file
$ echo 'hello world' > somefile.txt
$ cat somefile.txt
hello world
$ cat < /dev/null > somefile.txt
$ cat somefile.txt
$ du -h somefile.txt
0       somefile.txt

echo -n > file
$ echo 'hello world' > somefile.txt
$ cat somefile.txt
hello world
$ echo -n > somefile.txt
$ cat somefile.txt
$ du -h somefile.txt
0       somefile.txt

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Bitnami