The Computer Oracle

Mac Terminal 'cd' to a folder alias

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Mac Terminal 'Cd' To A Folder Alias
00:24 Accepted Answer Score 13
01:49 Thank you

--

Full question
https://superuser.com/questions/253984/m...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #terminal

#avk47



ACCEPTED ANSWER

Score 13


Mac OS aliases are more similar to Windows shortcuts than to Unix symlinks; you can double-click them but you cannot cd into them.

This article explains how to make cd follow OS X aliases:

This is a two-part process requiring a little familiarity with gcc and bash, but I’ll try to make it as simple as possible. Firstly, you need this file: getTrueName.c. This file was created by Thos Davis and is licensed under the GPLv2. Save it anywhere, then compile it with the following command:

gcc -o getTrueName -framework Carbon getTrueName.c

This will create the ‘getTrueName’ executable in the same directory as the source. You can add it to your PATH, or just copy it directly to /usr/bin so it’s easy to access.

Interestingly, when Terminal opens a new shell, .bashrc is not executed as you might expect. Instead, under the login shell, .bash_profile is executed. So, add the following to .bash_profile in your Home directory. You might need to create it first; it isn’t there by default.

cd() {
  if [[ -f "$1" || -L "$1" ]]; then
    path=$(getTrueName "$1")
    builtin cd "$path"
  else
    builtin cd "$@"
  fi
}

[edited the function a bit –grawity]