How can I cd to an alias directory in the Mac OSX terminal

MacosBash

Macos Problem Overview


Is there a way to get into an alias directory from shell with the command "cd" ? It always returns that "htdocs" isn't a directory.

Edit: I made the shortcut with the OS GUI -> rightclicked the htdocs directory and chose "Alias..." (i'm using a german OS if it's not alias maybe it's called shortcut in english?) then i moved it to my home directory (because my terminal starts from there when i open it).

All i want is to open my terminal and type "cd htdocs" so that i can work from there.

Macos Solutions


Solution 1 - Macos

you can make symbolic link to it.

ln -s EXISTING_PATH LINK_NAME

e.g.

ln -s ~/Documents/books ~/Desktop/

Reference

https://stackoverflow.com/questions/7920967/enter-into-a-directory-through-an-alias-in-mac-os-x-terminal

Solution 2 - Macos

> All i want is to open my terminal and type cd htdocs so that i can work from there.

The easier approach is probably to ignore the links and add the parent directory of your htdocs directory to the CDPATH environment variable. bash(1) will check the contents of the CDPATH environment variable when you type cd foo to find the foo directory in one of the directories listed. This will work no matter what your current working directory is, and it'll be easier than setting symbolic links.

If the path to your htdocs is located /srv/www/htdocs/, then you could use CDPATH=/srv/www. Then, cd foo would first look for /srv/www/foo/ and change to it if it exists; if not, then it would look for foo in the current working directory and change to it if it exists. (This might get confusing if you have multiple htdocs directories on your system; in that case, CDPATH=.:/srv/www would let you change into a child directory easily but still use the /srv/www/htdocs/ version if no ./htdocs directory is present.)

You can add the CDPATH=/srv/www line to your ~/.bashrc file so it works every time you start a terminal.

Solution 3 - Macos

I personally use this to quickly work in the directory which is present deep inside one of my Volumes in my Mac.

Open your ~/.bash_profile, create an alias to the directory by adding this:

alias cdh="cd /Volumes/Haiku/haiku/src/apps/superprefs"

Save it, restart your terminal. Now on typing cdh in your terminal should change the working directory to the one mentioned as the alias.

Solution 4 - Macos

I am not sure how OSX exposes Alias links but since you are using bash you can just create a variable in your .bashrc file.

On its own line put:

htdocs=YourDirectoryPath/

Once you have restarted bash you can just type cd $htdocs

Solution 5 - Macos

There is a old hint on macworld to do this in a way that is integrated with BASH: Enable 'cd' into directory aliases from the Terminal

Plus, here is an answer that uses this solution on superuser.

Solution 6 - Macos

You may be able to use osascript to do this -- this command seems to work:

>cd "`osascript -e "on run aFile" -e "set aFile to POSIX file aFile as alias" -e "tell application """Finder""" to return POSIX path of ( ( original item of aFile ) as text ) " -e "end run" path_to_my_Finder_alias 2>/dev/null`"

Basically this command is running an AppleScript that finds the destination path of the argument (path_to_my_Finder_alias) in a subshell, then wraps it in double quotes, and changes the directory to it.

Maybe someone with a little more bash expertise can turn it into a bash alias or function.

Solution 7 - Macos

try:

alias cdgo=`echo cd /root/go/`

cdgo will run, then get command "cd /root/go/" and enter, and it will change your directory in current terminal process

It works on my centos, no test with osx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncodepushrView Question on Stackoverflow
Solution 1 - MacosMarvin WView Answer on Stackoverflow
Solution 2 - MacossarnoldView Answer on Stackoverflow
Solution 3 - MacosAnirudh MView Answer on Stackoverflow
Solution 4 - MacosGibronView Answer on Stackoverflow
Solution 5 - MacosThomasWView Answer on Stackoverflow
Solution 6 - MacosBryanView Answer on Stackoverflow
Solution 7 - MacosDavidView Answer on Stackoverflow