How do I find the path to a program in Terminal?

29

7

I installed an application and now I can access it via terminal as myapplication. It is an alias, though. How can I find the full path to the file?

Xitrum

Posted 2013-03-22T21:06:41.560

Reputation: 433

Answers

31

You can use type and which to determine what a certain command in bash is, and, if it's an application, where it resides.

$ type type
type is a shell builtin
$ type cd
cd is a shell builtin
$ type ls
ls is aliased to `ls --color=auto'
$ type -P ls
/Users/danielbeck/bin/ls
$ which which
/usr/bin/which
$ which ls
/Users/danielbeck/bin/ls

The commands which and type -P only work for programs on your PATH, of course, but you won't be able to run others by just typing their command name anyway.


If you're looking for a simple way to determine where an OS X (GUI) application bundle is installed (as used e.g. by the open command), you can execute the following short AppleScript from the command line:

$ osascript -e 'tell application "System Events" to POSIX path of (file of process "Safari" as alias)'
/Applications/Safari.app

This requires that the program in question (Safari in the example) is running.

Daniel Beck

Posted 2013-03-22T21:06:41.560

Reputation: 98 421

1Is there a way to get the same result as your osascript but without the program running ? – Mathieu Westphal – 2018-08-24T14:12:58.237

3

If the program is running, call

ps -ef | grep PROGRAMM

Matthias M

Posted 2013-03-22T21:06:41.560

Reputation: 133

3

This is the method I currently to locate the Firefox application directory in OSX and Linux. Should be easy to adopt to another application. Tested on OSX 10.7, Ubuntu 12.04, Debian Jessie

#!/bin/bash

# Array of possible Firefox application names.
appnames=("IceWeasel" "Firefox")    # "Firefox" "IceWeasel" "etc

#
# Calls lsregister -dump and parses the output for "/Firefox.app", etc.
# Returns the very first result found.
#
function get_osx_ffdir()
{
    # OSX Array of possible lsregister command locations
    # I'm only aware of this one currently
    lsregs=("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister")

    for i in "${lsregs[@]}"; do
        for j in ${appnames[@]}; do
            if [ -f $i ]; then
                # Some logic to parse the output from lsregister
                ffdir=$($i -dump |grep -E "/$j.app$" |cut -d'/' -f2- |head -1)
                ffdir="/$ffdir"
                return 0
            fi
        done
    done
    return 1
}

#
# Uses "which" and "readlink" to locate firefox on Linux, etc
#
function get_ffdir()
{
    for i in "${appnames[@]}"; do
        # Convert "Firefox" to "firefox", etc
        lower=$(echo "$i" |tr '[:upper:]' '[:lower:]')
        # Readlink uses the symlink to find the actual install location
        # will need to be removed for non-symlinked applications
        exec=$(readlink -f "$(which $lower)")
        # Assume the binary's parent folder is the actual application location
        ffdir=$(echo "$exec" |rev |cut -d'/' -f2- |rev)
        if [ -f "$ffdir" ]; then
            return 0
        fi
    done
    return 1

}


echo "Searching for Firefox..."

ffdir=""
if [[ "$OSTYPE" == "darwin"* ]]; then
    # Mac OSX
    get_osx_ffdir
else
    # Linux, etc
    get_ffdir
fi

echo "Found application here: $ffdir"

# TODO: Process failures, i.e. "$ffdir" == "" or "$?" != "0", etc

tresf

Posted 2013-03-22T21:06:41.560

Reputation: 151

That's an impressive chuck of code.  Does it do anything that type and type -P don't?  Does the non-OSX code work on OS X?  If not, can you explain why not?  If yes, why do you have two versions? – G-Man Says 'Reinstate Monica' – 2015-05-28T05:07:47.120

1@G-Man: Due to character limitations, I'll answer this in two parts...

On OSX, 3rd party applications aren't installed to /bin or /usr/bin, etc but instead installed to /Applications/My3rdPartyApp.app and the binary is stored in a subdirectory Contents/MacOS making it quite difficult to use any cross-platform techniques to determine the location of the application (hence the use of lsregister) Worse yet, the OSX directory structure places the binary in a separate location from the resources. The snippet above was written to assist with locating Firefox's defaults/pref dir. – tresf – 2015-05-29T14:37:53.453

On Ubuntu, /usr/bin/firefox isn't actually the Firefox binary, so I use the output of readlink to locate where it points to, and then find the defaluts/prefs directory from there. On a side note, about the symlinking: Doing ls -al /usr/bin/* |grep -- '->' |wc -l illustrates about 303 binaries in that directory my Ubuntu configuration are actually symlinks. (about 16% of them) For this reason, the above code **should * eventually be modified to resolve symlinks recursively until it finds the canonical path to the binary. – tresf – 2015-05-29T14:53:28.740

Last, to answer the type -P question, I'm not familiar enough with that command to answer the question. Perhaps you can elaborate a bit. :) – tresf – 2015-05-29T14:54:53.520

Thanks for the detailed response.  (You might want to [edit] that information into your answer.)  I mention type because it was the basis of the first (& accepted) answer.  It is a shell builtin that indicates how specified name(s) would be interpreted if used as commands.  The POSIX version of it, which is rather bare-bones, is described here.  … (Cont’d)

– G-Man Says 'Reinstate Monica' – 2015-05-29T17:06:16.767

(Cont’d) …  The bash version of type supports a handful of options; see bash(1) or the Bash Reference Manual.  The -P option forces a PATH search for each name, even if it is also an alias, function, builtin, or keyword; but, to answer my own question, it doesn’t do readlink.

– G-Man Says 'Reinstate Monica' – 2015-05-29T17:06:32.903

Thanks for the explanation. If you feel the snippet has room for improvement, please do. I've intentionally tried to make the comments explanatory without being TL;DR, but if there is anything non-obvious, please add it in. – tresf – 2015-05-30T18:33:51.230

0

On MAC (OS X) you can do:

  1. Locate the application in Finder.
  2. Right-click the application and select "Show Package Contents."
  3. Locate the executable file: Typically, this is in Contents → MacOS, and has the same name as the application.
  4. Drag that file onto your blank Terminal command line.

Now you can see the full path to your application on the command line and run it from there if you want.

Taken from wikihow

Alex Sed

Posted 2013-03-22T21:06:41.560

Reputation: 101

0

The path of binaries is referenced in the $PATH variable.

You can see its content with env.

user209678

Posted 2013-03-22T21:06:41.560

Reputation: 11

error in os... sorry :'( – user209678 – 2013-03-22T21:42:46.043

Your answer isn't entirely clear, sorry. What do you mean with "error in os… sorry"? Also the $PATH can be viewed with env, but echo $PATH would be less verbose. – slhck – 2013-03-22T22:07:21.230

0

You can use "alias" command in terminal to list all of your aliases. Or if you are in a directory you can use "pwd" to show your current path.

If you know the filename or a part of filename, then you can use "find" to locate your file.

find / -name things.jpeg

Steve Butabi

Posted 2013-03-22T21:06:41.560

Reputation: 61