I always thought of /dev/null as a black hole, in the way that all data I send there will not be echoed back. However there's a problem with that definition. According to Wikipedia:

/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

What's going on there?

  • 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

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.

  • 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

When you do this:

# mv oi /dev/null

You are actually doing the equivalent of the following

# rm /dev/null
# mv oi /dev/null
  • 1
    I think thats the solution, since its overwrite the node – Marcos Roriz Junior Jan 25 '10 at 12:48

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

/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

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.

  • 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

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?


Shell Scripting is a common use:

cat somefile >/dev/null 2>&1

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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