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?
6 Answers
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.
-
3Is there a way to get the same result as your osascript but without the program running ? Aug 24, 2018 at 14:12
If the program is running, you can call
ps -ef | grep PROGRAM
On MAC (OS X) you can do:
- Locate the application in Finder.
- Right-click the application and select "Show Package Contents."
- Locate the executable file: Typically, this is in Contents → MacOS, and has the same name as the application.
- 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
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
-
1That's an impressive chuck of code. Does it do anything that
type
andtype -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, 2015 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 subdirectoryContents/MacOS
making it quite difficult to use any cross-platform techniques to determine the location of the application (hence the use oflsregister
) 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'sdefaults/pref
dir.– tresfMay 29, 2015 at 14:37 -
On Ubuntu,
/usr/bin/firefox
isn't actually the Firefox binary, so I use the output ofreadlink
to locate where it points to, and then find thedefaluts/prefs
directory from there. On a side note, about the symlinking: Doingls -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.– tresfMay 29, 2015 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. :)– tresfMay 29, 2015 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, 2015 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
The path of binaries is referenced in the $PATH
variable.
You can see its content with env
.
-
-
Your answer isn't entirely clear, sorry. What do you mean with "error in os… sorry"? Also the
$PATH
can be viewed withenv
, butecho $PATH
would be less verbose.– slhckMar 22, 2013 at 22:07