/dev/null
is a character special file, which means that it is a contentless file that is marked as being able to source and sink byte streams to it. What happens to those streams is determined by their device numbers. On my BSD system at the moment:
crw-rw-rw- 1 root wheel 3, 2 Jan 25 14:57 /dev/null
the device numbers are 3,2 (the major number is 3, minor number is 2). I can create other /dev/null
like files using mknod /dev/nil c 3 2
, where the c means I want a character special file, but on BSD systems, the device number pair is dynamically created and might not be the same next boot.
To see how these facts apply in practice:
sh-2.05b# mknod /dev/nil c 3 2
sh-2.05b# ls -l /dev/nil
crw-r--r-- 1 root wheel 3, 2 Jan 25 15:14 /dev/nil
sh-2.05b# echo abc > /dev/nil
sh-2.05b# cat < /dev/nil
sh-2.05b#
Linux uses the major-minor pair 1,3, so mknod /dev/null c 1 3
will recreate your file there.
Character special files are like most other files in many respects, and can be deleted if you have the permissions. Using mv
on a device file is just the same as if you moved a regular file, which will mean that you get a completely new file, which you can tell by looking at the inodes. For example (Mac OS this time, but all *nixes will behave the same here):
helen:tmp cas$ touch abc def
helen:tmp cas$ ls -i1
3775141 abc
3775142 def
helen:tmp cas$ mv abc def
helen:tmp cas$ ls -i1
3775141 def
helen:tmp cas$ echo clobber > def
helen:tmp cas$ ls -i1
3775141 def
See how the inode associated with the filename def
changed after I used mv
, with the old file simply being deleted, but when I wrote the output of echo
to the file named def
, the inode remained the same. So your mv
will either fail, likely if you are not root, or delete the file and put a new file under path /dev/null
, which is not what this character special is for.