$ sudo lsof -u t | grep -i "\.pdf"
evince 1788 t 37r REG 8,4 176328 134478 /home/t/some/path1/white space/string1 + string2 string3.pdf
evince 3737 t 36r REG 8,4 1252636 6692680 /home/t/some/path2/white space/string5 string3.pdf
How can I extract only the second column (pids of processes)?
How can I extract only the ninth column (pathnames of files)? (pathnames can contain any character allowed by Linux and ext4 file systems)
My real command is
$ sudo lsof -u t | grep -v "wineserv" | grep REG | grep "\.pdf" | grep "string"
where I would search for records whose first column "COMMAND" isn't wineserv
, and fifth column "TYPE" is REG
, and whose ninth column "NAME" contains .pdf
and string
.
Prefer bash, awk or Python solutions (and maybe Perl, but I don't know Perl, so won't be able to verify if it is correct or modify it later)
Thanks.
lsof
has-F
flag according to the manual, so you could dolsof -F p
to get just the PID itself. Let me know if you want that as an answer, but of course I can do Python and awk parsing as well – Sergiy Kolodyazhnyy Feb 16 '19 at 01:38find /proc/*/fd -ilname '*.pdf' 2>/dev/null | awk -F/ '{print$3}'
(btw, this will also work if the filenames contain newline, spaces, etc). – Feb 16 '19 at 12:56find /proc/*/fd -ilname '*.pdf' 2>/dev/null | awk -F/ '{print$3}'
accordingly? – Tim Feb 16 '19 at 23:02find /proc/*/fd -ilname '*.pdf' -printf '%l\n'
,find /proc/*/fd -ilname '*.pdf' -printf '%p\t%l\n'
. You can also get that info with whatever language you want (C
,perl
,python
, etc). The value added by a tool likelsof
should be the ease of use and the human-friendly way it presents that info -- andlsof
fails at both spectacularly. – Feb 17 '19 at 09:02