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 - 293 results
tail -f LOGFILE | awk '{system("say \"" $0 "\"");}'
2011-09-16 06:20:06
User: tamouse
Functions: awk tail
Tags: awk tail say
-1

like #9295, but awkish instead of perlish

grep "HTTP/1.1\" 404" access_log | awk '{print $7 } ' | sort | uniq -c | sort -n
shuf /usr/share/dict/words |grep "^[^']\{3,5\}$" |head -n4
2011-08-24 03:43:55
User: menachem
Functions: grep head
Tags: awk xkcd
0

This does the same thing that the command 'j_melis' submitted, but does it a lot quicker.

That command takes 43 seconds to complete on my system, while the command I submitted takes 6 seconds.

echo $(grep "^[^']\{3,5\}$" /usr/share/dict/words|shuf -n4)
2011-08-23 21:15:18
User: j_melis
Functions: echo grep
Tags: awk xkcd
1

The improvement of this command over Strawp's original alternative is that you can specify the size of the words, in this particular case words between 3 and 5 character's long. It also excludes words that contain apostrophes, if you'd rather keep those words simply substitue [^'] for .

awk 'BEGIN {srand} /^[a-z]{4,8}$/ {w[i++]=$0} END {while (j++<4) print w[int(rand*i)]}' /usr/share/dict/words
2011-08-18 02:09:42
User: putnamhill
Functions: awk
Tags: awk xkcd
-1

If you do not have shuf or an -R option in sort, you can fall back on awk. This provides maximum portability IMO. The command first collects words from the dictionary that match the criteria - in this case: lower case words with no punctuation that are 4 to 8 characters long. It then prints 4 random entries. I decided to print each word on a separate line to improve readability.

awk -F\" '/^DocumentRoot/{print $2}' $(httpd -V | awk -F\" '/\.conf/{print $2}')
TIME=$( { time redis-cli PING; } 2>&1 ) ; echo $TIME | awk '{print $3}' | sed 's/0m//; s/\.//; s/s//; s/^0.[^[1-9]*//g;'
2011-08-11 19:09:49
User: allrightname
Functions: awk echo sed time
-1

Outputs the real time it takes a Redis ping to run in thousands of a second without any proceeding 0's. Useful for logging or scripted action.

ls *.zip|awk '{$a="zip -fo "$1" FILENAME"; system($a);}'
2011-07-27 10:22:21
User: youkey
Functions: awk ls
Tags: awk zip
0

- all zips are in current folder

- FILENAME is file name that should be subsitute in all zips (new version of this file is in current folder)

curl -s --compressed http://funnyjunk.com | awk -F'"' '/ '"'"'mainpagetop24h'"'"'/ { print "http://funnyjunk.com"$4 }' | xargs curl -s | grep -o 'ht.*m/pictures/.*\.jpg\|ht.*m/gifs/.*\.gif' | grep "_......_" | uniq | xargs wget
2011-07-21 15:57:21
User: laniner
Functions: awk uniq xargs
0

If your version of curl does not support the --compressed option, use

curl -s http://funnyjunk.com | gunzip

instead of

curl -s --compressed http://funnyjunk.com
find . -type f -name "*.py" -exec wc -l {} \; | awk '{ SUM += $1} END {print SUM }'
netstat -nt | awk -F":" '{print $2}' | sort | uniq -c
ps -fu userid | awk '/userid/{print $2}' | xargs kill
2011-06-16 12:20:19
User: unxscorob
Functions: awk ps xargs
Tags: awk
0

to be executed from root. this works well on most commercial unix systems, have not tried on linux systems.

pkill -KILL -u username
2011-06-16 11:40:57
User: SuperFly
Tags: awk grep who
10

How to force a userid to log out of a Linux host, by killing all processes owned by the user, including login shells:

gpg --refresh-keys
ifconfig | awk '/HWaddr/ { print $1, $5 }'
ifconfig | grep HWaddr | awk '{print $1,$5}'
awk '{x+=$2; y+=$3} NR%5==0{print x/5,y/5; x=y=0}' file.txt
seq 20 | awk 'ORS=NR%5?FS:RS'
2011-05-26 11:39:34
User: kev
Functions: awk seq
Tags: awk
0

seq 20 | awk '{line=line sep $0; sep=" "} !(NR%5){print line; line=sep=""}'

echo $(( $( date +%s ) - $( stat -c %Y * | sort -nr | head -n 1 ) ))
ls -atr /home/reports/*.csv -o --time-sty=+%s | tail -1 | awk '{print systime()-$5}'
FILE=`ls -ltr /var/lib/pgsql/backups/daily/ | tail -n1 | awk '{print $NF}'`; TIME=`stat -c %Y /var/lib/pgsql/backups/daily/$FILE`; NOW=`date +%s`; echo $((NOW-TIME))
awk '{command="echo "$2"|md5sum" ;command | getline $2; close(command);sub(/[[:blank:]].*/,"",$2); print $0}'
apt-get install `ssh root@host_you_want_to_clone "dpkg -l | grep ii" | awk '{print $2}'`
domain=google.com; for ns in $(whois $domain | awk -F: '/Name Server/{print $2}'); do echo ">>> Nameservers for $domain from $a <<<"; dig @$ns $domain ns +short; echo; done;
2011-05-08 04:46:34
User: laebshade
Functions: awk dig echo whois
2

Change the $domain variable to whichever domain you wish to query.

Works with the majority of whois info; for some that won't, you may have to compromise:

domain=google.com; for a in $(whois $domain | grep "Domain servers in listed order:" --after 3 | grep -v "Domain servers in listed order:"); do echo ">>> Nameservers for $domain from $a

Note that this doesn't work as well as the first one; if they have more than 3 nameservers, it won't hit them all.

As the summary states, this can be useful for making sure the whois nameservers for a domain match the nameserver records (NS records) from the nameservers themselves.

ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
2011-05-01 13:32:58
User: hute37
Functions: awk ls printf xargs
Tags: awk mv xargs
3

rename file name with fixed length nomeric format pattern