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 bbbco from sorted by
Terminal - Commands by bbbco - 12 results
for i in {1..6};do c=;d=;for u in `eval echo {1..$i}`;do c="$c../";d="$d..";eval "$d(){ cd $c;}"; eval "$d.(){ cd $c;}";done;done
2013-09-04 20:12:45
User: bbbco
Functions: echo eval
Tags: cd eval dot
1

Use dots to cd down directories instead of having to remember all of the pesky back slashes!

Better yet, works on even and odd number of dots! Now, just estimate how far down you want to traverse.

sudo dmidecode | grep Product
svn diff <FILE>
2012-01-30 16:47:48
User: bbbco
Functions: diff
Tags: svn diff
0

If you have ever edited a locally checked out version of a file to tweak it for testing purposes, and came back to it over a weekend, you might have forgotten what you exactly changed. This command helps you see the differences between the the checked in SVN version, and the one you tweaked.

genRandomText() { a=( a b c d e f g h i j k l m n o p q r s t u v w x y z );f=0;for i in $(seq 1 $(($1-1))); do r=$(($RANDOM%26)); if [ "$f" -eq 1 -a $(($r%$i)) -eq 0 ]; then echo -n " ";f=0;continue; else f=1;fi;echo -n ${a[$r]};done;echo"";}
2012-01-20 21:18:16
User: bbbco
Functions: echo seq
0

Ever need to get some text that is a specific number of characters long? Use this function to easily generate it! Doesn't look pretty, but sure does work for testing purposes!

type -t $1
2012-01-10 21:57:29
User: bbbco
Functions: type
Tags: type
1

Prints a string indicating whether a command is an alias, keyword, function, builtin, or file. I have used this in my BASH scripts to allow an external parameter to define which function to run, and ensure that it is a valid function that can indeed be run.

ls -a | sed "s#^#${PWD}/#"
2011-12-16 22:19:06
User: bbbco
Functions: ls sed
Tags: sed ls pwd
-9

Use the -a flag to display all files, including hidden files. If you just want to display regular files, use a -1 (yes, that is the number one). Got this by RTFM and adding some sed magic.

[bbbco@bbbco-dt ~]$ ls -a | sed "s#^#${PWD}/#"

/home/bbbco/.

/home/bbbco/..

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/.adobe

/home/bbbco/.bash_history

/home/bbbco/.bash_logout

/home/bbbco/.bash_profile

/home/bbbco/.bashrc

...

[bbbco@bbbco-dt ~]$ ls -1 | sed "s#^#${PWD}/#"

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/cookies.txt

/home/bbbco/Desktop

/home/bbbco/Documents

/home/bbbco/Downloads

...

STARTING_DIR=$(cd $(dirname $0) && pwd)
2011-11-30 17:35:15
User: bbbco
Functions: cd dirname
0

Sometimes you need the full path to your script, regardless of how it was executed (which starting directory) in order to maintain other relative paths in the script.

If you attempt to just use something simple like:

STARTING_DIR="${0%/*}"

you will only get the relative path depending on where you first executed the script from.

You can get the relative path to the script (from your starting point) by using dirname, but you actually have to change directories and print the working directory to get the absolute full path.

psql -c "SELECT pg_database.datname, pg_database_size(pg_database.datname), pg_size_pretty(pg_database_size(pg_database.datname)) FROM pg_database ORDER BY pg_database_size DESC;" -d <ANYDBNAME>
2011-11-30 15:22:48
User: bbbco
3

Get a listing of all of your databases in Postgres and their sizes, ordering by the largest size first.

Requires that you give the -d parameter a valid database name that you can connect to.

du -scb
2011-06-27 14:20:11
User: bbbco
Functions: du
Tags: du
2

Even simpler! Use du ... the -s and -c flags summarize and print a grand total of all files recursively. The -b flag prints in byte format. You can use the -h flag instead to print in human readable format.

> [filename]
2011-05-18 14:59:02
User: bbbco
-3

Ever need to erase the contents of a file and start over from scratch? This easy command allows you to do so. Be warned! This will immediately erase all the contents of your file and start you over from scratch (i.e. your file will be at 0 bytes, like if you touch a file).

anagram(){ s(){ sed 's/./\n\0/g'<<<$1|sort;};cmp -s <(s $1) <(s $2)||echo -n "not ";echo anagram; }; anagram foobar farboo;
2011-02-17 15:10:43
User: bbbco
Functions: cmp echo sed
2

This is just a slight alternative that wraps all of #7917 in a function that can be executed

alias rdp='rdesktop -u <user> -g 1600x1200 -D -r disk:home=/home -r clipboard:PRIMARYCLIPBOARD'
2011-02-04 16:22:49
User: bbbco
Functions: alias
0

Sets an alias to remote desktop to the specified console, along with options to ensure the RDP session takes up the whole screen, includes a home directory mapping, and clipboard mappings.