osx上使用'cd'命令跳转到别名(alias)目录

时间:2022-10-13 04:37:28

在mac上使用windows的共享目录时,在terminal中时法使用cd的,会提示"xxx 不是目录",经过一番的查找,发现了Mac Terminal 'cd' to folder alias,
经过本人验证时可以用的,具体步骤参考Make Terminal Follow Aliases Like Symlinks:

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.

 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
    }

Effectively, this looks for Finder aliases and resolves them before deferring to the builtin cd command. Append it to your .bash_profile, then either execute it or restart Terminal for the changes to take effect. Now you can cd to Finder aliases within Terminal and have them treated just like symlinks. Just like it should be.

在修改完.bashrc后只有在新打开terminal时才会有效,解决方法有:

1 每次操作前执行source .bashrc

2 一劳永逸的方法:将下面代码加到.base_profile文件后面

if [ "${BASH-no}" != "no" ]; then
  [ -r ~/.bashrc ] && . ~/.bashrc
fi

__ 注:我是使用hexo写的本篇,应该是hexo的markdown生成引擎有问题,文中的引用部分生成的完全不对,让我找找问题先。。。 __