Data written to the null special file is discarded.
No data can be read from null since an immediate end-of-file (EOF) occurs; a read system call on null will always return 0 (zero).
Sometimes you might not want to see the error messages from a command.
If you are looking for a file in a filesystem using the
find
command, you can ignore the messages (on standard error) telling you
that you did not have permission to look at certain directories:
find / -name foofile -print 2> /dev/null
You can also use null to obtain harmless input. In the following
example,
grep
is forced to output the name of each file containing
pattern as well as the matching lines:
find / -exec grep pattern {} /dev/null \;
find runs grep on every file in the entire filesystem in turn. If /dev/null was not given as an additional file to search, grep would print the matching lines found, but not the name of the file. This is because grep omits the filename when examining only one file. /dev/null acts as a ``dummy'' second file.
A more common use of null as input is to discard the contents of
a file but leave its entry in the directory:
cat /dev/null > file_to_empty
An alternative way of doing this is to copy null to the file:
cp /dev/null file_to_empty
AT&T SVID Issue 2;
X/Open Portability Guide, Issue 3, 1990.