Why can I move things to /dev/null despite it being a pseudo-device represented by a file?
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?
Solution 1:
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
.
Solution 2:
When you do this:
# mv oi /dev/null
You are actually doing the equivalent of the following
# rm /dev/null
# mv oi /dev/null