/dev/null is a character special file, it is an empty device, it is a special device file, it discards all data written to it, the content written to it is lost forever, and there is nothing to read.
We use the file command to check it, indicating that the type is a character special file.
[root@localhost ~]# file /dev/null /dev/null: character special
try to read
[root@localhost ~]# cat /dev/null
Nothing can be read, like a black hole.
So we generally treat /dev/null as a garbage dump, throwing unwanted things into it. For example, to clear the contents of a file.
Example:
[root@localhost oa]# ls >> a.txt [root@localhost oa]# cat a.txt a.txt passwd time.sh [root@localhost oa]# cat /dev/null > a.txt [root@localhost oa]# cat a.txt
You can also read the contents of a file into /dev/null. However, when the file does not exist, it returns an error. We can prepend the number 2.
[root@localhost oa]# cat test.txt >/dev/ null cat : test.txt: no such file or directory [root@localhost oa]# cat test.txt 2>/dev/null
Generally standard output and standard error output are screen, so error messages will still be output on the screen. This number represents standard output.
0: means the standard input stream (stdin),
1: Indicates standard output (stdout).
2: means standard error output (stderr)
The above is to redirect standard error ( 2 ) output to /dev/null, so no more error messages are displayed on the screen.