Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Usually output of ps has a pid somewhere. Is there a way to combine this, preferably with a one-liner, with an output of lsof ?


27915 ?        Ss     0:03 gpg-agent --daemon
gpg-agent 27915      httpd  mem     REG      104,1    144776     229236 /lib64/
gpg-agent 27915      httpd  mem     REG      104,1   1718232     229237 /lib64/
gpg-agent 27915      httpd  mem     REG      104,1     23360     229238 /lib64/

6139 ?        Ss     0:00 /usr/sbin/restorecond
restoreco 6139 root  mem    REG  104,1    53880 228954 /lib64/
restoreco 6139 root    0u   CHR    1,3      0t0   1771 /dev/null

Note: the first block's first line is output of ps for pid 27915 followed by output lsof -p 27915; the second block is same for pid 6139.

Essentially I would like a join by pid between two commands but output line(s) of first command first and then output lines of second command with same pid. The output is not the same as running something like join -1 2 -2 2 <(ps aux | sort -nk2) <(lsof | sort -nk2) -- this works great but merges two outputs together on same line, producing left side repetition.

share|improve this question
up vote 1 down vote accepted
ps -ef | awk '{ print $1 }' | while IFS= read a_pid ; do echo "" ; ps -p $a_pid ; lsof -p $a_pid ; done
share|improve this answer
awk may be printing the wrong field. adjust depending on the output of your version of ps. Should be $1 or $2 – Olivier Dulac Jan 8 '13 at 4:31
man ps for more options depending on what you need to see (-o pid,ppid,somethingelse,etime,... will usually allow you to get a tailored output)(here again, depending on your os, many differences. if ps -ef is not good, try ps -aux, or read the man ps) – Olivier Dulac Jan 8 '13 at 4:35
Yep, for me just had to change $1 to $2 – Alex Jan 11 '13 at 3:13

Your Answer


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.