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,150 results
ls -lct /etc | tail -1 | awk '{print $6, $7}'
2009-09-03 10:26:37
User: MrMerry
Functions: awk ls tail
10

Show time and date when you installed your OS.

echo sortmeplease | awk '{l=split($1,a,"");asort(a);while(x<=l){printf "%s",a[x];x++ }print "";}'
2009-09-03 10:22:39
User: foob4r
Functions: awk echo
-1

using awk

missed the last char thanks @Josay

nmap -R -sL 209.85.229.99/27 | awk '{if($3=="not")print"("$2") no PTR";else print$3" is "$2}' | grep '('
2009-09-02 16:33:15
User: netsaint
Functions: awk grep
Tags: nmap dns
4

This command uses nmap to perform reverse DNS lookups on a subnet. It produces a list of IP addresses with the corresponding PTR record for a given subnet. You can enter the subnet in CDIR notation (i.e. /24 for a Class C)). You could add "--dns-servers x.x.x.x" after the "-sL" if you need the lookups to be performed on a specific DNS server.

On some installations nmap needs sudo I believe. Also I hope awk is standard on most distros.

awk '/d.[0-9]/{print $4}' /proc/partitions
2009-09-02 15:26:03
User: akg240
Functions: awk
3

Only one command and not dependant on full read access to the devices.

fdisk -l |grep -e '^/' |awk '{print $1}'|sed -e "s|/dev/||g"
free -m | awk '/Swap/ {print $4}'
2009-09-02 11:46:17
User: voyeg3r
Functions: awk free
-2

simple way to show free swap

awk '{print substr($0, index($0,$N))}'
2009-08-31 19:47:10
User: mstoecker
Functions: awk
0

This command will print all fields from the given input to the end of each line, starting with the Nth field.

awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -rnk1 | head -n 10
curl -s wap.kitco.com/exrate.wml | awk ' BEGIN { x=0; FS = "<" } { if ($0~"^<br/>") {x=0} if (x==1) {print $1} if ($0~"EUR/US") {x=1} }'
2009-08-27 16:30:19
User: jamaese
Functions: awk
4

You can get others rates changing the "EUR/US" part. look at the url: wap.kitco.com/exrate.wml to get more options.

cat /service/solr/log/main/current | tai64nlocal | grep "\(`date '+%F %H'`\|`date '+%F %H %M' | awk '{print $1" 0"$2-1":"$3}'`\)" | grep QTime | awk '{print $NF}' | awk -F\= '{ s += $2} END {print s/NR}'
echo 1234567890 | awk '{ print strftime("%c", $0); }'
2009-08-25 09:37:54
User: alvinx
Functions: awk echo
7

- convert unixtime to human-readable with awk

- useful to read logfiles with unix-timestamps, f.e. squid-log:

sudo tail -f /var/log/squid3/access.log | awk '{ print strftime("%c ", $1) $0; }

echo `date +%m/%d/%y%X |awk '{print $1;}' `" => "` cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2, $3;}'` >> datetmp.log
2009-08-24 21:26:29
User: ninadsp
Functions: awk cat echo
7

Uses the data in the /proc system, provided by the acpid, to find out the CPU temperature. Can be run on systems without lm-sensors installed as well.

find /dir | awk '{print length, $0}' | sort -nr | sed 's/^[[:digit:]]* //' | while read dirfile; do outfile="$(echo "$(basename "$dirfile")" | unaccent UTF-8)"; mv "$dirfile" "$(dirname "$dirfile")/$outfile"; done
2009-08-24 21:24:18
User: Patola
Functions: awk basename find mv read sed sort
2

This command changes all filename and directories within a directory tree to unaccented ones. I had to do this to 'sanitize' some samba-exported trees. The reason it works might seem a little difficult to see at first - it first reverses-sort by pathname length, then it renames only the basename of the path. This way it'll always go in the right order to rename everything.

Some notes:

1. You'll have to have the 'unaccent' command. On Ubuntu, just aptitude install unaccent.

2. In this case, the encoding of the tree was UTF-8 - but you might be using another one, just adjust the command to your encoding.

3. The program might spit a few harmless errors saying the files are the same - not to fear.

awk '!a[$0]++' file
2009-08-23 15:28:43
User: voyeg3r
Functions: awk
9

This create an array 'a' with wole lines. only one occurrence of each line - Not Get lines ++ !

awk '!($0 in a) {a[$0];print}' file
for i in `seq 1 255`; do ping -c 1 10.10.10.$i | tr \\n ' ' | awk '/1 received/ {print $2}'; done
2009-08-22 09:34:27
User: hemanth
Functions: awk ping tr
Tags: ping
2

Ping sweep without NMAP

mailq | grep DrWEB | awk {'print $1'} | sed s/*//g | postsuper -d -
ps -ef | awk '/process-name/ && !/awk/ {print}'
2009-08-19 11:22:09
User: dopeman
Functions: awk ps
1

This does the same thing as many of the 'grep' based alternatives but allows a more finite control over the output. For example if you only wanted the process ID you could change the command:

ps -ef | awk '/mingetty/ && !/awk/ {print $2}'

If you wanted to kill the returned PID's:

ps -ef | awk '/mingetty/ && !/awk/ {print $2}' | xargs -i kill {}
mysql -u<user> -p<password> -s -e 'DESCRIBE <table>' <database> | tail -n +1 | awk '{ printf($1",")}' | head -c -1
awk < file.name '{ system("resolveip -s " $1) }'
2009-08-17 08:09:39
Functions: awk
1

Given a file of FQDN, this simple command resolves the IP addresses of those Useful for log files or anything else that outputs domain names.

FFPID=$(pidof firefox-bin) && lsof -p $FFPID | awk '{ if($7>0) print ($7/1024/1024)" MB -- "$9; }' | grep ".mozilla" | sort -rn
2009-08-16 08:58:22
User: josue
Functions: awk grep pidof sort
6

Check which files are opened by Firefox then sort by largest size (in MB). You can see all files opened by just replacing grep to "/". Useful if you'd like to debug and check which extensions or files are taking too much memory resources in Firefox.

egrep "^lease" /var/lib/dhcp/db/dhcpd.leases |awk '{ print $2 }'
2009-08-14 07:31:00
User: peshay
Functions: awk egrep
0

You can use that to create a excludefile for nmap, to find hosts, with no DHCP lease in your DHCP range.

echo $PATH|awk -F: ' { for (i=1; i <= NF; i++) print $i }'
awk -F ',' '{ x = x + $4 } END { print x }' test.csv
find . -name \*.pdf -exec pdfinfo {} \; | grep Pages | sed -e "s/Pages:\s*//g" | awk '{ sum += $1;} END { print sum; }'