use lsof to list open files in linux

Recently I encountered a situation, in which I need to list open files under a directory. I searched the quick start tutorials of lsof command and found a few useful snippet. I posted them here.

1. List open file or directory using its path.

lsof /path/to/the/file/or/directory

2. List open files or directories under the top-most level of a directory

lsof +d /path/to/the/directory

Note: This also includes the top-most directory itself.

3. List open files recursively under a directory

lsof +D /path/to/the/directory

Note: This also includes the top-most directory itself.

4. List open files opened by a particular process

lsof -p pid

5. List  open files owned by a particular user

lsof -u user

6. List open files matching two or mare criterias

lsof -a -u user -p pid

7. List open files, which has a link count less than a value

lsof +L 8

The above commands except the last one cover most of the use cases in our daily work. I will write another post about the last one in the future. In fact, this post is triggered by the last command, which can demystify the abnormal output of the df command.