Can I recover a file from /dev/null? [duplicate]

mv ~ /dev/null    

moves my home directory to a black hole. Can I recover my home directory from /dev/null?


Solution 1:

The accepted existing answers are at least partially incorrect. /dev/null is a "black hole" but you can't move directories to it. The whole mv ~ /dev/null thing appears to be some sort of hoax. It looks really dangerous but it shouldn't actually work.

Still, I wouldn't recommend testing it on a home directory with irreplaceable data. Here's a quick test harness with a new user called test. We'll ask them to move their home into the ether.

$ sudo adduser test
$ sudo usermod -aG sudo test

$ sudo su test  # we are now running as test but let's check
$ cd ~; pwd
/home/test

$ sudo mv ~ /dev/null 
mv: cannot overwrite non-directory ‘/dev/null’ with directory ‘/home/test’

If something you wanted does end up in /dev/null, the recovery procedure is the same as any other data loss (as if you typed rm). Ideally stop using that disk as quickly as possible and start using something like TestDisk to recover. Ourdata-recovery tag can be really helpful here.


Just to clarify that you can sudo mv file /dev/null where file is a single file but this will not delete the file, it will replace /dev/null with that file. This has all sorts of fairly important ramifications because things rely on being able to redirect to /dev/null. Anyway, here's a simple proof of this as well as a fix in case you do end up moving a file over the top:

$ 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

Obviously if you want to rescue your file be quick and cp /dev/null ~/Desktop/file (or somesuch) before you replace it with the system definition of /dev/null.