0

I'm in a Ubuntu 18.04 environment, and I'm trying to grep the output of lsof to get some detailed info about files being locked on my system.

My command is simple, and goes like so: sudo lsof | grep 'query'. This however, always contains the same output, and the output includes many lines which do not match the grep query.

Specifically, I'm getting lines similar to lsof: no pwd entry for UID 496, and lsof: WARNING: can't stat() overlay file system.

I suspect this may be because the pipe operator | only pipes STDOUT, and not STDERR. If this is the case, how do I pipe STDERR and STDOUT to a single command? If this is not the case, why is my grep command printing out things that don't match the query?

tuskiomi
  • 190
  • 1
  • 15
  • Correct, `|` does indeed bypass stderr. That's what you _want_, generally; it would be a really bad idea for log messages, warnings, etc to end up in your data files instead of being readable to the user who's running the command that throws the warning. – Charles Duffy Nov 10 '20 at 19:02
  • 1
    Anyhow, `sudo lsof 2>&1 | grep 'query'` is your answer. Let me find a duplicate... – Charles Duffy Nov 10 '20 at 19:02
  • @anubhava my lsof output is 30MB, I can't post it here. – tuskiomi Nov 10 '20 at 19:02
  • @tuskiomi: or just `sudo lsof |& grep query` in Bash. – Arkadiusz Drabczyk Nov 10 '20 at 19:04
  • 1
    @ArkadiuszDrabczyk, ...note that that construct is listed in https://wiki.bash-hackers.org/scripting/obsolete -- granted, there's some opinion that goes into the curation of that page, but it tends to reflect a certain level of consensus. – Charles Duffy Nov 10 '20 at 19:04

0 Answers0