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 tagged awk from sorted by
Terminal - Commands tagged awk - 282 results
awk 'FNR==100 {print;exit}' file
2012-03-04 20:25:57
User: Testuser_01
Functions: awk
Tags: awk time LINES
0

This will save parsing time for operations on very big files.

awk '{cmd="date --date=\""$1"\" +\"%Y/%m/%d %H:%M:%S\" "; cmd | getline convdate; print cmd";"convdate }' file.txt
2012-02-28 14:08:52
User: EBAH
Functions: awk
0

Convert readable date/time with `date` command

print "$(lsvg -Lo |xargs lsvg -L|grep "TOTAL PPs"|awk -F"(" '{print$2}'|sed -e "s/)//g" -e "s/megabytes/+/g"|xargs|sed -e "s/^/(/g" -e "s/+$/)\/1000/g"|bc ) GB"
2012-02-03 13:58:41
0

Not figured by me, but a colleague of mine.

See the total amount of data on an AIX machine.

sed -r 's/(\[|])//g' | awk ' { $1=strftime("%D %T",$1); print }'
2012-02-03 13:07:37
User: Zulu
Functions: awk sed
Tags: sed awk timestamp
0

It remove the square bracket and convert UNIX time to human readable time for all line of a stream (or file).

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
awk -F":" '!list[$3]++{print $3}' /etc/passwd
find ./ -type f -size +100000k -exec ls -lh {} \; 2>/dev/null| awk '{ print $8 " : " $5}'
2012-01-21 04:19:35
User: Goez
Functions: awk find ls
0

This command does a basic find with size. It also improves the printout given (more clearer then default)

Adjusting the ./ will alter the path.

Adjusting the "-size +100000k" will specify the size to search for.

grep "cpu " /proc/stat | awk -F ' ' '{total = $2 + $3 + $4 + $5} END {print "idle \t used\n" $5*100/total "% " $2*100/total "%"}'
2012-01-21 04:12:50
User: Goez
Functions: awk grep
0

This command displays the CPU idle + used time using stats from /proc/stat.

diff -U99999 original.css modified.css | awk '/^-/{next} {f=f"\n"$0} /^\+.*[^ ]/{yes=1} /}/ {if(yes){print f} f="";yes=0}'
2012-01-12 07:57:22
User: unhammer
Functions: awk diff
0

This will extract the differing CSS entries of two files. I've left the initial character (plus or space) in output to show the real differing line, remove the initial character to get a working CSS file. The output CSS file is usable by either adding it in a below the to original.css, or by only using the output but adding @import url("original.css"); in the beginning.

This is very useful for converting Wordpress theme copies into real Wordpress child themes.

Could exclude common lines within entries too, I guess, but that might not be worth the complexity.

ps -ef | grep [j]ava | awk -F ' ' ' { print $1," ",$2,"\t",$(NF-2),"\t",$(NF-1),"\t",$NF } ' | sort -k4
2012-01-05 16:05:48
User: drockney
Functions: awk grep ps sort
Tags: sort awk grep ps
0

Tested in bash on AIX & Linux, used for WAS versions 6.0 & up. Sorts by node name.

Useful when you have vertically-stacked instances of WAS/Portal. Cuts out all the classpath/optional parameter clutter that makes a simple "ps -ef | grep java" so difficult to sort through.

awk -F: '$3 > 999 { print $1 }' /etc/passwd
2011-12-30 14:47:10
User: rockenrola
Functions: awk
Tags: awk uid passwd
0

To distinguish normal users from system users. Specify an UID, to list all all users with UID bigger than that in /etc/passwd.

awk -F ':' '{print $1 | "sort";}' /etc/passwd
ls -1 | awk ' { print "zip "$1".zip " $1 } ' | sh
2011-12-14 20:30:56
User: kaywhydub
Functions: awk ls
Tags: awk zip sh
1

This will list the files in a directory, then zip each one with the original filename individually.

video1.wmv -> video1.zip

video2.wmv -> video2.zip

This was for zipping up large amounts of video files for upload on a Windows machine.

