How to truncate content of file in command line

Published: 2024-07-08

Updated: 2024-07-17

Clear content of existing file without removing itself

$ truncate -s 0 path/to/file
$ echo "" >path/to/file

Command above can be simplified, because there is a special syntax for truncate the file.

$ >path/to/file

When I’ve tested on zsh, bash, oksh, ksh, dash. Only zsh not worked.

There is more portable command which was working also in zsh.

$ :>path/to/file

Resources