1

Is there any way or command to find out the path from where the a standard command is executed from? Example ls, mkdir, mysql, hadoop, etc..

I know that their origin can be found out from $PATH variable, and should be probably in one of the /bin directories. But how can I get the directory correctly from where the command runs from?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
sabra2121
  • 61
  • 9

2 Answers2

2

The which command returns the full path of an executable, e.g.:

mureinik@comupter ~ $ which mkdir
/usr/bin/mkdir

and if you really need the directory, you could always apply dirname to it:

mureinik@conmputer ~ $ dirname `which mkdir`
/usr/bin
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks Mureinik, for the quick answer.. :) – sabra2121 May 18 '15 at 22:21
  • Sorry, I tried to vote up, but it gave me a -1 and I am not able to rollback, it says I need 15 reputation. I found your answer useful, hence requesting the stackoverflow team to remove the -1 and change it to +1 – sabra2121 May 18 '15 at 22:26
  • @anna If this solved your question, you can mark the answer as accepted (the check mark underneath) the voting button. – Mureinik May 18 '15 at 22:29
  • @anna you should no have enough reputation to remove the downvote and add an upvote if this answer really helped you. – Mureinik May 19 '15 at 06:22
0

Reading the OP's question, I noticed the pointed exclusion of methods relying on PATH, and disregarded the obvious answer for which (because that is precisely what it does). The which program, by the way was originally a C-shell script although there are other implementations (FreeBSD, and bash for instance). Solaris, for instance, has a version little changed from 4.2BSD, and its manual page states

Both aliases and path are taken from the user's .cshrc file.

If you do not use C-shell, that original version of which has limited usefulness. Because there is no standard version, the results (and capability of the tool) can vary from system to system. Some shells (such as ksh) have similar features, but named differently, e.g., whence in the case of ksh (bash supplies the ksh alias type but not the `whence keyword).

I do not use which much, having better tools (conflict and path). But having ignored the obvious, the question as worded could easily pose a more interesting question: how can an application determine the directory from which another application was run. Not the obvious "where is the executable?", but "what was the working directory when the program was run?". There was a similar question recently dealing with relative pathnames.

On some of the systems which support the /proc pseudo-filesystem, there is a cwd node which gives the current working directory of a given process. The question Find out current working directory of a running process? goes into some detail regarding this.

xterm's optional feature "spawn-new-terminal" (added in patch #225 in 2007) uses this information to make a new terminal instance run in the same working directory as the user's current shell working directory.

A script could use this by starting with the process-id for the command in question, and inspecting the /proc tree for the corresponding cwd node.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105