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 awk from sorted by
Terminal - Commands using awk - 1,112 results
echo 0$(awk '/Pss/ {printf "+"$2}' /proc/$PID/smaps)|bc
2013-09-26 18:20:22
User: atoponce
Functions: awk echo
Tags: Linux awk echo bc proc
5

The "proportional set size" is probably the closest representation of how much active memory a process is using in the Linux virtual memory stack. This number should also closely represent the %mem found in ps(1), htop(1), and other utilities.

grep "10/Sep/2013" access.log| cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":"$3}' | sort -nk1 -nk2 | uniq -c | awk '{ if ($1 > 10) print $0}'
netstat -ntu | awk ' $5 ~ /^(::ffff:|[0-9|])/ { gsub("::ffff:","",$5); print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
2013-09-10 19:28:06
User: mrwulf
Functions: awk cut netstat sort uniq
1

Same as the rest, but handle IPv6 short IPs. Also, sort in the order that you're probably looking for.

function garg () { tail -n 1 ${HISTFILE} | awk "{ print \$$1 }" }
2013-09-10 04:07:46
User: plasticphyte
Functions: awk tail
0

This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.

I wrote this after getting irritated with having to continually type in long paths/arguments.

You could also use $_ if all you want is the last argument.

for I in $(awk -v LIMIT=500 -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd | cut -f 1-1 -d ':' | xargs); do usermod -g YOURGROUP $I ; done
host example.com | head -1 | awk '{print $4}'
nslookup www.example.com | tail -2 | head -1 | awk '{print $2}'
2013-09-05 20:26:45
User: wsams
Functions: awk head nslookup tail
1

I'm not sure how reliable this command is, but it works for my needs. Here's also a variant using grep.

nslookup www.example.com | grep "^Address: " | awk '{print $2}'

for i in `ip addr show dev eth1 | grep inet | awk '{print $2}' | cut -d/ -f1`; do echo -n $i; echo -en '\t'; host $i | awk '{print $5}'; done
apt-cache -n dumpavail | grep 'Package:' | awk '{print $2 }'
ipcs -q | grep foo | awk '{print $2}' | xargs -I ipcid ipcrm -q ipcid
brew cleanup -n | awk '{print $3}' | xargs du -s | awk '{s+=$1} END {print s}'
curl -s poncho.is/forecast/new_york/today/ | grep -E 'og:title|og:description' | cut -d\" -f4 | awk '{print $0,"<p>"}' | lynx -stdin -dump
tail -n 1000 access.log | grep "200 POST" | awk '{print substr($3,0,9)}' | awk '{data[$0]++}END{for(item in data){print item, data[item]}}'
du -Lsbc * |awk 'function hr(bytes){hum[1024**4]="TiB";hum[1024**3]="GiB";hum[1024**2]="MiB";hum[1024]="kiB";for(x=1024**4;x>=1024;x/=1024){if(bytes>=x){return sprintf("%8.3f %s",bytes/x,hum[x]);}}return sprintf("%4d B",bytes);}{print hr($1) "\t" $2}'
ip addr show |grep -w inet | grep -v 127.0.0.1 | awk '{ print $2}'| cut -d "/" -f 2
2013-08-17 18:54:23
User: htmlgifted
Functions: awk cut grep
0

This command give you Just the Network Cidr Notation

output=$(ps -ef|grep -i java 2>/dev/null); for w in ${output[@]}; do if [[ $w =~ .*Xmx.* ]]; then result=$(grep -oP "[0-9]+" <<< $w); echo $result ;fi ; done| awk 'BEGIN {sum=0} {for(i=1; i<=NF; i++) sum+=$i } END {print sum}'
2013-08-13 14:04:17
User: massiverobot
Functions: awk echo grep ps
Tags: java jvm xmx
0

This will find all the -Xmx[BIGINT] running on a system, add them up for you and give you the total.

ipcs -s | grep apache | awk ' { print $2 } ' | xargs ipcrm sem
2013-08-12 16:29:32
User: kernel01
Functions: awk grep ipcrm ipcs xargs
0

Solves these pesky errors you see in the Apache log:

[Fri Jun 28 17:51:00 2013] [emerg] (28)No space left on device: Couldn't create accept lock (/monsoon/opt/apache2/logs/accept.lock.356) (5)

Naturally, can be used to get rid of other semaphores. Note: change the apache user in accordance to your ENV.

for i in `find . -name "*.jar"`; do jar -tvf $i | grep -v /$ | awk -v file=$i '{print file ":" $8}'; done > all_jars.txt
sudo tune2fs -l $(df -h / |(read; awk '{print $1; exit}')) | grep -i created
2013-08-08 15:18:09
User: thechile
Functions: awk df grep sudo tune2fs
12

..not guaranteed to always be accurate but fun to see how old you Linux installation is based on the root partitions file system creation date.

ip addr list | grep global | awk '{print $7"\t"$2}'
randchannelurl=$(lynx -dump http://www.tvcatchup.com/channels.html | grep watch | sed 's/^......//'| awk 'BEGIN { srand() } int(rand() * NR) == 0 { x = $0 } END { print x }') && firefox -new-window $randchannelurl
2013-08-01 10:38:10
User: dunryc
Functions: awk grep sed watch
0

becuase im lazy and cant be bothered looking at the tv guide to choose a channel , any improvments or comments appreciated

ifconfig | egrep [0-9A-Za-z]{2}\(:[0-9A-Za-z]{2}\){5} | awk '{print $1 ":\t" $5}'
2013-07-30 17:02:07
User: jaimeanrm
Functions: awk egrep ifconfig
1

Is the better option on a Open SuSE Box

curl -sL http://www.dell.com/support/troubleshooting/us/en/555/Servicetag/$(dmidecode -s system-serial-number) | html2text -style pretty | awk -F\. '/with an end date of/ { print $1 "."}'
2013-07-30 14:46:12
User: mhollick
Functions: awk date
1

pretty much the same.

I use awk rather than grep and perl.

It looks like the URL has been updated.

The service tag can also be retrieved via snmp - potential for a for loop over a list of servers. I might have a look into doing an example.

for m in `df -P | awk -F ' ' '{print $NF}' | sed -e "1d"`;do n=`df -P | grep "$m$" | awk -F ' ' '{print $5}' | cut -d% -f1`;i=0;if [[ $n =~ ^-?[0-9]+$ ]];then printf '%-25s' $m;while [ $i -lt $n ];do echo -n '=';let "i=$i+1";done;echo " $n";fi;done
2013-07-29 20:12:39
User: drockney
Functions: awk cut echo grep printf sed
Tags: bash
5

Automatically drops mount points that have non-numeric sizes (e.g. /proc). Tested in bash on Linux and AIX.

while read X ; do printf "$X --"; virsh dumpxml $X | egrep "source dev|source file"; done< <(virsh list | awk '$1 ~ /^[1-9]/ { print $2 }')
2013-07-29 17:32:59
User: hugme
Functions: awk egrep printf read
0

This will strip out the relivent disk information from kvm. I'm using it to find disks on a SAN which are no longer in use.