Here's what I use
$ find . ! -exec sudo fuser -s "{}" 2>/dev/null \; -exec echo {} \;
It uses two neat features of find
:
exec
s are chained together, similarly to the bash operation &&
. If the first exec returns a non-zero exit code then the second exec doesn't get invoked.
!
is an inverse operation for the exit code. fuser
returns 1 for file not accessed, and 0 for file accessed, so we need to flip it to get what you want.
Note that I've added a sudo
to fuser, so that it can find files opened by other users. Without this it will happily report that the file is not opened, while in reality it's just reporting that file is not opened by a process you have permission to monitor
Also worth mentioning - obviously calling fuser is more expensive than just checking the inode info, so if you have some other filters that you care about in addition to "is this an open file", you should apply those filters to the find first before doing the more expensive fuser check