bash 如何在 Mac OSX 终端中 cd 到别名目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7781500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:07:02  来源:igfitidea点击:

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

macosbash

提问by codepushr

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.

有没有办法使用命令“cd”从 shell 进入别名目录?它总是返回“htdocs”不是目录。

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).

编辑:我使用 OS GUI 创建了快捷方式 -> 右键单击​​ htdocs 目录并选择“别名...”(如果它不是别名,我使用的是德国操作系统,也许它在英语中称为快捷方式?)然后我将它移到我的主目录(因为当我打开它时我的终端从那里开始)。

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

我想要的只是打开我的终端并输入“cd htdocs”,以便我可以从那里工作。

回答by Marvin W

you can make symbolic linkto it.

您可以对其进行符号链接

ln -s EXISTING_PATH LINK_NAME

e.g.

例如

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

Reference

参考

Enter into a directory through an alias in Mac OS X terminal

在 Mac OS X 终端通过别名进入目录

回答by sarnold

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

我想要的只是打开我的终端并输入,cd htdocs以便我可以从那里工作。

The easier approach is probably to ignore the links and add the parent directory of your htdocsdirectory to the CDPATHenvironment variable. bash(1)will check the contents of the CDPATHenvironment variable when you type cd footo find the foodirectory 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.

更简单的方法可能是忽略链接并将目录的父目录添加htdocsCDPATH环境变量中。当您键入以在列出的目录之一中查找目录时bash(1),将检查CDPATH环境变量的内容。无论您当前的工作目录是什么,这都可以工作,并且比设置符号链接更容易。cd foofoo

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

如果您的路径htdocs已找到/srv/www/htdocs/,那么您可以使用CDPATH=/srv/www. 然后,如果它存在,cd foo将首先查找/srv/www/foo/并更改它;如果没有,那么它会foo在当前工作目录中查找,如果存在则更改为该目录。(如果您htdocs的系统上有多个目录,这可能会让人感到困惑;在这种情况下,CDPATH=.:/srv/www您可以轻松切换到子目录,但/srv/www/htdocs/如果没有./htdocs目录,则仍可使用该版本。)

You can add the CDPATH=/srv/wwwline to your ~/.bashrcfile so it works every time you start a terminal.

您可以将该CDPATH=/srv/www行添加到您的~/.bashrc文件中,以便每次启动终端时它都可以工作。

回答by Anirudh M

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

我个人使用它来快速在我的 Mac 中我的卷之一中存在的目录中快速工作。

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

打开您的~/.bash_profile,通过添加以下内容为目录创建别名:

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

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

保存它,重新启动您的终端。现在cdh在您的终端中输入应该将工作目录更改为作为别名提到的目录。

回答by Gibron

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

我不确定 OSX 如何公开 Alias 链接,但由于您使用的是 bash,因此您可以在.bashrc文件中创建一个变量。

On its own line put:

在它自己的行上放:

htdocs=YourDirectoryPath/

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

重新启动 bash 后,您只需键入 cd $htdocs

回答by ThomasW

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

macworld 上有一个旧提示,可以通过与 BASH 集成的方式执行此操作:Enable 'cd' into directory aliases from the Terminal

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

另外,这是在超级用户上使用此解决方案的答案

回答by Bryan

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

您可以使用 osascript 来执行此操作——此命令似乎有效:

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`"

cd "`osascript -e "on run aFile" -e "set aFile to POSIX file aFile as alias" -e "tell application "\""Finder"\"" 返回 ( (aFile 的原始项目) 的 POSIX 路径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.

基本上这个命令正在运行一个 AppleScript,它在子 shell 中找到参数的目标路径 (path_to_my_Finder_alias),然后将它用双引号括起来,并将目录更改为它。

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

也许具有更多 bash 专业知识的人可以将其转换为 bash 别名或函数。

回答by David

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

cdgo 将运行,然后获取命令“cd /root/go/”并输入,它将在当前终端进程中更改您的目录

It works on my centos, no test with osx

它适用于我的 centos,没有用 osx 测试