How can I find all files open within a given directory?

2

I know I can use lsof to list open files, and I know I can use find to find files by inode within a given directory.

But how can I effectively combine these two programs to list all open files which are open within a given directory? Or is there a better way to answer this question?

spraff

Posted 2015-01-03T17:12:39.083

Reputation: 1 238

Answers

3

Easy. Just pipe the output of the lsof command into grep for further processing like this:

sudo lsof | grep /path/of/directory/you/care/about

JakeGould

Posted 2015-01-03T17:12:39.083

Reputation: 38 217

4

lsof has switches for doing this.

  • lsof +d 'directory' (will list open files in the folder)
  • lsof +D 'directory' (will list open files recursively)

5hack

Posted 2015-01-03T17:12:39.083

Reputation: 41

2This should be the accepted answer. – dr01 – 2018-11-13T09:27:20.600

If you just care about the current working directory, you can do lsof +d . – rinogo – 2019-03-27T15:45:44.807