tech-1137487_1920

[Short Tip] show processes accessing a file: fuser & lsof

920839987_135ba34fff

Sometimes it’s good to know which processes access certain files, paths or devices. Think of debugging, but also of certain files blocked due to currently running processes. There are two commonly used tools to get information about which process accesses which file: lsof and fuser.

The best known tool is arguably lsof – it lists all open files, the output can be limited with options and arguments. Typical commands are:

$ lsof -p $PROCESSID
$ lsof $FILENAME
$ lsof |grep $EXPRESSION

The other well known tool is fuser. Compared to lsof it works the other way around: it lists open processes for a given target. The target must always be supplied and is not optional. Typical commands are:

$ fuser $FILENAME
$ fuser -mva $MOUNTPOINT
$ fuser -k $FILENAME

The last command is used to kill the process directly.

In the day to day work fuser feels slightly less bloated and is more handy to simply kill processes right away. On the other hand, if you are used to lsof there is hardly any reason to switch to fuser.

One thought on “[Short Tip] show processes accessing a file: fuser & lsof”

Comments are closed.