lsof – Show Files Open as Read-Write

lsof

When I try to remount a partition as read-only, I get an error /foo is busy. I can list all files open from /foo with

lsof /foo

but that does not show me whether files are open read-only or read-write. Is there any way to list only files which are open as read-write ?

Best Answer

To answer this question specifically, you could do:

lsof /foo | awk 'NR==1 || $4~/[0-9]+u/'

This will show files which are opened read-write under the mount point foo. However, likely you really want to do is list all files which are open for writing. This would include files a which opened write-only as well as those opened read-write. For this you would do:

lsof /foo | awk 'NR==1 || $4~/[0-9]+[uw]/'

These commands should work provided FD is the 4th field in the output and none of the other fields are blank. This is the case for me on Debian when I include a path in the lsof command, however if I don't it prints and extra TID field which is sometimes blank (and will confuse awk). Mileage may vary between distros or lsof versions.

Related Question