Linux – How to recover open but deleted file on Linux using ln instead of cp

file-recoverylinux

I have a file that's downloading (from a source that's hard to re-download from), but accidentally deleted from the filesystem namespace (/tmp/blah), and I'd like to recover this file.

Normally I could just cp /proc/$PID/fd/$FD /tmp/blah, but in this case that would only get me a partial snapshot, since the file is still downloading. Furthermore, once the download completes, the downloading process (e.g. Chrome) will close the FD.

Any way to recover by inode/create a hard link? Any other solutions? If it makes any difference, I'm mainly concerned with ext4.

Best Answer

Try using tail to copy the file continually:

tail -c +0 -f /proc/$pid/fd/$fd > filename

Of course, you will have to stop the tail process by hand (or some other external means) when the download has finished.