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 tagged which from sorted by
Terminal - Commands tagged which - 16 results
which <command> > /dev/null 2>&1 && echo Success!
2013-03-13 10:04:42
User: skkzsh
Functions: echo which
Tags: which
-4

or

which <command> > /dev/null 2>&1 || echo Error!

For example, I write

which colordiff > /dev/null 2>&1 && alias diff=colordiff

in my `~/.bashrc`.

ls -l `whereis gcc`
2011-11-15 19:45:08
User: knathan54
Functions: ls
Tags: which ls zsh
0

whereis (1) - locate the binary, source, and manual page files for a command

Not actually better, just expanded a bit. The "whereis" command has the following output:

whereis gcc

gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc /usr/share/man/man1/gcc.1.gz

therefore the 'ls' error on first line, which could be eliminated with a little extra work.

ls -l =gcc
ls -l `which gcc`
command -v bash
2011-09-26 10:17:41
User: atoponce
Functions: command
Tags: which command
4

it is generally advised to avoid using which(1) whenever possible. which(1) is usually a csh(1) script, or sometimes a compiled binary. It's output is highly variable from operating system to operating system, so platform independent scripts could become quite complicated with the logic. On HP-UX 10.20, for example, it prints "no bash in /path /path /path ..."; on OpenBSD 4.1, it prints "bash: Command not found."; on Debian (3.1 through 5.0 at least) and SuSE, it prints nothing at all; on Red Hat 5.2, it prints "which: no bash in (/path:/path:...)"; on Red Hat 6.2, it writes the same message, but on standard error instead of standard output; and on Gentoo, it writes something on stderr. And given all these differences, it's still variable based on your shell. This is why POSIX is king. See http://mywiki.wooledge.org/BashFAQ/081 for more ways on avoiding which(1).

function summpkg { dpkg -s $(dpkg -S $1 | egrep -w $(which $1)$ | awk -F: '{print $1}') ; }
2011-07-05 23:06:37
User: b_t
Functions: awk egrep which
Tags: which dpkg
0

This version builds on my command 8776 (Find the package a command belongs to on debian-based distros). So if you use that command to find package name then you could alternatively use following for

package summary:

function summpkg { dpkg -s $(whichpkg $1 | awk -F: '{print $1}'); }
bind '"\C-e":"\eb `which \ef`\e\C-e"'
2011-01-26 16:11:52
User: jennings6k
0

Tested with bash v4.1.5 on ubuntu 10.10

Limitations:

as written above, only works for programs with no file extention (i.e 'proggy', but not 'proggy.sh')

because \eb maps to readine function backward-word rather then shell-backward-word (which

is unbinded by default on ubuntu), and correspondingly for \ef.

if you're willing to have Ctrl-f and Ctrl-g taken up too , you can insert the following lines

into ~/.inputrc, in which case invoking Ctrl-e will do the right thing both for "proggy" and "proggy.sh".

-- cut here --

\C-f:shell-backward-word

\C-g:shell-forward-word

"\C-e":"\C-f`which \C-g`\e\C-e"

-- cut here --

alias whichall='{ command alias; command declare -f; } | command which --read-functions --read-alias -a'
2010-11-18 03:32:04
User: AskApache
Functions: alias command which
2

5 helpful aliases for using the which utility, specifically for the GNU which (2.16 tested) that is included in coreutils.

Which is run first for a command. Same as type builtin minus verbosity

alias which='{ command alias; command declare -f; } | command which --read-functions --read-alias'

Which (a)lias

alias whicha='command alias | command which --read-alias'

Which (f)unction

alias whichf='command declare -f | command which --read-functions'

Which e(x)ecutable file in PATH

alias whichx='command which'

Which (all) alias, function, builtin, and files in PATH

alias whichall='{ command alias; command declare -f; } | command which --read-functions --read-alias -a'

# From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

perl -le 'chomp($w=`which $ARGV[0]`);$_=`file $w`;while(/link\b/){chomp($_=(split/`/,$_)[1]);chop$_;$w.=" -> $_";$_=`file $_`;}print "\n$w";' COMMAND_NAME
2010-07-30 19:26:35
User: dbbolton
Functions: perl
0

This will show you any links that a command follows (unlike 'file -L'), as well as the ultimate binary or script.

Put the name of the command at the very end; this will be passed to perl as the first argument.

For obvious reasons, this doesn't work with aliases or functions.

ls -l `which foo`
2010-07-09 01:34:02
User: adeverteuil
Functions: ls
1

You may also use the $(which foo) variant instead of backticks. I personnaly have an alias ll='ls -l'.

type <command>
whereis command
which command
dpkg -S `which nm` | cut -d':' -f1 | (read PACKAGE; echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}"; dpkg -L "${PACKAGE}") | less
2009-07-14 20:21:48
User: mohan43u
Functions: cut echo read
Tags: which dpkg
2

In Debian based distros, this command will list 'binutils' package details which contains 'nm' command. You can replace 'nm' to any other command.

vim `which <scriptname>`
2009-05-08 17:21:47
User: bunedoggle
Functions: vim
Tags: vim which script
1

Often I need to edit a bash or perl script I've written. I know it's in my path but I don't feel like typing the whole path (or I don't remember the path).

rpm -qif `which more`
2009-02-27 08:59:07
User: alcik
Functions: rpm
Tags: rpm which
3

Replace 'more' command with any command which is in your PATH.