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 haivu from sorted by
Terminal - Commands by haivu - 52 results
alias install='sudo apt-get install'
2011-10-27 19:53:01
User: haivu
Functions: alias
Tags: Ubuntu alias
-6

I put this line in my ~/.bashrc file (which I source via ~/.bash_profile). Now, when I need to install a package, I typed *install* instead of the longer version.

say WORD
2010-03-26 15:23:32
User: haivu
Tags: Os X osx
2

I often use this command to learn pronunciation of unfamiliar words.

printf ${PATH//:/\\n}
pydoc -p 8888 & gnome-open http://localhost:8888
2010-02-28 07:01:43
User: haivu
Tags: python help
12

I use this command to start a local Python document server over HTTP port 8888.

long_command; say I am all done
2010-01-27 19:03:01
User: haivu
Tags: audio osx
-2

I often run some command that takes a while to finish. By putting the say command afterward, I get an audio notification.

Please note that this command (say) only works on Mac OS X and not Linux.

man hier
2010-01-26 16:31:05
User: haivu
Functions: man
59

Curious about differences between /bin, /usr/bin, and /usr/local/bin? What should be in the /sbin dir? Try this command to find out.

Tested against Red Hat & OS X

alias PS1="man bash | sed -n '/ASCII bell/,/end a sequence/p'"
2010-01-15 23:39:28
User: haivu
Functions: alias
Tags: bash prompt ps1
3

I use this command (PS1) to show a list bash prompt's special characters. I tested it against A flavor of Red Hat Linux and Mac OS X

ls | grep -vi pattern
shopt -s cdable_vars
2009-10-26 22:10:56
User: haivu
Tags: bash shell
4

Usage:

mydir=/very/long/path/to/a/dir cd mydir

I often need to cd where no man wants to go (i.e. long path). by enabling the shell option cdable_vars, I can tell cd to assume the destination is the name of a variable.

declare -f [ function_name ]
set | fgrep " ()"
2009-10-22 17:48:54
User: haivu
Functions: fgrep set
0

If you issue the "set" command, you'll see a list of variables and functions. This command displays just those functions' names.

sed -n '/^function h\(\)/,/^}/p' script.sh
2009-10-19 07:55:35
User: haivu
Functions: sed
Tags: bash sed
8

I often need to extract a function from a bash script and this command will do it.

PROMPT_COMMAND=command
2009-10-15 06:01:18
User: haivu
Tags: bash prompt
1

For example, if you are the type who type ls very often, then

PROMPT_COMMAND=ls

will ls after every command you issue.

h() { if [ -z "$1" ]; then history; else history | grep "$@"; fi; }
2009-10-13 21:49:37
User: haivu
Functions: grep
Tags: bash grep
6

Place this in your .bash_profile and you can use it two different ways. If you issue 'h' on its own, then it acts like the history command. If you issue:

h cd

Then it will display all the history with the word 'cd'

echo $PATH|tr : '\n'|sort|uniq -d
(IFS=:;for p in $PATH; do test -d $p || echo $p; done)
2009-09-19 17:51:06
User: haivu
Functions: echo test
Tags: bash PATH
9

I often need to know of my directory in the PATH, which one DOES NOT exist. This command answers that question

* This command uses only bash's built-in commands

* The parentheses spawn a new sub shell to prevent the modification of the IFS (input field separator) variable in the current shell

ls -t | awk 'NR>5 {system("rm \"" $0 "\"")}'
2009-09-16 04:58:08
User: haivu
Functions: awk ls
Tags: awk ls
-2

I have a directory containing log files. This command delete all but the 5 latest logs. Here is how it works:

* The ls -t command list all files with the latest ones at the top

* The awk's expression means: for those lines greater than 5, delete.

PS3="Enter a number: "; select f in *;do $EDITOR $f; break; done
2009-09-10 06:04:42
User: haivu
Tags: bash
8

This command displays a simple menu of file names in the current directory. After the user made a choice, the command invokes the default editor to edit that file.

* Without the break statement, the select command will loop forever

* Setting the PS3 prompt is optional

* If the user types an invalid choice (such as the letter q), then the variable $f will become an empty string.

* For more information, look up the bash's select command

awk 'length>72' file
2009-09-10 05:54:41
User: haivu
Functions: awk
Tags: awk
16

This command displays a list of lines that are longer than 72 characters. I use this command to identify those lines in my scripts and cut them short the way I like it.

curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+'
2009-05-21 16:12:21
User: haivu
Functions: grep
4

The curl command retrieve the HTML text containing the IP address. The grep command picks out the IP address from that HTML text.

echo $BASH_VERSION
%
2009-05-21 15:02:39
User: haivu
Tags: vim vi
7

This is a simple command for jumping to the matching brace, square bracket, or parentheses. For example, it can take you from the beginning of a function to the end with one key stroke. To delete everything between the pairs of {}, [], or (), issue the command:

d%

To replace text between pairs of braces, brackets, or parentheses, issue the command:

c%

You can also use this command to find out if an opening brace has been properly closed.

curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=csco&f=l1'
2009-05-04 08:13:59
User: haivu
Tags: curl finance
11

Retrieve the current stock price from Yahoo Finance. The output is simply the latest price (which could be delayed). If you want to look up stock for a different company, replace csco with your symbol.

nl filename | more
2009-05-04 07:35:16
User: haivu
Functions: nl
-2

The nl command lists the contents of a file where is each line is prefixed by a line number. For more information about this command, check out its man page. I tested under Mac OS X and Xubuntu 9.04

export LSCOLORS=gxfxcxdxbxegedabagacad
2009-05-04 04:07:36
User: haivu
Functions: export
Tags: bash ls osx
2

I use terminal with black background on the Mac. Unfortunately, the default ls color for the directory is blue, which is very hard to see. By including the line above in my ~/.bash_profile file, I changed the directory's color to cyan, which is easer to see. For more information on the syntax of the LSCOLORS shell variable:

man ls

I tested this command on Mac OS X Leopard