8

I started downloading a big file and accidently deleted it a while ago. I know how to get its current contents by cping /proc/<pid>/fd/<fd> but since the download is still in progress it'll be incomplete at the time I copy it someplace else.

Can I somehow salvage the file right at the moment the download finishes but before the downloader closes the file and I lose it for good?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175

2 Answers2

11

Using tail in follow mode should allow you to do what you want.

tail -n +0 -f /proc/<pid>/fd/<fd> > abc.deleted

I just did a quick test and it seems to work here. You did not mention whether your file was a binary file or not. My main concern is that it may not copy from the start of file but the -n +0 argument should do that even for binary files.

The tail command may not terminate at the end of the download so you will need to terminate it yourself.

Richm
  • 3,812
  • 23
  • 15
3

Actually I had another thought. If you use another command to read /proc/<pid>/fd/<fd> and keep that program running then when you download completes you can copy it from the appropriate /proc/<pid2>/fd/<fd2> file of the second command.

Just try

less /proc/<pid>/fd/<fd>

in a separate shell. Looking at the /proc entry for the less command should show you your deleted file.

Richm
  • 3,812
  • 23
  • 15