Clear a file using /dev/null in Linux

/dev/null is often referred to a black hole in Linux based systems. It discards all the data written to it and sends EOF (End of File) character to any process reading data from it. With this logic, we can clear the contents of a file

$ cat file.txt

This file has some content. Let's use /dev/null to clear the file

Let’s clear the file contents

$ cat /dev/null > file.txt

As you can see we are directing the contents of /dev/null to the file.txt. As mentioned earlier, /dev/null is a blank file, size of which is 0. So by redirecting /dev/null to the file file.txt, Let’s confirm this

$ stat file.txt
File: `file.txt'
 Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d    Inode: 1004        Links: 1

You can see more options to clear a file in Linux.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s