/dev/null VS /dev/zero

本文详细对比了Linux系统中/dev/null和/dev/zero的功能差异。/dev/null作为黑洞设备,会丢弃所有输入;而/dev/zero则产生无限的空字节流,常用于创建空白文件或交换空间。通过cat和dd命令演示了两者的具体用法及输出区别。

Refer to https://unix.stackexchange.com/questions/254384/difference-between-dev-null-and-dev-zero

https://en.wikipedia.org/wiki//dev/zero

 

Yes, both accept and discard all input, but their output is not the same:

  • /dev/null produces no output.
  • /dev/zero produces a continuous stream of NULL (zero value) bytes.

You can see the difference by executing cat /dev/null and cat /dev/zero.

  • Try cat /dev/null > file and you will find an empty file.

  • Now try cat /dev/zero > file, while watching the size of the file (watch -n 1 du -h file) continuously increase. This is because reading from /dev/zero gives an endless stream of \0 (null) characters.

Use dd to visualize the difference more appropriately:

$ dd if=/dev/null of=file count=10
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000276193 s, 0.0 kB/s

 

$ dd if=/dev/zero of=file count=10
10+0 records in
10+0 records out
5120 bytes (5.1 kB) copied, 0.00090775 s, 5.6 MB/s

/dev/zero is used to create dummy files or swap.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值