empty file in Unix

How to empty or clear the contents of an existing UNIX file

If you want to empty the contents of a UNIX file, you could delete it and recreate it, but, as is typical of UNIX, there are more elegant alternatives. Here are a couple of ways to dump the innards of a file. If you know of any others, add them to the comments.

The special file /dev/null can and does contain nothing. While most often used as a sink of nothingness, for example writing output to it that you don’t want to keep, it can be used as a source of nothingness, too. To replace the contents of the file blah.txt with nothing, you can use the command

cat /dev/null > blah.txt

This command makes sense as it is using standard UNIX redirection to place the contents of one file (a known empty one) to another file. Another way to do this, although a less obvious way and probably less readable in a shell script by others, is

> blah.txt

Latest Post

Popular Post

Our Skills


Project Director Information Architect Database Administrator Unix Administrator Senior Developer Project Manager Product Manager Web Marketer Cloud Engineer

Contact Us

For any questions or project ideas please contact me at the following.

I'm here