Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I allways tough about /dev/null being a black hole, in the way that all data I send there will not be echoed back. However there's some problem with the definition of /dev/null. According to wikipedia /dev/null is:

/dev/null is a special file, not a directory, so one cannot move files into it with the Unix mv command.

So following this definition, why can I "mv" stuff there? For example, I can easily do:

# touch oi
# mv oi /dev/null

???

share|improve this question

migrated from stackoverflow.com Jan 25 '10 at 12:33

This question came from our site for professional and enthusiast programmers.

2  
[citation needed] –  Ignacio Vazquez-Abrams Jan 25 '10 at 12:23
3  
It does not work for me. What OS do you use? –  Anonymous Jan 25 '10 at 12:24
add comment

7 Answers

up vote 8 down vote accepted

If you'll try to move anything to /dev/null under a plain user (not root), you will get a Permission denied error because mv something /dev/null is actually trying to delete /dev/null and then move something to what /dev/null was.

If you will try to do it under root, it will delete /dev/null (and then move the file provided by you in it's place)! You can restore it by rebooting or typing the following in a root shell (on Linux): mknod /dev/null c 1 3; chmod a+w /dev/null or in BSD: mknod /dev/null c 3 2; chmod a+w /dev/null.

share|improve this answer
1  
The device numbers will vary drastically from OS to OS: the pair 1,3 works on linux, but not on bsd-unixes. –  Charles Stewart Jan 25 '10 at 14:06
    
edited accordingly. –  whitequark Jan 25 '10 at 15:13
add comment

When you do this:

# mv oi /dev/null

You are actually doing the equivalent of the following

# rm /dev/null
# mv oi /dev/null
share|improve this answer
1  
I think thats the solution, since its overwrite the node –  Marcos Roriz Junior Jan 25 '10 at 12:48
add comment

You can't

# uname -sr
Linux 2.6.32-ARCH
# touch a
# mv a /dev/null
mv: inter-device move failed: `a' to `/dev/null'; unable to remove target: Permission denied
share|improve this answer
add comment

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

share|improve this answer
2  
You can use cp -R /dev/null /some/other/null. This is POSIX-compliant (mkssoftware.com/docs/man1/cp.1.asp) so it will work on virtually any Unix. –  whitequark Jan 25 '10 at 15:15
    
@whitequark: edited, and I've put in an explanation of the difference between what mv and clobbering do. –  Charles Stewart Feb 24 '11 at 9:56
add comment

Why mv a file to /dev/null (even if it's possible) when you could simply delete it?

Surely, /dev/null is more useful and is intended as a destination for output data?

share|improve this answer
add comment

Shell Scripting is a common use:

cat somefile >/dev/null 2>&1

Sometimes you don't want to see/show errors etc.

share|improve this answer
add comment

/dev/null is like a black hole and/or an empty file. terrific! You can put anything in here and get nothing from there.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.