Why is moving directories to /dev/null dangerous?
Solution 1:
Because people assume. I was one of those people until I tested it. It's easy to understand why people assume... It looks dangerous...
... but you can't actually move things to /dev/null
— It's a special file that just absorbs redirects (and sends them into nothingness). If you try to move a directory to it, the filesystem will verbosely explode in your face and if you try to move a file to it, you will probably end up replacing it.
The first link will deal with directories, but here's a separate test just for overwriting it with a file. As Rmano points out in the comments, this is probably something you shouldn't do without adult supervision. There is risk involved.
$ echo "this is my file" > test
$ cat test
this is my file
$ sudo mv test /dev/null
$ cat /dev/null
this is my file
# Fix this!
$ sudo rm /dev/null
$ sudo mknod -m 0666 /dev/null c 1 3
Solution 2:
/dev/null
is just a file, it's a "special character" file but it's non the less still bound by the rules that files must follow. That being said you could never run this command:
$ mv ~ /dev/null
The mv
command won't allow this since you're moving a directory to a file, that just doesn't make sense contextually and mv
knows this.
Example
$ mkdir dir
$ touch afile
$ mv dir afile
mv: cannot overwrite non-directory ‘afile’ with directory ‘dir’
You can't copy onto /dev/null
either, given it's a character file, if you try to copy a regular file onto it.
$ cp ~/bzip2_1.0.6-4_amd64.deb /dev/null
$ ls -l |grep null
crw-rw-rw- 1 root root 1, 3 Mar 16 14:25 null
About the only thing you can do to this file is copy mv
over it another file or delete it.
$ mv /path/to/afile /dev/null
After this command, /dev/null
is a regular file. The most dangerous effect of this change is that /dev/null
is supposed to never output any data, so a number of shell script will assume that
`... < /dev/null`
is equivalent to say "nothing". Having this assumption broken can lead to random data (well, the data the last process wrote to `/dev/null') inserted in system files all around the system --- which could lead to an utterly broken and unrecoverable system.