39

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?

0
45

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.

1
  • 1
    Is there a way to get the same result as your osascript but without the program running ? Aug 24 '18 at 14:12
5

If the program is running, you can call

ps -ef | grep PROGRAM
1
  • or save a pipe and just use pgrep -lf PROGRAM
    – luckman212
    Oct 25 '20 at 14:11
4

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
7
  • 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? May 28 '15 at 5:07
  • 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
    May 29 '15 at 14:37
  • 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
    May 29 '15 at 14:53
  • 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
    May 29 '15 at 14:54
  • 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) May 29 '15 at 17:06
3

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

1

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
0

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

You can see its content with env.

2
  • error in os... sorry :'(
    – user209678
    Mar 22 '13 at 21:42
  • 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
    Mar 22 '13 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.