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 by putnamhill from sorted by
Terminal - Commands by putnamhill - 75 results
awk '(length>t) {t=length} END {print t}' *.cpp
X='pattern'; vim +/"$X" `egrep -lr "$X" *`
perl -pi -e 's/\r\n?/\n/g'
2010-03-18 17:48:16
User: putnamhill
Functions: perl
Tags: perl
3

This method will also convert mac line endings.

hdiutil burn foo.iso
curl -s http://www.commandlinefu.com/commands/by/$1/xml | awk -F'</?div[^>]*>' '/class=\"command\"/{gsub(/&quot;/,"\"",$2); gsub(/&lt;/,"<",$2); gsub(/&gt;/,">",$2); gsub(/&amp;/,"\\&",$2); cmd=$2} /class=\"num-votes\"/{printf("%3i %s\n", $2, cmd)}'
2010-02-16 17:24:45
User: putnamhill
Functions: awk
2

This version prints current votes and commands for a user. Pass the user as an argument. While this technically "fits" as a one liner, it really is easier to look at as a shell script with extra whitespace. :)

sed 's/.*/ /'
2010-02-11 17:45:56
User: putnamhill
Functions: sed
Tags: sed
0

The first version printed:

tr: empty string2

The second version printed:

sed: -i may not be used with stdin

Maybe I misunderstood the orginal problem.

date -s "`curl -sI www.example.com | sed -n 's/^Date: //p'`"
2010-01-08 21:05:41
User: putnamhill
Functions: date
Tags: curl date
0

If you don't have netcat, you can use curl.

jot -s '' -b '-' 50
command | sed -n '1,/regex/p'
curl -s search.twitter.com | awk -F'</?[^>]+>' '/\/intra\/trend\//{print $2}'
dig +short -x {ip}
2009-12-21 14:43:35
User: putnamhill
Functions: dig
Tags: IP reverse dig dns
7

The +short option should make dig less chatty.

ps aux | grep [h]ttpd | cat -n
2009-12-17 20:45:44
User: putnamhill
Functions: cat grep ps
Tags: cat
0

If you're on a system that doesn't have nl, you can use cat -n.

xmllint --noout some.xml 2>&1 >/dev/null || exit 1
unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'
while read str; do echo "$((++i)) - $str"; done < infile
2009-12-09 15:11:25
User: putnamhill
Functions: echo read
Tags: bash
-1

Hi glaudiston, you can save a few chars by leaving out cat and pipe and still enjoy the added flexibility.

curl -sL 'dynamic.xkcd.com/comic/random/' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
cat -n
if (nc -zw2 www.example.com 80); then echo open; fi
while read n; do host $n; done < list
curl -o id.gif `date +http://d.yimg.com/a/p/uc/%Y%m%d/largeimagecrwiz%y%m%d.gif`
2009-12-04 20:16:32
User: putnamhill
Tags: curl date
0

Requires the date command. This also works with some other comics. Here's a bash script that displays daily Garfield, Id, and Andy Capp:

http://putnamhill.net/cgi-bin/yahoo-comics.sh?ga,crwiz,crcap

curl -s 'xkcd.com' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
2009-12-03 19:49:37
User: putnamhill
Functions: awk
0

I wasn't sure how to display the image, so I thought I'd try xml for a different twist.

date +%V
egrep 'https?://([[:alpha:]]([-[:alnum:]]+[[:alnum:]])*\.)+[[:alpha:]]{2,3}(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?'
2009-11-28 15:41:42
User: putnamhill
Functions: egrep
5

For the record: I didn't build this. Just shared what I found that worked. Apologies to the original author!

I decided I should fix the case where http://example.com is not matched for the next time I need this. So I read rfc1035 and formalized the host name regex.

If anyone finds any more holes, please comment.

perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'
ls -F | sed -n 's/@$//p'