Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 9 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
    
+1 for suggesting fuser !!! Thanks a ton !!! –  puffadder Feb 14 '12 at 14:51
add comment

Your Answer

 
discard

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.