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 using which from sorted by
Terminal - Commands using which - 21 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`.

xinit $(which awesome) -- /usr/bin/Xephyr :5 -once -fullscreen -ac -keybd "ephyr,,,xkbmodel=pc105,xkblayout=it,xkbrules=evdev,xkboption="
2012-11-03 00:54:33
User: hute37
Functions: which
Tags: awesome
-2

alternatives:

xinit $(which gnome-awesome-session)

with cat /usr/local/bin/gnome-awesome-session:

#!/bin/bash

exec gnome-session --session awesome "$@"

following: http://awesome.naquadah.org/wiki/Quickly_Setting_up_Awesome_with_Gnome

note: in ubuntu 12.10 a probable Xephyr recent bug rejects keybd option ...

echo -n '#!'$(which awk)
echo '#!'$(which bash) > script.sh
2012-02-06 08:25:27
User: sharfah
Functions: echo which
Tags: bash
-4

Writes out the shebang line (#!/bin/bash) to the script.

{ echo -n '#!'; which bash; } > script.sh
vie(){vi $(which $1)}
2011-10-03 15:07:19
User: lpb612
Functions: which
0

vie myscript

will find where myscript is, and then use vi to edit that file. Not much trick, but saves typing if you use it a lot.

which any_path/a_command.sh | sed "s|^./|$(pwd)|"
2011-09-22 10:38:56
User: keymon
Functions: sed which
-8

This works in multiple unixes, not only linux, for different paths.

On solaris, if you do not have which, you can use:

ksh whence -p anypath/a_command.sh | sed "s|^./|$(pwd)|" ksh whence -p

sedit() { cp "$*"{,.bk}; which $EDITOR > /dev/null && $EDITOR "$*" || vim "$*"; }
2011-08-16 18:28:22
User: kaedenn
Functions: cp vim which
-6

Some people put spaces in filenames. Others have an $EDITOR environment variable set. This defaults to vim, but you can use whatever you wish: emacs, nano, ed, butterflies, etc.

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}'); }
function whichpkg { dpkg -S $1 | egrep -w $(which $1)$; }
for c in gcc bison dialog bc asdf; do if ! which $c >/dev/null; then echo Required program $c is missing ; exit 1; fi; done
shebang() { if i=$(which $1); then printf '#!%s\n\n' $i > $2 && vim + $2 && chmod 755 $2; else echo "'which' could not find $1, is it in your \$PATH?"; fi; }
2011-03-09 14:47:32
User: bartonski
Functions: chmod echo printf vim which
3

The first argument is the interpreter for your script, the second argument is the name of the script to create.

equery belongs $( which mv )
dpkg -S $( which ls )
find_alternatives(){ for i;do which "$i" >/dev/null && { echo "$i"; return 0;};done;return 1;}
2011-01-06 19:53:46
User: eightmillion
Functions: echo return which
-1

This function is used to set environmental variables from a list of alternatives depending on what's installed on the system. It returns the first program found in the list.

Example usage:

export BROWSER=$(find_alternatives chromium-browser google-chrome opera firefox firefox-bin iceweasel konqueror w3m lynx)

.

export EDITOR=$(find_alternatives vim nano pico emacs kate)

.

export PAGER=$(find_alternatives vimpager less most more pg)
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

which command
grep CONFIG $(which mysqlbug)
which <filename>
while true; do { $(which logger) -p local4.notice `free -m | grep Mem`; sleep 60; } done &
2009-06-22 00:29:53
User: Neo23x0
Functions: grep sleep which
3

Uses logger in a while loop to log memory statistics frequently into the local syslog server.

which somecommand
2009-02-05 10:29:04
User: chrisdrew
Functions: which
-1

Returns the pathnames of the files which would be executed in the current environment had its arguments been given as a command.