What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands using netstat from sorted by
Terminal - Commands using netstat - 118 results
netstat -n | awk '/^tcp/ {++B[$NF]} END {for(a in B) print a, B[a]}'
netstat -luntp
netstat -nr | awk 'BEGIN {while ($3!="") getline; print $2}'
2010-03-07 01:47:55
User: freethinker
Functions: awk netstat

This is to fetch the Gateway Ip Address of a machine. Use the below format to put the value in a variable if you wish to find the gateway ip in a script

GATEWAY=$(netstat -nr | awk 'BEGIN {while ($3!="") getline; print $2}')
netstat -in
netstat -nptl
2009-12-11 10:23:43
User: sputnick
Functions: netstat

Alternative of OJM snippet :

This one show the IP too, where ports bind. It's very important, because if there's only instead of, connections from internet are rejected.

netstat | awk '/EST/{print $5}' | sort
netstat | grep EST | awk '{print $5}' | sort
2009-11-24 13:38:28
User: unixoid
Functions: awk grep netstat

Sometimes I need a quick visual way to determine if there is a particular server who is opening too many connections to the database machine.

for i in $(netstat --inet -n|grep ESTA|awk '{print $5}'|cut -d: -f1);do geoiplookup $i;done
2009-10-18 20:41:47
Functions: awk cut grep netstat

Sample command to obtain a list of geographic localization for established connections, extracted from netstat. Need geoiplookup command ( part of geoip package under CentOS)

netstat -an|grep -ci "tcp.*established"
2009-10-09 01:08:18
User: romulusnr
Functions: grep netstat

If you want prepend/append text just wrap in echo:

echo Connected: `netstat -an|grep -ci "tcp.*established"`
netstat -ltun
2009-10-05 08:39:03
User: Decoy
Functions: netstat

Works only on Linux.

Last option (n) turn name of service resolving (/etc/services) off.

netstat -anp --tcp --udp | grep LISTEN
netstat -an | grep -Ec '^tcp.+ESTABLISHED$'
netstat -an | awk '$1 ~ /[Tt][Cc][Pp]/ && $NF ~ /ESTABLISHED/{i++}END{print "Connected:\t", i}'
netstat -lantp | grep -i stab | awk -F/ '{print $2}' | sort | uniq
2009-09-19 14:54:31
User: ProMole
Functions: awk grep netstat sort

Show apps that use internet connection at the moment.

Can be used to discover what programms create internet traffic. Skip the part after awk to get more details, though it will not work showing only unique processes.

This version will work with other languages such as Spanish and Portuguese, if the word for "ESTABLISHED" still contain the fragment "STAB"(e.g. "ESTABELECIDO")

netstat -lantp | grep -i establ | awk -F/ '{print $2}' | sort | uniq
netstat -lantp | grep -i establ | awk -F/ '{print $2}' | uniq | sort
2009-09-19 13:54:36
User: ktoso
Functions: awk grep netstat uniq

Can be used to discover what programms create internet traffic. Skip the part after awk to get more details.

Has anyone an idea why the uniq doesn't work propperly here (see sample output)?

netstat -ant | grep :80 | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
while true; do netstat -p |grep "tcp"|grep --color=always "/[a-z]*";sleep 1;done
2009-07-16 04:52:49
User: buffer
Functions: grep netstat

The -p parameter tell the netstat to display the PID and name of the program to which each socket belongs or in digestible terms list the program using the net.Hope you know what pipe symbol means!

Presently we wish to only moniter tcp connections so we ask grep to scan for string tcp, now from the op of grep tcp we further scan for regular expression /[a-z]*.

Wonder what that means ?

If we look at the op of netstat -p we can see that the name of the application is preceded by a / ( try netstat -p ) so,now i assume application name contains only characters a to z (usually this is the case) hope now it makes some sense.Regular expression /[a-z]* means to scan a string that start with a / and contains zero or more characters from the range a-z !!. Foof .. is t

netstat -ntauple
netstat -lnp
kill_daemon() { echo "Daemon?"; read dm; kill -15 $(netstat -atulpe | grep $dm | cut -d '/' -f1 | awk '{print $9}') }; alias kd='kill_daemon
2009-05-26 20:39:56
User: P17

Just find out the daemon with $ netstat -atulpe. Then type in his name and he gets the SIGTERM.

netstat -4tnape
netstat -taupe
2009-05-25 12:46:38
User: farwarx
Functions: netstat

Affiche des infos detaillees sur vos connexions reseaux.

Port en ?coute, protocole, paquets, adresses, ustilisateur, PID etc...

netstat -ntlp | grep -w 80 | awk '{print $7}' | cut -d/ -f1
p=$(netstat -nate 2>/dev/null | awk '/LISTEN/ {gsub (/.*:/, "", $4); if ($4 == "4444") {print $8}}'); for i in $(ls /proc/|grep "^[1-9]"); do [[ $(ls -l /proc/$i/fd/|grep socket|sed -e 's|.*\[\(.*\)\]|\1|'|grep $p) ]] && cat /proc/$i/cmdline && echo; done
2009-04-30 12:39:48
User: j0rn
Functions: awk cat grep ls netstat sed

Ok so it's rellay useless line and I sorry for that, furthermore that's nothing optimized at all...

At the beginning I didn't managed by using netstat -p to print out which process was handling that open port 4444, I realize at the end I was not root and security restrictions applied ;p

It's nevertheless a (good ?) way to see how ps(tree) works, as it acts exactly the same way by reading in /proc

So for a specific port, this line returns the calling command line of every thread that handle the associated socket