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 head from sorted by
Terminal - Commands using head - 229 results
host example.com | head -1 | awk '{print $4}'
nslookup www.example.com | tail -2 | head -1 | awk '{print $2}'
2013-09-05 20:26:45
User: wsams
Functions: awk head nslookup tail
1

I'm not sure how reliable this command is, but it works for my needs. Here's also a variant using grep.

nslookup www.example.com | grep "^Address: " | awk '{print $2}'

psgrep() { ps aux | tee >(head -1>&2) | grep -v " grep $@" | grep "$@" -i --color=auto; }
2013-08-02 12:44:32
User: fnl
Functions: grep head ps tee
Tags: grep ps
0

Pipes the header row of ps to STDERR, then greps for the command on the output of ps, removing the grep entry before that.

find /logs -ls -xdev | sort -nrk 7 | head -10
2013-07-22 08:36:03
User: vidya99343
Functions: find head sort
0

bash-3.2$ find /logs -ls -xdev | sort -nrk 7 | head -10

1761905 205380 -rwxrwxr-x 1 wsadmin logadmin 210095353 Jul 22 01:33 /logs/intlpymt/Trace.log

652689 187360 -rwxrwxr-x 1 wsadmin logadmin 191663182 Jul 21 23:00 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut_13.07.21_23.00.12.log

2380449 186536 -rwxrwxr-x 1 wsadmin logadmin 190819939 Jul 16 14:03 /logs/omset/traceIntl.log.201307161403.lppwa1213.gz

2119524 183888 -rwxrwxr-x 1 wsadmin logadmin 188110111 Jul 22 01:33 /logs/intlpymt/intlpymtria/Trace.log

652816 160332 -rwxrwxr-x 1 wsadmin logadmin 164011871 Aug 14 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_08142012.gzip

653312 128916 -rwxrwxr-x 1 wsadmin logadmin 131873943 Jul 18 10:49 /logs/websphere/heapdump.20130718.104150.27592.0006.phd.201307181406.lppwa1213.gz

653320 128916 -rwxrwxr-x 1 wsadmin logadmin 131873735 Jul 18 10:40 /logs/websphere/heapdump.20130718.104012.27592.0002.phd.201307181406.lppwa1213.gz

653309 128912 -rwxrwxr-x 1 wsadmin logadmin 131867602 Jul 18 10:46 /logs/websphere/heapdump.20130718.104008.27592.0001.phd.201307181405.lppwa1213.gz

653323 128872 -rwxrwxr-x 1 wsadmin logadmin 131828157 Jul 18 10:41 /logs/websphere/heapdump.20130718.104109.27592.0004.phd.201307181407.lppwa1213.gz

652783 120288 -rwxrwxr-x 1 wsadmin logadmin 123047750 Aug 13 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_0813.2012.gzip

bash-3.2$

find /logs -ls -xdev | sort -nrk 7 | head -10
head -c20 /dev/urandom | xxd -ps
2013-07-16 10:14:21
User: opexxx
Functions: head
Tags: HEAD urandom
-1

20characters long alpahnumeric "password"

history | awk '{CMD[$4]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | sort -nr | nl | column -t | head -n 10
du -xB M --max-depth=2 /var | sort -rn | head -n 15
awk '{print $1}' ~/.bash_history | sort | uniq -c | sort -rn | head -n 10
awk '{ print $9 }' access.log | sort | uniq -c | sort -nr | head -n 10
cat -n install.log | head -88 | tac | head -n $(( 88 - 50 )) | tac
cat -n install | head -n 150 | tac | head -n 50 | tac
awk '/Dec\/2012/ {print $1,$8}' logfile | grep -ivE '(.gif|.jpg|.png|favicon|.css|.js|robots.txt|wp-l|wp-term)' | sort | uniq -c | sort -rn | head -n 20
rpm -qa --queryformat '%{size} %{name}-%{version}-%{release}\n' | sort -k 1,1 -rn | nl | head -16
2013-03-19 21:10:54
User: mpb
Functions: head nl rpm sort
1

Interesting to see which packages are larger than the kernel package.

Useful to understand which RPMs might be candidates to remove if drive space is restricted.

find . -type f -exec ls -s {} \; | sort -n -r | head -5
tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | fold -w 12 | head -n 1
xxd -p /dev/urandom |fold -60|head -30|sed 's/\(..\)/\1 /g'
2013-02-19 22:18:52
User: psifertex
Functions: fold head sed
1

Replaces hexdump with the more succint xxd, and the sed was unnecessarily complex.

hexdump -v -e '"%u"' </dev/urandom|fold -60|head -n 30|sed 's/\(.\{2\}\)/\1 /g'
2013-02-16 12:56:24
User: knoppix5
Functions: fold head hexdump sed
6

or

od /dev/urandom -w60 -An|sed 's/ ..../ /g'|head -n 30

(this one lacks digits 8 and 9)

finger `whoami` | awk -F: '{ print $3 }' | head -n1 | sed 's/^ //'
48 function gbl() { git for-each-ref --sort=-committerdate --format='%(committerdate) %(authorname) %(refname)' refs/remotes/origin/|grep -e ".$@"|head -n 10; }
du . | sort -nr | awk '{split("KB MB GB TB", arr); idx=1; while ( $1 > 1024 ) { $1/=1024; idx++} printf "%10.2f",$1; print " " arr[idx] "\t" $2}' | head -25
2012-12-03 02:59:13
User: agas
Functions: awk du head printf sort
0

Lists the size in human readable form and lists the top 25 biggest directories/files

seq 10| shuf | head -1
2012-11-21 03:57:02
Functions: head seq
0

Above command will generate a random number between 1 to 10.

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -1 | python -c "import sys,crypt; stdin=sys.stdin.readline().rstrip('\n'); print stdin;print crypt.crypt(stdin)"
2012-11-09 00:40:22
User: cnyg
Functions: cat fold head python tr
1

Generate a 18 character password from character set a-zA-Z0-9 from /dev/urandom, pipe the output to Python which prints the password on standard out and in crypt sha512 form.

for k in `git branch -r|awk '{print $1}'`;do echo -e `git show --pretty=format:"%Cgreen%ci_%C(blue)%c r_%Cred%cn_%Creset" $k|head -n 1`$k;done|sort -r|awk -F"_" '{printf("%s %17s %-22s %s\n",$1,$2,$3,$4)}'
tr -dc '[:graph:]' </dev/urandom | head -c30; echo