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
cd $(dirname $(find ~ -name emails.txt))
2009-05-01 21:26:58
User: haivu
Functions: cd dirname find
Tags: bash dirname
9

This command looks for a single file named emails.txt which is located somewhere in my home directory and cd to that directory. This command is especially helpful when the file is burried deep in the directory structure. I tested it against the bash shells in Xubuntu 8.10 and Mac OS X Leopard 10.5.6

python -c "import socket; print '\n'.join(socket.gethostbyname_ex(socket.gethostname())[2])"
2009-04-30 16:10:43
User: haivu
Functions: python
Tags: Network python
-6

The socket.gethostname() call returns the host name of the computer. The socket.gethostbyname_ex() call returns a list of three items: The host name, the list of aliases for this host, and a list of IP addresses. Recall that Python?s array starts with index 0, the socket.gethostbyname_ex(?)[2] expression refers to the list of IP addresses. Finally, the print statement prints out the IP addresses, one per line.

history | grep ssh
2009-04-03 01:35:52
User: haivu
Functions: grep
Tags: bash csh
-9

After seeing the command you wish to repeat, just invoke it using the ! syntax.

ls -l | grep ^l
2009-04-02 17:47:36
User: haivu
Functions: grep ls
Tags: ls
-6

Shows all linked file and destinations. The 'ls -l' command lists the files in long (1 file per line) format, and the grep command displays only those lines that starts with an l (lower case L) -- a linked file.

Updated: Remove reference to hard links because this command does not apply to hard link as others kindly pointed out.

cal -3
2009-04-02 17:40:38
User: haivu
Functions: cal
Tags: PIM
-3

the -3 show the previous, current, and next month.

whatis [command-name]
2009-04-02 17:30:13
User: haivu
Functions: whatis
5

The whatis command displays a short description for the command you list on the command line. It is useful to quickly learn what a command does

bc
2009-03-24 16:19:12
User: haivu
Functions: bc
-2

bc is a wonderful calculator. Just type bc at the command line and have at it. Ctrl+D (or type quit) will get you out. This usage is just scratching the surface: bc can handle a mini scripting language, complete with variable, statements, loop, conditional statements and more. Do a man page on it to find out.

w
2009-03-24 16:03:28
User: haivu
-1

Short and sweet command. This command is also useful for other information such as what IP address a particular user logged in from, how long had they been logged in, what shell do they use.

bvi [binary-file]
2009-03-24 15:30:50
User: haivu
Tags: edit
-3

bvi is your vi for binary editing. If your system does not have it, you can get it from

http://bvi.sourceforge.net/

for i in {1..15}; do echo $i; done
2009-03-21 23:08:41
User: haivu
Functions: echo
Tags: bash
0

The brace expansion also allows you to count backward:

for i in {15..1}; do echo $i; done

You can also use this construct to create new file or new directory:

mkdir dir{1..3} # Same as mkdir dir1 dir2 dir3

ls -hog
2009-03-21 05:24:49
User: haivu
Functions: ls
Tags: shell
20

I often deal with long file names and the 'ls -l' command leaves very little room for file names. An alternative is to use the -h -o and -g flags (or together, -hog).

* The -h flag produces human-readable file size (e.g. 91K instead of 92728)

* The -o suppresses the owner column

* The -g suppresses the group column

Since I use to alias ll='ls -l', I now do alias ll='ls -hog'

fc [history-number]
2009-03-20 15:09:43
User: haivu
Functions: fc
Tags: bash
8

If you would like to edit a previous command, which might be long and complicated, you can use the fc (I think it stands for fix command). Invoke fc alone will edit the last command using the default editor (specified by $FCEDIT, $EDITOR, or emacs, in that order). After you make the changes in the editor, save and exit to execute that command. The fc command is more flexible than what I have described. Please 'man bash' for more information.

CDPATH=:..:~:~/projects
2009-03-20 14:50:25
User: haivu
Tags: bash
81

CDPATH tells the cd command to look in this colon-separated list of directories for your destination. My preferred order are 1) the current directory, specified by the empty string between the = and the first colon, 2) the parent directory (so that I can cd lib instead of cd ../lib), 3) my home directory, and 4) my ~/projects directory.

