What is /dev/null?

I was told that it is kind of a "black hole" in the UNIX system. Whatever you put there is gone forever. But where to find of if itd just a joke .......dunno. :confused: :) :( :( :(
 
/dev/null

Device 0

It's a blackhole. Nothing exists there, so if you put something there, it goes bye bye for a long long time! :D
 
It has a very useful purpose in scripting: in order to discard unwanted (diagnostic or otherwise) output from a command in a shell script, one simply redirects this output to /dev/null.

One example is using grep to see whether a file contains a regex; grep will print the line(s) containing this regex, which may not be needed in the script--one is interested only in yes/no

if grep REGEX file >/dev/null 2>&1; then ...

will do nicely.

/dev/null is also an empty file, and can be used to truncate existing files by copying it on top of them

cp /dev/null file-to-truncate

will in effect make file-to-truncate to be of size zero (even if it previously existed), because it copied an empty file (there are other ways of doing that)
 
Back
Top