Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

mv ~ /dev/null    

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

share|improve this question

marked as duplicate by Richard, psusi, Rmano, rusty, Avinash Raj Mar 18 at 18:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5  
This mv ~ /dev/null command won't work."mv ~ /dev/null Moves my home directory to a (black hole)" line is totally wrong. –  Avinash Raj Mar 18 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? –  Eliah Kagan Mar 19 at 2:56
add comment

2 Answers 2

up vote 21 down vote accepted

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.

share|improve this answer
2  
According to this in some systems, moving a file to /dev/null replaces the device instead of removing it. –  Braiam Mar 18 at 12:48
add comment

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

share|improve this answer
1  
None of this is true or needed, see Oli's answer. –  terdon Mar 18 at 18:12
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.