( IFS=:; for p in $PATH; do echo $p; done )
2009-03-19 22:45:47
User: haivu
Functions: echo
Tags: bash
3

The output of "echo $PATH" is hard to read, this is much easier. The parentheses ensure that the change to the input field separator (IFS) only happens the the sub shell and not affecting the current shell.

over myscript.sh
2009-03-13 06:13:21
User: haivu
-2

over works like 'more' or 'less', but it pretty print the source code

leave +15
2009-03-13 06:09:52
User: haivu
Tags: PIM
30

If you spend most of your time in front of the terminal, leave is a useful reminder. Leave can have absolute form: leave 1555 reminds you to leave at 3:55PM

ls -A
2009-03-13 05:12:01
User: haivu
Functions: ls
Tags: terminal
0

This command is almost the same as 'ls -a', but it does not display the current dir (.) or parent (..)

for person in Alex Bruce Fred Kathy Vicki Victoria ; do say -v $person "Hello, my name is $person"; sleep 1; done
2009-03-09 15:42:13
User: haivu
Functions: sleep
Tags: Os X
-5

Listen to different voices in the system--useful for picking the voice you like

export PS1='\n[\u@\h \! \w]\n\[\e[32m\]$ \[\e[0m\]'
2009-03-09 15:34:22
User: haivu
Functions: export
Tags: bash
7

I put that line in my .bash_profile (OS X) and .bashrc (Linux).

Here is a summary of what the \char means: n=new line, u=user name, h=host, !=history number, w=current work directory

The \[\e[32m\] sequence set the text to bright green and \[\e[0m\] returns to normal color.

For more information on what you can set in your bash prompt, google 'bash prompt'

cd() { builtin cd "${@:-$HOME}" && ls; }
2009-03-05 22:37:35
User: haivu
Functions: cd
6

Often, the very next command after the cd command is 'ls', so why not combine them?. Tested on a Red Hat derivative and Mac OS X Leopard

Update: changed ${1:-$HOME} to "${@:-$HOME}" to accomodate directories with spaces in the names

vi -R filename
2009-03-04 06:56:40
User: haivu
Functions: vi
Tags: vim
-3

The -R flag prevents you from making changes to a file, useful when you only want to view the file. This command is nicer than the 'more' or 'less' commands because for source codes, vi shows syntax highlight.

cal | grep --before-context 6 --after-context 6 --color -e " $(date +%e)" -e "^$(date +%e)"
2009-03-04 06:46:52
User: haivu
Functions: cal grep
Tags: PIM
3

Explanation:

* The date command evaluated to today's date with blank padded on the left if single digit

* The grep command search and highlight today's date

* The --before-context and --after-context flags displays up to 6 lines before and after the line containing today's date; thus completes the calendar.

I have tested this command on Mac OS X Leopard and Xubuntu 8.10

ping -i 60 -a IP_address
2009-03-04 06:21:22
User: haivu
Functions: ping
Tags: Network
97

Waiting for your server to finish rebooting? Issue the command above and you will hear a beep when it comes online. The -i 60 flag tells ping to wait for 60 seconds between ping, putting less strain on your system. Vary it to your need. The -a flag tells ping to include an audible bell in the output when a package is received (that is, when your server comes online).

ping -c 10 hostname
2009-03-04 06:14:52
User: haivu
Functions: ping
Tags: Network
-4

On Linux and Mac systems (I have not tested with other Unix systems), the ping command will keep on pinging until the user interrupts it with Ctrl+C. On Windows system, ping will execute for a number of times then quit. The -c flag on Linux and Mac will make this happen

alias vb='vim ~/.bashrc; source ~/.bashrc'
2009-03-02 21:01:49
User: haivu
Functions: alias col groff man
2

Place the line above in your ~/.bahsrc file. Now every time you issue the 'vb' command, you invoke the vim editor to edit it, then source it so the changes take effect immediately.

Notes:

* This mechanism is not working well if your .bashrc contains commands that should not be sourced more than once.

* This trick also work for your csh or tclsh users: place the following line in your ~/.cshrc file:

alias vc 'vim ~/.cshrc; source ~/.cshrc

Thank you adzap for pointing out the missing quote