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 - 238 results
ls -t1 | head -n1
rm -rf `ls | head -5000`
2009-02-25 22:52:23
User: rockon
Functions: head rm
-3

Deletes thousands of files at one go, I'm not able to recall the exact # of files that rm can delete at one go(apprx. around 7000.)

tr -dc "a-zA-Z0-9-_\$\?" < /dev/urandom | head -c 10 | gpg -e -r medha@nerdish.de > password.gpg
2009-02-25 08:48:26
User: hans
Functions: gpg head tr
2

Adjust the

head -c

part for password length.

I use filenames like "hans@commandlinefu.com.gpg" and a vim which automatically decrypts files with .gpg suffixes.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
2009-02-24 09:43:40
User: Blackbit
Functions: head tr
11

If you want a password length longer than 6, changing the -c6 to read -c8 will give you 8 random characters instead of 6. To end up with a line-feed, use this with echo:

# echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6`

while [ 1 ]; do curl -s -u username:password http://twitter.com/statuses/friends_timeline.rss|grep title|sed -ne 's/<\/*title>//gp' | head -n 6 |festival --tts; sleep 300;done
2009-02-20 20:20:21
User: tomwsmf
Functions: head sleep
9

Pump up the chatter, run this script on a regular basis to listen to your twitter timeline.

This is a rough first cut using several cli clips I have spotted around. There is no facility to not read those things already read to you. This could also easily be put in a loop for timed onslaught from the chatterverse, though I think it might violate several pointsof the Geneva Convention

UPDATE - added a loop, only reads the first 6 twits, and does this every 5 mins.

sudo dmidecode | grep Serial\ Number | head -n1
2009-02-18 14:54:28
User: nlinux
Functions: grep head sudo
14

This will give you the Dell Service tag number associated with your machine. Incredibly useful when you need that number for tech support or downloads.

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
2009-02-16 00:39:28
User: jbcurtis
Functions: grep head strings tr
45

Find random strings within /dev/urandom. Using grep filter to just Alphanumeric characters, and then print the first 30 and remove all the line feeds.

cat -n $file | tail -n 100 && head -n number-of-lines-you-want-to-keep > newfile
2009-02-15 01:02:10
User: bbelt16ag
Functions: cat head tail
-1

tells you the number of lines in said file, and then tail the last 100 lines ( or how many are messed up) then u take the total amount of lines and then subract the 100 or so lines u DONT WANT, then do a head -n $new_number and then redirect it to new file.db

ps gv [pid] | head -2
2009-02-12 05:04:55
User: ren
Functions: head ps
-1

sh as:

#! /bin/sh

while [ 1 -ne 6 ]; do

pid=`ps -ef | grep -v "grep" | grep "trans_gzdy" | cut -c10-17`

ps gv $pid | head -2

sleep 1

done

check changes of RSS.

diff <(head -500 product-feed.xml) <(head -500 product-feed.xml.old)
2009-02-11 09:24:38
User: root
Functions: diff head
3

Useful for massive files where doing a full diff would take too long. This just runs diff on the first 500 lines of each. The use of subshells to feed STDIN is quite a useful construct.

head -1000 < lines.txt | tail -1
# cut -d ' ' -f1 /var/log/nginx/nginx-access.log | sort | uniq -c | sort -nr | head -10 | nl
export SSH_AUTH_SOCK=`find /tmp/ssh* -type s -user [user] -mtime -1 | head -1`
2009-02-05 20:55:41
User: wwest4
Functions: export head
1

Must be done as root - will cause subsequent ssh connections to use the identities available via the [user]'s agent socket.