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,116 results
vim `git status | grep modified | awk '{print $3}'`
2012-11-19 09:48:46
User: TetsuyO
Functions: awk grep vim
Tags: vim git
0

This oneliner gets all the 'modified' files in your git repository, and opens all of them in vim.

Very handy when you're starting to work in the morning and you simply want to review your modified files before committing them.

Maybe there are better ways to do that (and maybe integrated in vim and/or git, who knows), but I found quicker to do this oneliner.

awk '/^md/ {printf "%s: ", $1}; /blocks/ {print $NF}' </proc/mdstat
find . -type f -print | awk -F'.' '{print $NF}' | sort | uniq -c
identify path/to/image.jpg | awk '{print $3;}'
for k in `git branch -r|awk '{print $1}'`;do echo -e `git show --pretty=format:"%Cgreen%ci_%C(blue)%c r_%Cred%cn_%Creset" $k|head -n 1`$k;done|sort -r|awk -F"_" '{printf("%s %17s %-22s %s\n",$1,$2,$3,$4)}'
echo /home/foo/dir1/bar | awk -F/ -v OFS=/ '{$3 = "dir2"}1'
2012-10-26 16:17:20
User: zlemini
Functions: awk echo
1

Awk replaces the value of a specific field while retaining the field separator "/" .

ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -u -f {}|awk '/^[0-9]/&&!/^12:00:01|RESTART|CPU/{print "%user: "$4" %system: "$6" %iowait: "$7" %nice: "$5" %idle: "$9}'|sort -nk10|head
awk '{word=(substr($0,index($0,"blah"),5));print word}'
for i in $(ps -eo pid|grep -v PID);do echo ""; echo -n "==$i== ";awk '/^read|^write/{ORS=" "; print}' /proc/$i/io 2>/dev/null; echo -n " ==$i=="; done|sort -nrk5|awk '{printf "%s\n%s %s\n%s %s\n%s\n\n",$1,$2,$3,$4,$5,$6}'
ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -q -f {}| awk '/Average/'|awk '{runq+=$2;plist+=$3}END{print "average runq-sz:",runq/NR; print "average plist-sz: "plist/NR}'
for i in `ls /var/log/sa/|grep -E "sa[0-9][0-9]"`;do echo -ne "$i -- ";sar -r -f /var/log/sa/$i|awk '{ printf "%3.2f\n",($4-$6-$7)*100/(3+$4)}'|grep -Eiv "average|linux|^ --|0.00|^-" |awk '{sum+=$1 }END{printf "Average = %3.2f%%\n",sum/NR}';done
echo "abcdefg"|awk 'BEGIN {FS="''"} {print $2}'
/sbin/ifconfig |awk '/bond|eth/{getline i;printf $0" ";printf "%s\n", i" "}'|awk '{print $1,substr($7,6)}'
echo 1268727836 | awk '{print strftime("%c",$1)}'
awk '{print NR "\t" $0}'
ompload() { wget -O- - "$1" --quiet|curl -# -F file1=@- http://ompldr.org/upload|awk '/Info:|File:|Thumbnail:|BBCode:/{gsub(/<[^<]*?\/?>/,"");$1=$1;print}';}
2012-10-19 22:54:55
User: dzup
Functions: awk wget
-2

Like i said, i havent test it yet, all becouse my internet its soo slow, if you try and works please share, also be nice to do it using the direct url link.

a=$(stty -a -F /dev/tty1| awk -F'[ ;]' '/columns/ { print $9 }'); fold -w$a /dev/vcs1;echo
export proc=chrome && ps aux | grep $proc | grep -v grep |awk '{print $2}'
tshark -qr [cap] -z conv,tcp | awk '{printf("%s:%s:%s\n",$1,$3,$10)}' | awk -F: '{printf("%s %s %s\n",$1,$3,substr($5,1,length($5)-10))}' | sort | uniq -c | sort -nr
for P in $(mount | awk '/type nfs / {print $3;}'); do echo $P; sudo umount $P && sudo mount $P && echo "ok :)"; done
2012-10-14 22:39:58
User: amatix
Functions: awk echo mount sudo umount
Tags: Linux mount nfs
0

`mount -o remount` doesn't pick up new NFS options (eg. timeo, soft, retrans, etc) so you need to do a full mount/remount cycle. This one-liner makes it quick and easy :) Update your fstab with the new options, then run it.

netstat -tn | awk '($4 ~ /:22\s*/) && ($6 ~ /^EST/) {print substr($5, 0, index($5,":"))}'
NUMCPUS=`grep ^proc /proc/cpuinfo | wc -l`; FIRST=`cat /proc/stat | awk '/^cpu / {print $5}'`; sleep 1; SECOND=`cat /proc/stat | awk '/^cpu / {print $5}'`; USED=`echo 2 k 100 $SECOND $FIRST - $NUMCPUS / - p | dc`; echo ${USED}% CPU Usage
2012-10-02 03:57:51
User: toxick
Functions: awk echo sleep wc
0

Using the output of 'ps' to determine CPU usage is misleading, as the CPU column in 'ps' shows CPU usage per process over the entire lifetime of the process. In order to get *current* CPU usage (without scraping a top screen) you need to pull some numbers from /proc/stat. Here, we take two readings, once second apart, determine how much IDLE time was spent across all CPUs, divide by the number of CPUs, and then subtract from 100 to get non-idle time.

find . \( -iname '*.cpp' -o -iname '*.h' \) -exec wc -l {} \; | sort -n | cut --delimiter=. -f 1 | awk '{s+=$1} END {print s}'
2012-09-19 15:21:01
User: jecxjoopenid
Functions: awk cut find sort wc
0

Searches for *.cpp and *.h in directory structure, counts the number of lines for each matching file and adds the counts together.

awk '{a[$3]++} END {for(i in a) print i}' /var/log/squid/access.log
for file in `svn st | awk '{print $2}'`; do svn revert $file; done