两个特殊设备文件dev/zero dev/null

/dev/null:

In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it, and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

This entity is a common inspiration for technical jargon expressions and metaphors by Unix programmers, e.g. "please send complaints to /dev/null" or "my mail got archived in /dev/null", being jocular ways of saying, respectively: "don't bother to send any complaints" and "my mail got deleted". A famous advertisement for the Titanium PowerBook G4 read [The Titanium Powerbook G4] Sends other UNIX boxes to /dev/null.

The null device is also a favorite subject of technical jokes, such as warning users that the system's /dev/null is already 98% full. The April Fool's, 1995 issue of the German magazine c't reported on an enhanced /dev/null chip that would efficiently dispose of the incoming data by converting it to flicker on an internal glowing LED.

/dev/null is a special file, not a directory (folder), so one cannot move files into it with the Unix mv command. See rm for the proper way to delete files in Unix.

The equivalent device in CP/M (and later DOS and Windows) is called NUL:, and on some versions of DOS just NUL (for example, one may hide output by directing it to NUL, e.g. PAUSE>NUL, which waits for the user to press any key without printing anything to the screen). Under classic Amiga operating systems, the device's name is NIL:. In Windows NT and its successors, it is named /Device/Null internally, though, the DOS NUL is a symbolic link to it. Similarly, in OpenVMS the device is named NL:.

   这个就和路由里面的ip route 192.168.2.0 255.255.255.0 null 相通的。路由里面是把来自特定IP的数据包抛弃。因此这种路由也叫做路由黑洞。有时候为了避免路由环路,会用到这种配置。


 linux中,在shell脚本编程时,有时不希望命令运行结果或错误信息输出到标准输出,可以将其重定向到日志文件

commond 2>&1 ,如果不希望错误信息写入文件便可以将其输出到dev/null.

/dev/zero:

In Unix-like operating systems, /dev/zero is a special file that provides as many null characters (ASCII NULL, 0x00; not ASCII character "digit zero", "0", 0x30) as are read from it. One of the typical uses is to provide a character stream for overwriting information. Another might be to generate a clean file of a certain size. Using mmap to map /dev/zero to RAM is the BSD way of implementing shared memory.

# Initialise partition (important note: trying out this command will eradicate 
# any files that were on the partition, make sure you have a backup of any important data.)
dd if=/dev/zero of=/dev/hda7

# Create a large empty file called 'foobar'
dd if=/dev/zero of=foobar count=1000 bs=1000

Like /dev/null, /dev/zero acts as a source and sink for data. All writes to /dev/zero succeed with no other effects (the same as for /dev/null, although /dev/null is the more commonly used data sink); all reads on /dev/zero return as many NULs as characters requested.

 

像/dev/null一样, /dev/zero也是一个伪文件, 但它实际上产生连续不断的null的流(二进制的零流,而不是ASCII型的). 写入它的输出会丢失不见, 而从/dev/zero读出一连串的null也比较困难, 虽然这也能通过od或一个十六进制编辑器来做到. /dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件.

关于 /dev/zero 的另一个应用是为特定的目的而用零去填充一个指定大小的文件, 如挂载一个文件系统到环回设备(loopback device)或"安全地" 删除一个文件.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
04-18 483

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值