awk 'NR >= 3 && NR <= 6' /path/to/file
2011-12-14 14:28:56
User: atoponce
Functions: awk
Tags: awk
6

This command uses awk(1) to print all lines between two known line numbers in a file. Useful for seeing output in a log file, where the line numbers are known. The above command will print all lines between, and including, lines 3 and 6.

find -iname "*.pdf" -exec pdfinfo -meta {} \;|awk '{if($1=="Pages:"){s+=$2}}END{print s}'
2011-12-13 15:02:11
User: Barabbas
Functions: awk find
Tags: awk find pdf count sum
0

This sums up the page count of multiple pdf files without the useless use of grep and sed which other commandlinefus use.

ifconfig | awk '/inet / {sub(/addr:/, "", $2); print $2}'
svn log -v -r{2009-11-1}:HEAD | awk '/^r[0-9]+ / {user=$3} /./{if (user=="george") {print}}' | grep -E "^ M|^ G|^ A|^ D|^ C|^ U" | awk '{print $2}' | sort | uniq
2011-12-05 07:36:44
User: smilyface
Functions: awk grep sort
Tags: svn awk log
0

svn log -v --> takes log of all

Filter1

--------

-r {from}{to} --> gives from and to revision

Filter2

--------

awk of line 'r'with numbers

Assign user=3rd column [ie; username]

Filter3

--------

if username = George

print details

Filter4

--------

Print lines starts with M/U/G/C/A/D

[* A Added * D Deleted * U Updated * G Merged * C Conflicted]

Filter5

--------

sort all files

Filter6

-------

Print only uniq file's name alone.

awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display
2011-12-04 01:56:44
Functions: awk printf
19

Parses /etc/group to "dot" format and pases it to "display" (imagemagick) to show a usefull diagram of users and groups (don't show empty groups).

lsmod | awk 'BEGIN{print "digraph{"}{split($4, a, ","); for (i in a) print $1, "->", a[i]}END{print "}"}'|display
2011-12-04 01:41:23
Functions: awk lsmod
15

parse "lsmod" output to "dot" format and pass it to "display". Without perl!

human_filesize() { awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } '}
2011-12-02 18:21:20
User: ArtBIT
Functions: awk printf
0

Converts a number of bytes provided as input, to a human readable number.

while true; do cat /proc/net/dev; sleep 1; done | awk -v dc="date \"+%T\"" '/eth0/{i = $2 - oi; o = $10 - oo; oi = $2; oo = $10; dc|getline d; close(dc); if (a++) printf "%s %8.2f KiB/s in %8.2f KiB/s out\n", d, i/1024, o/1024}'
2011-11-21 05:24:44
Functions: awk cat printf sleep
Tags: awk sh
9

poorman's ifstat using just sh and awk. You must change "eth0" with your interface's name.

ps -p pid -o logname |tail -1
git rev-list --reverse HEAD | awk "/$(git log -n 1 --pretty="format:%h")/ {print NR}"
2011-11-15 21:49:32
User: atoponce
Functions: awk
Tags: git awk
-1

Git uses secure hash sums for its revision numbers. I'm sure this is fine and dandy for ultra-secure computing, but it's less than optimal for humans. Thus, this will give you sequential revision numbers in Git all the way from the first commit.

/usr/bin/snmpwalk -v 1 -c public 10.0.0.22 1.3.6.1.4.1.789.1.21.1.2.1.25.1| awk -F : '{print "netapp temp:" $4 }'
2011-11-10 01:04:00
User: ndhail
Functions: awk
Tags: awk snmp netapp
0

if you can use this depends on which netapp you have, this netapp is an FAS2020

svn list -vR svn://server/repo/somedir | awk '{if ($3 !="") sum+=$3; i++} END {print "\ntotal size= " sum/1024000" MB" "\nnumber of files= " i/1000 " K"}'
2011-11-09 00:52:22
User: ndhail
Functions: awk
Tags: svn awk
1

afaik, svn doesn't have a good, scriptable way of telling you these two basic pieces of information.