How to *really* remove a file?

Bo Berglund bo.berglund at gmail.com
Thu Jan 5 09:56:50 UTC 2023


On Thu, 5 Jan 2023 08:24:46 +0000, Chris Green <cl at isbd.net> wrote:

>On Thu, Jan 05, 2023 at 09:19:11AM +0100, Bo Berglund wrote:
>> I have noted that on Ubuntu if I have a file and run:
>> 
>> rm filename
>> 
>> the file vanishes from sight (is no longer listed by ls or the like) but it
>> seems to still exist!
>> 
>> For example if I have a process logging to the file when I run the rm command it
>> still continues to log (invisibly) to the now removed file instead of creating
>> the file anew and log to the new (same name) file.
>> 
>> Same if I don't rm it but instead mv it to a new name, then the logging
>> continues into the new name of the old file.
>> 
>That's exactly how it's expected (meant?) to work.   Once a process
>has a file descriptor open it uses that to access the file and no
>longer looks at the directory entry (which is what ls reports) at all.

That is what I thought happened according to observations...

>Only when the file descriptor is no longer in use will the 'file' (as
>in the space on the storage device) be made available for re-use.

Is there a way to interrogate which processes has an open file descriptor (aka
handle) to a specific file?

>Why would you want it otherwise?

Well, one case is to make an edited version of a video replace the un-edited
version when the user has already started viewing the un-edited video file.
Basically remove access to the unedited video also for an already started
player. Then save the new video to the old name.

This seems only to be possible if I write zeros to the file, or mybe this is not
even possible?

Like thís:

mv originalfile newfile  #To free up the file name
mv editedfile originalfile  #Replace the original file with edited file
echo "0" > newfile #Reset the moved file to contain 0.

The last command should stop any viewing from continuing, right?


-- 
Bo Berglund
Developer in Sweden




More information about the ubuntu-users mailing list