Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 7 down vote accepted

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.

share|improve this answer
    
thank you very much –  Xitrum Mar 22 '13 at 21:30

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 and Ubuntu 12.04.

#!/bin/bash

# Array of possible Firefox application names.
appnames=("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
share|improve this answer
    
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 May 28 at 5:07
    
@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. –  QZ Support May 29 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. –  QZ Support May 29 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. :) –  QZ Support May 29 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) –  G-Man May 29 at 17:06

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
share|improve this answer

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

You can see its content with env.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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