Why can I move things to /dev/null despite it being a pseudo-device represented by a file?

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.


When you do this:

# mv oi /dev/null

You are actually doing the equivalent of the following

# rm /dev/null
# mv oi /dev/null

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

Tags:

Linux