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

I wanted to write a grep function to show me all listening ports on my host.

I know how to do it by using the -i function in grep :

netstat -a |egrep -i 'listen'

But now I wanted to write it in regex :

netstat -a |egrep 'm/listen/i'

I thought : m because netstat outputs multiple lines and i because it is LISTEN, so I wanted case insensivety.

However this does not give any output. What am I doing wrong ?

share|improve this question
up vote 2 down vote accepted

You could try perl instead:

netstat -an | perl -n -e 'print m/listen/i'

I like the netstat solution. 1 Command done and dusted.

share|improve this answer
this is where I went wrong, thank you :) – Lucas Kauffman Aug 6 '11 at 12:59

You can show all listening ports with:

netstat --protocol=ip -nlp

About your command, grep works line by line. Where did you read this syntax, it seems belong to sed.

share|improve this answer

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.