Lesser known Linux Commands: type

type: This is a simple and very useful shell built-in command. It will tell us location of an executable file.  Type searches through $PATH, returns the pathname of the file. e.g
$ type sudo
sudo is /usr/bin/sudo

locate and which are similar command. However type differs in following respects
a.It is shell built in and hence fast.
b.It searches only $PATH
c.Searches for executable files only

Following timings are returned from system :
type
$ time type sudo
sudo is /usr/bin/sudo

real    0m0.000s
user    0m0.000s
sys     0m0.000s

which
$ time which sudo
/usr/bin/sudo

real    0m0.004s
user    0m0.004s
sys     0m0.000s

locate
$ time locate sudo
/etc/sudoerr
/etc/init.d/sudo
..

real    0m0.201s
user    0m0.192s
sys     0m0.004s

As can be seen above the execution time is remarkably less for type.
type is preferred to other in following conditions
a.Want to search in $PATH
b.Want to search only executables
type is not useful in following conditions
a.search whole file system
b.search on partial name.
c.search for non-executable files

2 Responses to Lesser known Linux Commands: type

  1. Krishna says:

    HI,

    Though the command does similar function i think only type and sudo are comparable to each other , as locate is a search utility for the whole filesystem while others are not , type and which can search only system path like bin /usr/local/bin.

    more over locate needs updatedb to be run periodically

    finding the time was a nice idea i liked it , its very useful🙂

    Bye,
    Krishna.

    • ramanchennai says:

      The reason I brought locate in this discussion is , I saw locate being used very common in most of the discussions related to file search. You are right about differences. As I mentioned type has advantage under some conditions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: