简体   繁体   中英

How to find NOT open files with lsof

Say, I got a specific task to find files that are not open . I understand how to get a list of open files with lsof ( lsof +D ). Furthermore I need to find them only in so called 1st level subdirectories. To do this i use find command with maxdepth 1 operand.

smth like this: find -maxdepth 1 -type d -ls >dirlist

Then, in a while loop i am trying to use lsof to check the list of directories (parental and 1 level sub) for NOT open files but i am completely green to bash. Say i might be able to get a list of OPEN files, but it's not what i need. Any ideas are welcomed.

Thanks for any help-

cheers

Update: awk might be helpful, as far as I understand. Also case and shift maybe?

Update 1: I figured out just now that I am supposed to call (use, invoke, not sure which word is correct) for lsof once only, that's why my while loop isn't a suitable solution. Moreover, I need to stdout a list of directories that have no open files , each in a separate line. It's not that hard, i kinda got it working, just trying to clarify what the tsak is. The problem is that i don't know how to redirect a list of directories to lsof without a while loop. smth like this doesn't work the way i want it to: file="path"; name=$(cat "$file") file="path"; name=$(cat "$file")

Given a list of files returned by lsof , you want to remove those from dirlist and print the rest.

lsof +D . | grep -vFxf - dirlist

If lsof descends the directory to a deeper level and finds files which are not in dirlist , the only effect will be that grep cannot find and remove those, so apart from a minor inefficiency, that should be harmless.

grep -F says the search expressions are exact strings, not regular expressions; -x says the match needs to be an entire line; -v says to remove matches; and -f - says to read the search patterns from standard input.

Use find command

find -maxdepth 1 -type f | while read myfile ; do fuser -s $myfile || echo $myfile ; done

"find -maxdepth 1 -type f" - find all files in -maxdepth 1 directory
"while read myfile ; do fuser -s $myfile || echo $myfile ; done" - check if file does not have its process(it means file is not used) then print this filename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM