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,117 results
ls foo*.jpg | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2' | /bin/sh
svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{print $2}' | sort | uniq -c | sort -r
2009-07-29 02:10:45
User: askedrelic
Functions: awk egrep ls sort uniq xargs
Tags: svn count
16

I'm working in a group project currently and annoyed at the lack of output by my teammates. Wanting hard metrics of how awesome I am and how awesome they aren't, I wrote this command up.

It will print a full repository listing of all files, remove the directories which confuse blame, run svn blame on each individual file, and tally the resulting line counts. It seems quite slow, depending on your repository location, because blame must hit the server for each individual file. You can remove the -R on the first part to print out the tallies for just the current directory.

ifconfig | awk -F':| +' '/ddr:/{print $4}'
2009-07-25 22:51:08
User: 0x89
Functions: awk ifconfig
Tags: Linux ifconfig
1

and, a lot uglier, with sed:

ifconfig | sed -n '/inet addr:/s/[^:]\+:\(\S\+\).*/\1/p'

Edit:

Wanted to be shorter than the perl version. Still think that the perl version is the best..

cat /var/log/secure.log | awk '{print substr($0,0,12)}' | uniq -c | sort -nr | awk '{printf("\n%s ",$0) ; for (i = 0; i<$1 ; i++) {printf("*")};}'
2009-07-24 07:20:06
User: knassery
Functions: awk cat sort uniq
15

Busiest seconds:

cat /var/log/secure.log | awk '{print substr($0,0,15)}' | uniq -c | sort -nr | awk '{printf("\n%s ",$0) ; for (i = 0; i<$1 ; i++) {printf("*")};}'
awk '{print NR": "$0; for(i=1;i<=NF;++i)print "\t"i": "$i}'
2009-07-23 06:25:31
User: recursiverse
Functions: awk
Tags: awk
16

Breaks down and numbers each line and it's fields. This is really useful when you are going to parse something with awk but aren't sure exactly where to start.

$ awk '{ split(sprintf("%1.3e", $1), b, "e"); p = substr("yzafpnum_kMGTPEZY", (b[2]/3)+9, 1); o = sprintf("%f", b[1] * (10 ^ (b[2]%3))); gsub(/\./, p, o); print substr( gensub(/_[[:digit:]]*/, "", "g", o), 1, 4); }' < test.dat
2009-07-22 16:54:14
User: mungewell
Functions: awk
Tags: awk
2

converts any number on the 'stdin' to SI notation. My version limits to 3 digits of precious (working with 10% resistors).

curl -u 'username' https://api.del.icio.us/v1/posts/all | sed 's/^.*href=//g;s/>.*$//g;s/"//g' | awk '{print $1}' | grep 'http'
2009-07-22 07:32:59
User: bubo
Functions: awk grep sed
2

a variation of avi4now's command - thanks by the way!

/sbin/ifconfig | awk -F'[ :]+' '/inet addr/{print $4}'
2009-07-21 14:18:17
User: inof
Functions: awk
-1

That one works on Linux. On BSD and Solaris, the ifconfig output is much easier to parse:

/sbin/ifconfig -a | awk '/inet/{print $2}'
cat `whereis mysqlbug | awk '{print $2}'` | grep 'CONFIGURE_LINE='
ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill
2009-07-20 10:06:32
User: buffer
Functions: awk grep ps xargs
-4

Well this can come handy , when you don't feel like playing with pid rather if you know

the process name say "firefox",it would kill it.The script given below would kill the process with its name given as first parameter , though not robust enough to notify that process doesn't exist , well if you know what you are doing that's wouldn't be a problem.:)

----

killhim.sh

----

#!/bin/bash

ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill

----

vmstat 1 | awk '{now=strftime("%Y-%m-%d %T "); print now $0}'
2009-07-17 07:41:57
Functions: awk vmstat
8

Also useful with iostat, or pretty much anything else you want timestamped.

grep -rc logged_in app/ | cut -d : -f 2 | awk '{sum+=$1} END {print sum}'
2009-07-15 14:16:44
User: terceiro
Functions: awk cut grep
-2

grep's -c outputs how may matches there are for a given file as "file:N", cut takes the N's and awk does the sum.

tar cvfz changes.tar.gz --exclude-vcs `svn diff -rM:N --summarize . | grep . | awk '{print $2}' | grep -E -v '^\.$'`
2009-07-13 19:39:10
User: jaysee
Functions: awk diff grep tar
2

Handy when you need to create a list of files to be updated when subversion is not available on the remote host. You can take this tar file, and upload and extract it where you need it. Replace M and N with the revisions specific to yours. Make sure you do this from an updated (svn up) working directory.

cat /dev/urandom|awk 'BEGIN{"tput cuu1" | getline CursorUp; "tput clear" | getline Clear; printf Clear}{num+=1;printf CursorUp; print num}'
2009-07-13 07:30:51
User: axelabs
Functions: awk cat printf
Tags: nawk awk clear tput
0

awk can clear the screen while displaying output. This is a handy way of seeing how many lines a tail -f has hit or see how many files find has found. On solaris, you may have to use 'nawk' and your machine needs 'tput'

lsof |awk ' {if ( $0 ~ /home/) print substr($0, index($0,"/home") ) }'|cut -d / -f 1-4|sort|uniq -c|sort -bgr
while true; do beep -l66 -f`head -c2 /dev/input/mice|hexdump -d|awk 'NR==1{print $2%10000}'`; done
2009-07-11 12:01:27
User: 5z474n
Functions: awk hexdump
4

Beeps on mouse's every move. Bear in mind that, at least on Ubuntu, /dev/input/mice can be read only by root.

tokill=`ps -fea|grep process|awk '{ printf $2" "}'`; kill -9 $tokill;
unzip -lt foo.zip | grep testing | awk '{print $2}' | xargs rm -r
awk '{c=split($0, s); for(n=1; n<=c; ++n) print s[n] }' INPUT_FILE > OUTPUT_FILE
2009-07-06 06:10:21
User: agony
Functions: awk
Tags: awk
0

Basically it creates a typical word list file from any normal text.

S=`pidof skype`;grep heap /proc/$S/maps|cut -f1 -d' '|awk -F- '{print "0x" $1 " 0x" $2}'|xargs echo "du me t ">l;gdb -batch -p $S -x l>/dev/null 2>&1;strings t|grep \(smirk|head -n1
2009-06-26 20:03:17
User: alvieboy
Functions: awk cut echo grep head xargs
0

Skype has an internal regex which depicts the emoticons it supports. However you cannot simply search the binary file for it. This small 181 character line will do just that, provided skype is running. And of course, only works in linux.

awk '/regex/{print x};{x=$0}'
2009-06-26 19:29:54
User: res0nat0r
Functions: awk
4

Use this if you don't have access to GNU grep's -B option.

echo "${STRING}" | tr '[A-Z]' '[a-z]' | awk '{print toupper(substr($0,1,1))substr($0,2);}'
2009-06-23 21:11:34
User: mohan43u
Functions: awk echo tr
Tags: awk tr
0

Helpful when we want to do mass file renaming(especially mp3s).

sed -e "s/\[{/\n/g" -e "s/}, {/\n/g" sessionstore.js | grep url | awk -F"," '{ print $1 }'| sed -e "s/url:\"\([^\"]*\)\"/\1/g" -e "/^about:blank/d" > session_urls.txt
2009-06-14 15:08:31
User: birnam
Functions: awk grep sed
2

This will extract all of the urls from a firefox session (including urls in a tab's history). The sessionstore.js file is in ~/.mozilla/firefox/{firefox profile}

w3m -dump_head www.fiat.com | awk '/Date+/{print $6, $7}'
2009-06-08 00:06:01
User: m33600
Functions: awk
-1

w3m is a commanline web browser, full of options, I used -dump_head for less unnecessary page download.

With awk, I can retrieve dynamic changes in webpages in this very econnomical fashion

awk '{print}' | espeak -v pt -stdin
2009-06-07 20:45:44
User: m33600
Functions: awk
0

Speaks whatever comes in via stdin (-v pt = portuguese, default = englisg)

..speech part of keyboard event talker. Made to accomplish a simple alarm central based on a cheap keyboard circuit. This way I have one zone per direct keypress. Depends on keypress.sh. It speaks out loud wich zone (key) has been pressed ( faulted ). Here is keypress.sh :

#!/bin/bash

# keypress.sh: Detect a user keypress ("hot keys").

echo

old_tty_settings=$(stty -g) # Save old settings (why?).

stty -icanon

Keypress=$(head -c1) # or $(dd bs=1 count=1 2> /dev/null)

# on non-GNU systems

aplay -q /home/mm/bash/beep-1.wav

echo

echo "Chamada quarto \""$Keypress"\"."

echo

stty "$old_tty_settings" # Restore old settings.

# Thanks, Stephane Chazelas.

exit 0