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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands using awk from sorted by
Terminal - Commands using awk - 1,207 results
pkg search SEARCH_TERM | awk '{print $NF}' | sed -e 's;.*/\(.*\)\@.*;\1;' | sort -u
awk 'BEGIN {a=1;b=1;for(i=0;i<'${NUM}';i++){print a;c=a+b;a=b;b=c}}'
2009-09-06 03:05:55
User: arcege
Functions: awk
Tags: awk

Does not require input to function or complete. Number of iterations controlled by shell variable $NUM.

ls -lct /etc/ | tail -1 | awk '{print $6, $7, $8}'
2009-09-04 16:52:50
User: peshay
Functions: awk ls tail

shows also time if its the same year or shows year if installed before actual year and also works if /etc is a link (mac os)

find . -type f -name '*.c' -exec wc -l {} \; | awk '{sum+=$1} END {print sum}'
2009-09-04 15:51:30
User: arcege
Functions: awk find wc
Tags: awk find wc

Have wc work on each file then add up the total with awk; get a 43% speed increase on RHEL over using "-exec cat|wc -l" and a 67% increase on my Ubuntu laptop (this is with 10MB of data in 767 files).

find . -exec grep foobar /dev/null {} \; | awk -F: '{print $1}' | xargs vi
grep -ir 'foo' * | awk -F '{print $1}' | xargs vim
grep -ir 'foo' * | awk '{print $1}' | sed -e 's/://' | xargs vim
2009-09-03 15:12:27
User: elubow
Functions: awk grep sed xargs
Tags: vim sed awk grep

This will drop you into vim to edit all files that contain your grep string.

ls -lct /etc | tail -1 | awk '{print $6, $7}'
2009-09-03 10:26:37
User: MrMerry
Functions: awk ls tail

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

using awk

missed the last char thanks @Josay

nmap -R -sL | 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

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

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

simple way to show free swap

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

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

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

- 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

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

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

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

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

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 {}