Close

Copy a file after it's been deleted

A project log for Silly software wishlist

Motivation to do some software projects by writing them down.

lion-mclionheadlion mclionhead 05/07/2020 at 20:450 Comments

A common technique in content management is for a web browser to download to a temporary file, then rename the temporary file only after the download finishes & it has permission to copy it.  If the download fails or of the content is copy protected, the browser deletes the temporary when it finishes. 

You can copy the file before it's deleted, but there's no way with the traditional cat & cp programs to get the complete file.  The cat & cp programs exit when they hit the end of the file.  Fortunately for Linux, once you have a file handle open, the file handle can still read to the end of the file after the file has been deleted.  The rm & unlink commands are exceptions to the rule of not being able to remove something that's still in use.

After years of using a custom program to open a file & wait for new output indefinitely, the lion kingdom finally found a variant of the tail program which does it.

tail -n +0 -f

It just outputs all the data starting from line 0 & waits for new output.  This technique can't undelete files, of course.

Discussions