Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to "list" all the files that are NOT open, within a certain directory using the GNU find command. Now, I am able to list the files that are open, but can't figure out a way to do the inverse of it.

find /my/dir/* -exec lsof {} \;  

Any ideas how this can be done?

share|improve this question
up vote 11 down vote accepted
find /my/dir -type f | while read filename ; do fuser -s $filename || echo $filename ; done

This uses find just to generate a list of files. Then the while loop iterates over the list of results, for each result it runs fuser -s $filename which will exit with success if something is using the file.

The || echo $filename part means "if fuser failed (meaning nobody is using the file), echo the filename"

share|improve this answer

Here's what I use

$ find . ! -exec sudo fuser -s "{}" 2>/dev/null \; -exec echo {} \;

It uses two neat features of find:

  • execs 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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.