【linux】/dev/null作用

      /dev/null属于字符特殊文件,它属于空设备,是一个特殊的设备文件,它会丢弃一切写入其中的数据,写入它的内容都会永远丢失,而且没有任何可以读取的内容。

我们用file命令查看下,说明类型是字符特殊文件。

[root@localhost ~]# file /dev/null
/dev/null: character special

尝试读取

[root@localhost ~]# cat /dev/null

什么也读取不到,就像一个黑洞一样。

所以我们一般会把/dev/null当成一个垃圾站,不要的东西丢进去。比如来清除文件中的内容。

示例:

[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

也可以把一个文件内容读到/dev/null里面。不过当文件不存在的时候回报错。我们可以在前面加上数字2。

[root@localhost oa]# cat test.txt   >/dev/null
cat: test.txt: 没有那个文件或目录
[root@localhost oa]# cat test.txt   2>/dev/null

一般标准输出和标准错误输出都是屏幕,因此错误信息还是会在屏幕上输出。 这个数字代表的是标准输出。

    0:表示标准输入流(stdin),

    1:表示标准输出(stdout)。

    2:表示标准错误输出(stderr)

   上面就是将标准错误( 2 )输出重定向到/dev/null,所以屏幕上不会再显示错误提示了。

猜你喜欢

转载自www.cnblogs.com/songgj/p/8998049.html