Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Everyone seems to suggest this, and, I guess, this is, what I'll have to do. The thing is, such a scan gives a lot more information, than I need -- it will tell me, which processes have "my" file open, for example -- whereas I'm only looking for whether there are any such... Commented May 9, 2021 at 19:35
  • @MikhailT. you don't need to use the extra info. The example bash algorithm above doesn't, all it does is note the fact that a filename is opened by something,openfiles[$l]=1, so all it ends up with is a list of open files. Commented May 10, 2021 at 1:04
  • though I don't need to use it, I'm paying the price of collecting it. The kernel already has the information I'm looking for -- for each file opened, there is a reference count so that filesystem does not reuse the underlying blocks even if the file is deleted... I just can't figure out, how to access it this information. Commented May 10, 2021 at 1:07
  • So, take a pragmatic approach - implement the obvious method you know now. Replace it with something better (or more aesthetically pleasing) later, when you've figured out if it's possible and how to do it. BTW, there's no guarantee that the kernel or any filesystem drivers even makes that count available to external callers for each individual file (you can get a total open files count from proc/sys/fs/file-nr, but not a list of files). And the answer might be different for each different fs (zfs, for example, doesn't seem to export any info to /sys or /proc/sys/fs). Commented May 10, 2021 at 1:33
  • Such a list may not be available, because no one has ever requested it - most people, for example, want to know about open files in the context of a particular process. e.g. they want to know which pid(s) are preventing them from unmounting a filesystem. and it might not be worth implementing because anyone who wants such a list can always trawl through /proc like lsof does (or like my answer above, which does much less but is faster, 1.5 seconds on my system for my bash code, 15 seconds for lsof. and the same algorithm would be even faster in perl or python or anything that wasn't shell) Commented May 10, 2021 at 1:47