Monday, April 29, 2024
28
rated 0 times [  28] [ 0]  / answers: 1 / hits: 6581  / 3 Years ago, thu, august 19, 2021, 6:27:02
mv ~ /dev/null    


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


More From » data-recovery

 Answers
6

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.


[#35342] Friday, August 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terose

Total Points: 185
Total Questions: 125
Total Answers: 131

Location: Venezuela
Member since Mon, Dec 13, 2021
2 Years ago
terose questions
;