您的位置:首页 > 其它

/dev/null 于 /dev/zero文件

2007-12-03 20:33 661 查看
在目录/dev下有两个特殊的设备文件:/dev/null和/dev/zero。它们并不是真实的设备文件,那它们是什么?它们有什么区别?我在自己的blog上做个记录。

下面关于它们的内容来自于维基百科。

关于/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:.

关于/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.

(转)http://www.rainway.org/index.php/2004/10/28/unix-stdout/

细细品味UNIX的数据流重定向

  我们经常会在UNIX系统下的一些脚本中看到类似”2>&1″这样的用法,例如“/path/to/prog 2>&1 > /dev/null &”,那么它的具体含义是什么呢?
  UNIX有几种输入输出流,它们分别与几个数字有如下的对应关系:0-标准输入流(stdin),1-标准输出流(stdout),2-标准错误流(stderr)。”2>&1″的意思就是将stderr重定向至stdout,并一起在屏幕上显示出来。如果不加数字,那么默认的重定向动作是针对stdout(1)的,比如”ls -l > result”就等价于”ls -l 1 > result”。这样便于我们更普遍性的理解重定向过程。
  下面举例说明:
#cat std.sh
#!/bin/sh
echo “stdout”
echo “stderr” >&2

#/bin/sh std.sh 2>&1 > /dev/null
stderr

#/bin/sh std.sh > /dev/null 2>&1

  第一条命令的输出结果是stderr,因为stdout和stderr合并后一同重定向到/dev/null,但stderr并未被清除,因此仍将在屏幕中显示出来;第二条命令无输出,因为当stdout重定向至/dev/null后,stderr又重定向到了stdout,这样stderr也被输出到了/dev/null。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: