The null device : /dev/null

/dev/null is a special file in unix. It is also called the ‘Bit Bucket’. Data sent to the /dev/null is as good as sending data to a black hole. But why would you ever want to send data to a black hole ? Here’s an example of why you might use it :

Lets say you want to list all files in a directory which contains the word ‘test’ in it.

Sharaths-MacBook-Air:shell sharath$ grep -l test ~/Documents/*

grep: /Users/sharath/Documents/Dir: Is a directory


You can get rid of the annoying error message by using 2>/dev/null in the command.

Sharaths-MacBook-Air:shell sharath$ grep -l test ~/Documents/* 2>/dev/null


Sharaths-MacBook-Air:shell sharath$

The number 2 stands for the STDERR (STDIN and STDOUT being o and 1 respectively). So your pretty much telling the program to send the error messages to the black hole !


Theres another situation where /dev/null might be useful and that is when you want to check the exit status of a command and your not interested in its output. Example :

if grep pattern file > /dev/null


#pattern exists


#pattern doesn't exist



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s