3
mv ~ /dev/null    

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

2
  • 7
    This mv ~ /dev/null command won't work."mv ~ /dev/null Moves my home directory to a (black hole)" line is totally wrong. Mar 18, 2014 at 13:06
  • 1
    Also related (and perhaps the best master question we have for this): I can read from /dev/null; how to fix it? Mar 19, 2014 at 2:56

2 Answers 2

29

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. Our 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.

2
  • 2
    According to this in some systems, moving a file to /dev/null replaces the device instead of removing it.
    – Braiam
    Mar 18, 2014 at 12:48
  • Why do you do the move attempt as root? You just said "We'll ask them to move", not "ask root".
    – Ruslan
    Feb 7, 2016 at 18:53
-1

In such a case, the first thing to do is to stop the system. The hope is that the files are still somewhere on the hard disk; by stopping the system we want to avoid that they are overwritten (if removed, they are marked as free space).

The second thing to do is to read data recovery manual and follow the instructions therein. Unfortunately, this succeeds as often as not.

The third is to check your backups (there are two sorts of people: those who do backups and those who will; hope you are the former kind).

1
  • 2
    None of this is true or needed, see Oli's answer.
    – terdon
    Mar 18, 2014 at 18:12

Not the answer you're looking for? Browse other questions tagged .