Adds "-c" canonical option to bash "type" builtin command to follow symbolic links

type () { if [ "$1" = "-c" ]; then shift; for f in "$@"; do ff=$(builtin type -p "$f"); readlink -f "$ff"; done; else builtin type $typeopts "$@"; fi; }
The "type" builtin command is handy to find out what executable will be used if you issue a command. But on some distros, particularly when using /etc/alternatives, certain executables get buried under layers and layers of symbolic links and it becomes hard to find which one. If you put the above command in your .bashrc, it adds a "-c" option to the type command that will weed through the symbolic links and prints the actual file that will be executed.
Sample Output
splante@linux3101:~> type -c java
splante@linux3101:~> type java
java is /usr/bin/java
splante@linux3101:~> ls -l /usr/bin/java /etc/alternatives/java /etc/alternatives/jre /etc/alternatives/jdk /etc/alternatives/jdk_1.6.21/jre/bin/java
lrwxrwxrwx 1 root root 22 Jun 25  2010 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 12 Jul 21  2010 /etc/alternatives/java -> jre/bin/java
lrwxrwxrwx 1 root root 7 Jul 21  2010 /etc/alternatives/jre -> jdk/jre
lrwxrwxrwx 1 root root 10 Jul 21  2010 /etc/alternatives/jdk -> jdk_1.6.21
-rwxr-xr-x 1 root root 47308 Jun 22  2010 /etc/alternatives/jdk_1.6.21/jre/bin/java   

By: splante
2011-04-07 18:57:51

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? 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.

Share Your Commands

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.


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: