Saturday, January 3, 2015

Find Commands of Open Files - lsof | ps alias

alias lsofcmd=$'_(){ CP="\\033[1;3" && Y="${CP}3m" && C="${CP}6m" && G="${CP}2m" && RST="${CP}0m" && IFS=$(echo -en "\\n\\b") && for a in $(lsof -w ${1} | awk \'{printf "Short_Cmd: %s Pid: %s File: ",$1,$2;for(i=9; i<=NF; i++)printf "%s ",$i;printf "\\n";}\' | sed \'1d\'); do echo -en "${Y}[[[[${C}${a}\b${Y}]]]]=>${RST}${G}$(/bin/ps --no-headers -p $(awk \'{print $4}\' <<< "${a}") -o cmd -ww)${RST}\n"; done; }; _' 

E.g.  $ lsofcmd /dev/sdb1
                                                                         . . .
[[[[Short_Cmd: wget Pid: 29355 File: /var/host/media/removable/MyBook/software/Qt/submodules/qt.mirror.constant.com/archive/qt/4.5/qt-win-opensource-src-4.5.0.zip]]]] => wget -E -r -N -Kk --random-wait --content-disposition --no-check-certificate -p --restrict-file-names=windows,lowercase,ascii --header User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.2) Gecko/20100101 Firefox/19.0 http://download.qt.io/archive/qt/5.4/5.4.0/submodules/ 

This is a colorized version, it should work on practically any terminal since it only uses colors in the 8 basic ANSI colors range 30-37 and looks decent on either a white or black background. If you want to see a good tips on Bash colorization have a look here:  FLOZz'

No comments:

Post a Comment