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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands by bartonski from sorted by
Terminal - Commands by bartonski - 44 results
2end () ( export LC_ALL=C; nl -n rz $1 > $1.tmp; ${EDITOR:-vi} $1.tmp; sort $1.tmp | sed -r 's/^.*[0-9]+\t+//' > $1; rm $1.tmp; )
2010-03-06 23:02:28
User: bartonski
Functions: export nl rm sed sort

This function is used to sort selected lines of a text file to the end of that file. Especially useful in cases where human intervention is necessary to sort out parts of a file. Let's say that you have a text file which contains the words






For whatever reason, you want to sort all words rhyming with 'tough' to the bottom of the file, and all words denoting colors to the top, while keeping the order of the rest of the file intact.

'$EDITOR' will open, showing all of the lines in the given file, numbered with '0' padding. Adding a '~' to the beginning of the line will cause the line to sort to the end of the file, adding '!' will cause it to sort to the beginning.

box() { t="$1xxxx";c=${2:-=}; echo ${t//?/$c}; echo "$c $1 $c"; echo ${t//?/$c}; }
2010-02-26 13:17:12
User: bartonski
Functions: echo

First argument: string to put a box around.

Second argument: character to use for box (default is '=')

Same as command #4948, but shorter, and without the utility function.

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

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

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

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

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

given lines of the form




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

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

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

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

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

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





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

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

for example:

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



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

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)