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
curl -s 'http://xkcd.com/rss.xml' | xpath '//item[1]/description/text()' 2>&1 | sed -n 's/.*title="\([^"]*\)".*/\1/p' | fold -s
2013-02-16 22:38:26
User: putnamhill
Functions: fold sed
Tags: sed curl fold xpath
0

I look at xkcd in my news reader, but it displays the image's title attribute only for a few seconds which makes reading the longer ones more challenging. So I use this to display it in my console.

function up { i=$1; while [ $((i--)) -gt 0 ]; do cd ../ && pwd; done }
module_exists(){ perl -e 'use '$1 2>/dev/null; }
2013-01-11 14:47:07
User: putnamhill
Functions: perl
Tags: bash perl
-2

This version uses a bash function and does not print the path to the module.

echo $((RANDOM % 10 + 1))
2012-11-21 13:49:53
User: putnamhill
Functions: echo
Tags: bash
1

If you don't have seq or shuf, bash can be used.

sed 1d foo.txt
tr a-zA-Z A-Za-z < input.txt
genRandomText() { perl -e '$n=shift; print chr(int(rand(26)) + 97) for 1..$n; print "\n"' $1;}
2012-01-21 00:21:20
User: putnamhill
Functions: perl
-1

If you don't have seq, you can use perl.

perl -e '$f = join("", <>); for (0..127) {$_ = chr($_); if (/[[:print:]]/) {print if index($f, $_) < 0}} print "\n"'
2012-01-05 23:38:06
User: putnamhill
Functions: perl
Tags: perl slurp
0

Here's a perl version that only considers printable characters. Change the regex /[[:print:]]/ to look for different sets of delimiter characters.

ls -d $PWD/*
xxd <file>
2011-09-09 21:52:30
User: putnamhill
0

xxd can convert a hexdump back to binary using the -r option which can be useful for patching or editing binary files.

fileName(){ echo ${1##*/}; }
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}')
awk '(length > n) {n = length} END {print n}'
2011-08-15 13:10:38
User: putnamhill
Functions: awk
8

If your wc does not have the -L option, you can use awk.

read day month year <<< $(date +'%d %m %y')
MyVAR=86; awk -v n=$MyVAR '{print n}'
a=(*); echo ${a[$((RANDOM % ${#a[@]}))]}
ssh username@remotehost 'mysqldump -u <dbusername> -p<dbpassword> <dbname> tbl_name_1 tbl_name_2 tbl_name_3 | gzip -c -' | gzip -dc - | mysql -u <localusername> -p<localdbpassword> <localdbname>
echo $((RANDOM%256)).$((RANDOM%256)).$((RANDOM%256)).$((RANDOM%256))
echo -e "Content-type: text/plain\n\n$REMOTE_ADDR"
2010-12-20 14:51:42
User: putnamhill
Functions: echo
-12

This version does not rely on a 3rd party service. Just put this in a bash cgi on your own server.

openssl base64 -d < file.txt > out
sed 's/^[[:blank:]]*//; s/^#.*//; /^$/d' filename
2010-12-10 13:24:16
User: putnamhill
Functions: sed
Tags: sed BRE
0

This will first remove any leading white space. If the line then starts with a comment character, it is cleared. If the result is an empty line, it's deleted. This allows for comment lines with leading white space.

perl -e 'for(;;){@d=split("",`date +%H%M%S`);print"\r";for(0..5){printf"%.4b ",$d[$_]}sleep 1}'
2010-12-04 00:05:54
User: putnamhill
Functions: perl
9

Fun idea! This one adds seconds and keeps running on the same line. Perl's probably cheating though. :)

pcharc(){ perl -e 'for (0..255) {$_ = chr($_); print if /['$1']/}' | cat -v; echo;}
2010-11-13 00:32:41
User: putnamhill
Functions: cat perl
Tags: perl
1

Today I needed a way to print various character classes to use as input for a program I was writing. Also a nice way to visualize character classes.

awk -F, '{print $1" "$2" "$NF}' foo.txt