Extract target of alias from Terminal

aliasterminal

In my Finder windows I have many aliases that target other folders or documents. At the shell prompt in Terminal, these aliases show up as regular files. How can I get the target of an alias so that, for example, I can "cd" to it or perform other command-line operations on the alias target?

Best Answer

To enable cd'ing into an Folder Alias I've found the following at Mac OS X Hints. Don't know if it still works.

Here is the bash function I include in .bashrc. Note that the second elif requires double brackets for the test, because it include the logical operator or (||):

function cd {
  if [ ${#1} == 0 ]; then
    builtin cd
  elif [ -d "${1}" ]; then
    builtin cd "${1}"
  elif [[ -f "${1}" || -L "${1}" ]]; then
    path=$(getTrueName "$1")
    builtin cd "$path"
  else
    builtin cd "${1}"
  fi
}

And here's the C source code for getTrueName.

A similar approach is available at superuser.com