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,163 results
awk '{ total += gsub(/yourstring/,"") } END { print total }' yourfile
2014-12-16 21:00:45
User: bugmenot
Functions: awk
0

Count how many times a pattern is present into a file. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

echo something | awk '{ total += gsub(/yourstring/,"") } END { print total }'
2014-12-16 20:58:42
User: bugmenot
Functions: awk echo
0

Count how many times a pattern is present into a stream. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

ls -l | head -n 65535 | awk '{if (NR > 1) total += $5} END {print total/(1024*1024*1024)}'
ps axo pcpu,args | awk '/[p]hp.*pool/ { sums[$4] += $1 } END { for (pool in sums) { print sums[pool], pool } }' | sort -rn | column -t
mv -iv $FILENAME{,.$(stat -c %y $FILENAME | awk '{print $1}')}
2014-12-01 22:41:38
User: pdxdoughnut
Functions: awk mv stat
Tags: mv
3

Note that the -i will not help in a script. Proper error checking is required.

ps axo pid=,stat= | awk '$2~/^Z/ { print $1 }'
2014-11-26 21:29:33
User: krizzo
Functions: awk ps
0

This prints out a list of all zombie processes PIDs so you can do things like kill the zombies

PID=$(ps -ef | grep processName | grep -v grep | awk '{print $2'}); kill -9 $PID
for i in `lanscan -i | awk '{print $1}'` ; do ifconfig $i ; done 2> /dev/null
netstat -nr|egrep -v "Routing|Interface|lo0"|awk '{print $5}'|sort -u| while read l; do ifconfig $l ; echo " Station Addr: `lanscan -ia|grep "$l "|cut -d ' ' -f 1`" ; done
lsof -ns | grep REG | grep deleted | awk '{s+=$7/1024} END {print s}'
git branch -r | awk -F'/' '{print "git fetch "$1,$2}' | xargs -I {} sh -c {}
smartctl -a /dev/sda |grep Writ |awk '{print $NF/2/1024/1024/1024 " TeraBytes Written"}'
2014-10-21 03:40:32
User: khyron320
Functions: awk grep
2

You must have smartmontools installed for this to work. This also assumes you 512 byte sector sizes, this is pretty standard.

ip a s eth0 | awk -F'[/ ]+' '/inet[^6]/{print $3}'
cat /etc/httpd/logs/access.log | awk '{ print $6}' | sed -e 's/\[//' | awk -F'/' '{print $1}' | sort | uniq -c
2014-10-13 13:39:53
User: suyashjain
Functions: awk cat sed sort uniq
0

The command will read the apache log file and fetch the virtual host requested and the number of requests.

curl -s http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl |grep 'is:' |awk 'BEGIN { FS=">"; } { print $10; }'
2014-10-10 21:17:33
User: toj
Functions: awk grep
Tags: curl BOFH
0

Sure, it's dirty, but it's quick, it only displays the excuse, and it works.

ip addr show enp3s0 | awk '/inet[^6]/{print $2}' | awk -F'/' '{print $1}'
url=`curl http://proxybay.info/ | awk -F'href="|" |">|</' '{for(i=2;i<=NF;i=i+4) print $i,$(i+2)}' | grep follow|sed 's/^.\{19\}//'|shuf -n 1` && firefox $url
2014-10-04 19:08:13
User: dunryc
Functions: awk grep sed
-1

polls the pirate bay mirrors list and chooses a random site and opens it for you in firefox

for i in `cat hosts_list`; do RES=`ssh myusername@${i} "ps -ef " |awk '/[p]rocessname/ {print $2}'`; test "x${RES}" = "x" && echo $i; done
2014-10-03 14:57:54
User: arlequin
Functions: awk echo test
Tags: ssh awk test ps
0

Given a hosts list, ssh one by one and echo its name only if 'processname' is not running.

echo "DISK:";df -Pl | grep -v "Filesystem" | awk '{print $5,$6}' ; echo "MEM:" ; free -mto | awk '{ print $1,$2,$3,$4 }'; echo "CPU:"; top -b -d1 -n1 | grep Cpu | awk '{print $2,$3,$4,$5,$6,$7,$8,$9}';echo "LOAD:"; cat /proc/loadavg
2014-09-29 12:43:52
User: injez
Functions: awk cat df echo free grep top
0

Monitoring system in one line :

DISK : disk space

MEM: memory ( mem , swap, Total)

CPU : all information about cpu activity

LOAD : load average

history|awk '{print $2}'|sort|uniq -c|sort -rn|head -30|awk '!max{max=$1;}{r="";i=s=100*$1/max;while(i-->0)r=r"#";printf "%50s %5d %s %s",$2,$1,r,"\n";}'
2014-09-29 12:40:43
User: injez
Functions: awk head printf sort uniq
0

Top 30 History Command line with histogram display

tcpdump -tnn -c 2000 -i eth0 | awk -F "." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | awk ' $1 > 10 '
2014-09-26 01:15:23
User: hochmeister
Functions: awk sort tcpdump uniq
1

capture 2000 packets and print the top 10 talkers

for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB ; done
find . -name "*.pdf" -print0 | xargs -r0 stat -c %y\ %n | sort|awk '{print $4}'|gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.pdf\n", $0, a++ }' | bash
2014-09-23 06:40:45
Functions: awk find gawk printf stat xargs
Tags: sort awk find xargs
0

Caution: distructive overwrite of filenames

Useful for concatenating pdfs in date order using pdftk

awk '$2 == "/media/KINGSTON" {print $1}' /etc/mtab
docker images | grep '' | awk '{print $3}' | xargs docker rmi