How to make a file empty on linux

1 minute read Modified:

Why use :> instead of > ?

During a IRC conversation somebody mentioned that using :> was a better way than using >. So to make a file named testfile empty you would use

:> testfile

The explaination is that :> is more portable. Some shells (such as zsh) auto-instantiate a cat in the current shell and listen for stdin when given a redirect with no command. Rather than cat /dev/null, : is much simpler. Often this behavior is different in interactive shells rather than scripts, but if you write the script in a way that also works interactive, debuging by copy-paste is much easier.

Recent posts
- full list -