Hide

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using netstat from sorted by
Terminal - Commands using netstat - 115 results
netstat -town
netstat -tn | awk '($4 ~ /:22\s*/) && ($6 ~ /^EST/) {print substr($5, 0, index($5,":"))}'
netstat -an | grep 80 | wc -l
netstat -t -p --extend | grep USERNAME
2012-08-07 02:25:54
User: wr8cr8
Functions: grep netstat
-2

This obtains a list of open connections that a user is connected to if he/she is using a SSH tunnel

netstat -ie
netstat -tn | grep :80 | awk '{print $5}'| grep -v ':80' | cut -f1 -d: |cut -f1,2,3 -d. | sort | uniq -c| sort -n
2012-06-26 08:29:37
User: krishnan
Functions: awk cut grep netstat sort uniq
0

cut -f1,2 - IP range 16

cut -f1,2,3 - IP range 24

cut -f1,2,3,4 - IP range 24

netstat-nat -n
2012-05-29 13:12:06
User: slezhuk
Functions: netstat
0

Show state of NAT, readed from '/proc/net/ip_conntrack' or '/proc/net/nf_conntrack'

netstat -Aan | grep .80 | grep -v 127.0.0.1 | grep EST | awk '{print $6}' | cut -d "." -f1,2,3,4 | sort | uniq
2012-02-03 13:54:11
Functions: awk cut grep netstat sort
0

See who is using a specific port. Especially when you're using AIX. In Ubuntu, for example, this can easily be seen with the netstat command.

netstat -tan | awk '$1 == "tcp" && $4 ~ /:/ { port=$4; sub(/^[^:]+:/, "", port); used[int(port)] = 1; } END { for (p = 32768; p <= 61000; ++p) if (! (p in used)) { print p; exit(0); }; exit(1); }'
2012-01-26 20:42:56
User: wirawan0
Functions: awk netstat
0

This is also perl-less, and only uses AWK as its postprocessor. Tested with GAWK and MAWK.

netstat -a --numeric-ports | grep 8321
2012-01-20 22:00:56
User: peter4512
Functions: grep netstat
Tags: net tcp
1

if you don't do --numeric-ports, netstat will try to resolve them to names

netstat -plantu
netstat -anpe
2011-12-06 18:27:09
User: anarcat
Functions: netstat
1

Lists all opened sockets (not only listeners), no DNS resolution (so it's fast), the process id and the user holding the socket.

Previous samples were limiting to TCP too, this also lists UDP listeners.

netstat -atn | grep :22 | grep ESTABLISHED | awk '{print $4}' | sed 's/:22//'
netstat -plntu
netstat -plnt
2011-09-30 19:56:32
User: DopeGhoti
Functions: netstat
7

While `lsof` will work, why not use the tool designed explicitly for this job?

(If not run as root, you will only see the names of PID you own)

while true ; do sleep 1 ; clear ; (netstat -tn | grep -P ':36089\s+\d') ; done
2011-09-28 11:39:43
User: hute37
Functions: clear grep netstat sleep true
-3

shell loop to scan netstat output avoiding loolback aliases (local/remote swap for local connections)

netstat -rn | convert label:@- netstat.png
2011-09-21 11:52:28
User: appanax
Functions: netstat
0

We use the "convert" command (ImageMagick package) : see man convert (http://www.ma.utexas.edu/cgi-bin/man-cgi?convert+1)

netstat -tn | awk 'NR>2 {print $6}' | sort | uniq -c | sort -rn
sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep udp && echo && sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep tcp
netstat -an |grep ":80" |awk '{print $5}' | sed s/::ffff://g | cut -d: -f1 |sort |uniq -c |sort -n | tail -1000 | grep -v "0.0.0.0"
netstat -nut | sed '/ESTABLISHED/!d;s/.*[\t ]\+\(.*\):.*/\1/' | sort -u
netstat -nut | awk '$NF=="ESTABLISHED" {print $5}' | cut -d: -f1 | sort -u
2011-07-27 07:24:10
User: bitbasher
Functions: awk cut netstat sort
-1

find all computer connected to my host through TCP connection

netstat -lantp | grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -u
2011-07-21 21:23:10
User: bitbasher
Functions: awk grep netstat sort
16

find all computer connected to my host through TCP connection.

netstat -nt | awk -F":" '{print $2}' | sort | uniq -c
netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
2011-07-04 20:23:21
User: letterj
Functions: awk cut netstat sort uniq
Tags: netstat
-4

netstat has two lines of headers:

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

Added a filter in the awk command to remove them