Search code examples
parsingshellsortingawklsof

Ignoring directories from a file


I am in the process of creating a script that lists all files opened via lsof output. I would like to checksum specific files and ignore directories from that output but am at a loss to do so EFFECTIVELY. For example: (I'm using FreeBSD btw)

lsof | awk '/\//{print $9}' | sort -u | head -n 5    

prints:

/
/bin/sleep
/dev/bpf

What I'd like to do is: FROM that output, ignore any directories and perform an md5 on FILES (not directories).

Any pointers?


Solution

  • Give a try to following perl command:

    lsof | perl -MDigest::MD5=md5_hex -ane '
        $f = $F[ $#F ]; 
        -f $f and printf qq|%s %s\n|, $f, md5_hex( $f )
    '
    

    It filters lsof output to plain files (-f). Take a look into perlfunc to change it to add different kind of files.

    It outputs each file and its md5 separated by a space character. An example in my system is like:

    /usr/lib/libm-2.17.so a2d3b2de9a1f59fb99427714fefb49ca
    /usr/lib/libdl-2.17.so d74d8ac16c2d13128964353d4be7061a
    /usr/lib/libnsl-2.17.so 34b6909ec60c337c21b044642b9baa3d
    /usr/lib/ld-2.17.so 3d0e7b5b5c4e59c5c4b6a858cc79fcf1
    /usr/sbin/lsof b9b8fbc8f296e47969713f6369d97c0d
    /usr/lib/locale/locale-archive 3ea56273193198a718b9a5de33d553db
    /usr/lib/libc-2.17.so ba51eeb4025b7f5d7f400f1968f4b5f9
    /usr/lib/ld-2.17.so 3d0e7b5b5c4e59c5c4b6a858cc79fcf1
    ...