简体   繁体   English

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

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

Is there a way to get into an alias directory from shell with the command "cd" ?有没有办法使用命令“cd”从 shell 进入别名目录? It always returns that "htdocs" isn't a directory.它总是返回“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”,以便我可以从那里工作。

you can make symbolic link to it.您可以对其进行符号链接

ln -s EXISTING_PATH LINK_NAME

eg例如

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

Reference参考

Enter into a directory through an alias in Mac OS X terminal 在 Mac OS X 终端通过别名进入目录

All i want is to open my terminal and type cd htdocs so that i can work from there.我想要的只是打开我的终端并输入cd htdocs以便我可以从那里工作。

The easier approach is probably to ignore the links and add the parent directory of your htdocs directory to the CDPATH environment variable.更简单的方法可能是忽略链接并将htdocs目录的父目录添加到CDPATH环境变量中。 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.当您键入cd foo以在列出的目录之一中查找foo目录时, bash(1)将检查CDPATH环境变量的内容。 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 .如果您的htdocs的路径位于/srv/www/htdocs/ ,那么您可以使用CDPATH=/srv/www Then, cd foo would first look for /srv/www/foo/ and change to it if it exists;然后, cd foo将首先查找/srv/www/foo/并在它存在时更改它; if not, then it would look for foo in the current working directory and change to it if it exists.如果不是,那么它将在当前工作目录中查找foo并在它存在时更改为它。 (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.) (如果您的系统上有多个htdocs目录,这可能会令人困惑;在这种情况下, CDPATH=.:/srv/www可以让您轻松切换到子目录,但如果没有,仍然使用/srv/www/htdocs/版本./htdocs目录存在。)

You can add the CDPATH=/srv/www line to your ~/.bashrc file so it works every time you start a terminal.您可以将CDPATH=/srv/www行添加到您的~/.bashrc文件中,这样每次启动终端时它都可以工作。

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 cdh in your terminal should change the working directory to the one mentioned as the alias.现在在终端中输入cdh应该将工作目录更改为作为别名提到的目录。

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.我不确定 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

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 .另外,这是在超级用户上使用此解决方案的答案

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 别名或函数。

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 测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM