How to Undelete files removed with “rm” in Ubuntu
Recently I found myself in a situation where I needed to recover some files I accidentally deleted. I had written a program in Java to read off a JBoss Messaging Dead Letter Queue, and save them in a format that could be re-inputted back into the system.
So my program was writing files into a directory within my home directory, and while I was testing the program I was not committing back to JMS so ensure the messages stayed there. When I was confident that the program was correct, I added the commit, I then removed the files that my tests had produce by using rm \*
.
I then ran my program for real. In the directory I was expecting files to appear I typed the usual ls -ltr
to see a list of files. I pressed the up key to run the last command i.e. ls -ltr
to see some more files created. I repeated this a few times, until I accidentally pressed the up key twice…..yep you guessed it….rm \*
, the files were deleted and the commit to JMS means they were no longer on the queue. My heart sank.
Now I rememebered on the old days of MS-DOS
there was an UNDELETE
command, so I was looking around the web to see if there was something similar for Linux. I was in luck, especially as my program was still running.
Basically thanks to this information, I recovered my deleted files with relative ease. 🙂
Ok so to do this, I believe you need to be as fortunate as I was and still have the program that created the deleted files still running, so let’s create a Java program that will write to a file and then keep running…
Ok, now run that program and it will create a file in /tmp
called testFile.txt
, so lets see it..
So we can see the file is there, let’s delete it to see if we can recover it…
Now the file is gone. So how do we get it back? Well first we need to find the PID of the process that created the file that was deleted. We know that it was a java program that created it so we’ll use the lsof and search for java..
No now we know the PID is 9048 lets find the file… so there should be a directory /proc/[PID]/fd…
Bingo! We have found the deleted file, so now all we need to do is cat the link to a new file, so here I put it into my home directory…
Next, I go to my home folder and have a look at the file, and hey presto… it’s restored…
Phew!