Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 542981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:30:11+00:00 2026-05-13T10:30:11+00:00

What happens to an open file handle on Linux if the pointed file meanwhile

  • 0

What happens to an open file handle on Linux if the pointed file meanwhile gets:

  • Moved away -> Does the file handle stay valid?
  • Deleted -> Does this lead to an EBADF, indicating an invalid file handle?
  • Replaced by a new file -> Does the file handle pointing to this new file?
  • Replaced by a hard link to a new file -> Does my file handle “follow” this link?
  • Replaced by a soft link to a new file -> Does my file handle hit this soft link file now?

Why I’m asking such questions: I’m using hot-plugged hardware (such as USB devices etc.). It can happen, that the device (and also its /dev/file) gets reattached by the user or another Gremlin.

What’s the best practice dealing with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:30:11+00:00Added an answer on May 13, 2026 at 10:30 am

    If the file is moved (in the same filesystem) or renamed, then the file handle remains open and can still be used to read and write the file.

    If the file is deleted, the file handle remains open and can still be used (This is not what some people expect). The file will not really be deleted until the last handle is closed.

    If the file is replaced by a new file, it depends exactly how. If the file’s contents are overwritten, the file handle will still be valid and access the new content. If the existing file is unlinked and a new one created with the same name or, if a new file is moved onto the existing file using rename(), it’s the same as deletion (see above) – that is, the file handle will continue to refer to the original version of the file.

    In general, once the file is open, the file is open, and nobody changing the directory structure can change that – they can move, rename the file, or put something else in its place, it simply remains open.

    In Unix there is no delete, only unlink(), which makes sense as it doesn’t necessarily delete the file – just removes the link from the directory.


    If on the other hand the underlying device disappears (e.g. USB unplug) then the file handle won’t be valid any more and is likely to give IO/error on any operation. You still have to close it though. This is going to be true even if the device is plugged back in, as it’s not sensible to keep a file open in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

failed to open file file://D/:/dev/test_all.html JavaException: java.net.UnknownHostException: D Any ideas for why this happens?
What happens if i open a file using fopen some n number of times
Error 1 fatal error LNK1104: cannot open file 'C:\Users\hongo\Documents\VisualStudio2008\Projects\PruebaNodosLcmasmas\Debug\PruebaNodosLcmasmas.exe' PruebaNodosLcmasmas PruebaNodosLcmasmas This error happens
What happens to the JFileChooser object when the open or cancel button is pressed
Because wikipedia is open source, I can modify anything I want. But what happens
It happens to me all the time. I accidentally version a file, I do
This happens after I hit Build and Go to run my app on my
This happens in IDLE and Windows 7 RC1 (if that helps). Here is the
I know that I should open a binary file using rb instead of r
This is a part of an open source project called JNotify. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.