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 bartonski from sorted by
Terminal - Commands by bartonski - 42 results
box() { l=${#1}+4;x=${2:-=};n $l $x; echo "$x $1 $x"; n $l $x; }; n() { for (( i=0; $i<$1; i=$i+1)); do printf $2; done; printf "\n"; }
2010-02-26 06:56:59
User: bartonski
Functions: echo printf
1

The function 'box' takes either one or two arguments. The first argument is a line of text to be boxed, the second argument (optional) is a character to use to draw the box. By default, the drawing character will be '='.

The function 'n()' is a helper function used to draw the upper and lower lines of the box, its arguments are a length, and an character to print. (I used 'n' because 'line', 'ln' and 'l' are all commonly used)

underline() { echo $1; for (( i=0; $i<${#1}; i=$i+1)); do printf "${2:-=}"; done; printf "\n"; }
2010-02-26 05:46:49
User: bartonski
Functions: echo printf
1

underline() will print $1, followed by a series of '=' characters the width of $1. An optional second argument can be used to replace '=' with a given character.

This function is useful for breaking lots of data emitted in a for loop into sections which are easier to parse visually. Let's say that 'xxxx' is a very common pattern occurring in a group of CSV files.

You could run

grep xxxx *.csv

This would print the name of each csv file before each matching line, but the output would be hard to parse visually.

for i in *.csv; do printf "\n"; underline $i; grep "xxxx" $i; done

Will break the output into sections separated by the name of the file, underlined.

myinfo() { info --subnodes -o - $1 | less; }
2010-02-16 13:09:32
User: bartonski
Functions: info
9

For those who hate navigating info pages, a shell function which will dump the contents to stdout, then page it through less, thus acting like 'man'.

username=bartonski;curl -s http://www.commandlinefu.com/commands/by/$username/json|perl -e 'BEGIN{$s=0;$n=0};END{print "Score: $s\nEntries: $n\nMean: ";printf "%3.2f\n",$s/$n}' -0173 -nae 'foreach $f (@F){if($f =~ /"votes":"(-*\d+)"/){$s += $1; $n++;}}'
2010-02-16 01:03:29
User: bartonski
Functions: perl
2

Like command #4845, prints score, number of entries, and average score.

:.,$!perl -pne 'for $i ("0001".."0004"){ s/XXXX/$i/ if($i == $.) }'
2010-02-11 03:56:26
User: bartonski
0

given lines of the form

123|XXXX|1000

...

123|XXXX|1011

each 'XXXX' will be replaced with a serial number between 0001 and 0004.

exec bash
2010-02-10 18:51:26
User: bartonski
Functions: exec
4

This replaces the current bash session with a new bash session, run as an interactive non-login shell... useful if you have changed /etc/bash.bashrc, or ~/.bashrc

If you have changed a startup script for login shells, use

exec bash -l

Suitable for re-running /etc/profile, ~/.bash_login and ~/.profile.

edit: chinmaya points out that

env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"

will clear any shell variables which have been set... since this verges on unwieldy, might want to use

alias bash_restart='env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"'
smenu() ( IFS=',' ; select x in $*; do echo "$x" | xsel -i; done )
2010-02-08 15:33:53
User: bartonski
Functions: echo
Tags: xsel select
2

The function will take a comma separated list of items to be 'selected' by xsel -i:

smenu "First item to paste,Paste me #2,Third menu item"

You will then be prompted to choose one of the menu items. After you choose, you will be able to paste the string by clicking the middle mouse button.

The menu will keep prompting you to choose menu items until you break out with Control-C.

echo "I'm going to paste this into WINDERS XP" | xsel -i
2010-02-08 00:23:43
User: bartonski
Functions: echo
Tags: X xsel putty XMing
4

Set up X forwarding in PuTTY, with X display location set to :0.0

Launch PuTTY ssh session.

Launch Xming. Make sure that display is set to :0.0 (this is default).

echo "I'm going to paste this into WINDERS XP" | xsel -i

will insert the string into the windows cut and paste buffer.

Thanks to Dennis Williamson at stackoverflow.com for sharing...

export QQ=$(mktemp -d);(cd $QQ; curl -s -O http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' > clf.sh;rm -r $QQ
2010-01-30 19:47:42
User: bartonski
Functions: cd export grep mktemp perl sort
8

Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file.

Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.

set -x
diff {$path1,$path2}/file_to_diff
2010-01-29 23:00:57
User: bartonski
Functions: diff
1

This is useful when you're diffing two files of the same name in radically different directory trees. For example:

Set

path1='/some/long/convoluted/path/to/all/of/your/source/from/a/long/dead/machine'

then

path2='/local/version/of/same/file'

then run the command. Much easier on the eyes when you're looking back across your command history, especially if you're doing the same diff over and over again.

ruler() { for s in '....^....|' '1234567890'; do w=${#s}; str=$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do echo -n $s; done ); str=$(echo $str | cut -c -$COLUMNS) ; echo $str; done; }
dmd () { ( if [ "$1"x != "x" ]; then cd $1; fi; mkdir `date +%Y%m%d` ) }
2010-01-27 15:53:26
User: bartonski
Functions: cd mkdir
1

Creates a directory named with the current date, in the format YYYYMMDD. If you give it a directory name as an argument, it will create the new directory inside the specified directory.

This is an alternative to command #1993.

range () { end=$(echo "$1 + $2 - 1" | bc); echo "$1-$end"; }
2009-11-12 22:53:08
User: bartonski
Functions: echo
0

for example:

echo "..1234567." | cut -c $(range 3 7)

yields

1234567

slice="-rw-r--r-- "; ls -l | cut -c $(echo "$slice" | wc -c)-
tweet () { curl -u UserName -d status="$*" http://twitter.com/statuses/update.xml; }
2009-11-07 06:54:02
User: bartonski
Tags: twitter tweet
6

This version of tweet() doesn't require you to put quotes around the body of your tweet... it also prompts you for password. It will still barf on a '!' character.

enscript jrandom.txt -o - | ps2pdf - ~/tmp/jrandom.pdf (from file) or: ls | enscript -o - | ps2pdf - ~/tmp/ls.pdf (from stdout)