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). Show Sample Output
Replace 'more' command with any command which is in your PATH. Show Sample Output
In Debian based distros, this command will list 'binutils' package details which contains 'nm' command. You can replace 'nm' to any other command.
You may also use the $(which foo) variant instead of backticks. I personnaly have an alias ll='ls -l'. Show Sample Output
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
Show Sample Output
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).
Some command names are very different from the name of the package that installed them. Sometimes, you may want to find out the name of the package that provided a command on a system, so that you can install it on another system. Show Sample Output
Put this one-line function somewhere in your shell init, re-login and try
whatinstalled <command>
This is an elaborate wrapper around "dpkg -S", with numerous safeguards. Symlinks and command aliases are resolved. If the searched command is not an existing executable file or was installed by some other means than dpkg/apt, nothing is printed to stdout, otherwise the package name.
Show Sample Output
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. Show Sample Output
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 -- Show Sample Output
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}'); }
Show Sample Output
this is the much easier zsh equivalent ...
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. Show Sample Output
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`.
commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.
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
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: