此处为老的 LinuxGem,新版 LinuxGem 请移步

使用 CDPATH 变量在目录间跳转

galeki posted @ 2008年11月26日 04:46 in Bash 和它的朋友们 with tags cdpath bash , 4492 阅读

PATH 环境变量大家都熟悉,当你敲入命令,shell 按照 PATH 中的目录顺序来查找可执行文件。

CDPATH 和 PATH 的用法完全相同,只不过 CDPATH 控制的是执行 cd 目录跳转时的查找顺序。

假如在 ~/works 下,有很多项目的文件夹:

  |__ red
  |__ blue
  |__ yellow
  |__ green
  |__ dark
  |__   ...

这样就可以设置 CDPATH 为:

export CDPATH

这样,在任何当前目录下,都可以直接 cd red 进入 ~/works/red 文件夹了。

CDPATH 和 PATH 一样,也是用冒号来分隔不同的目录,并且按照先后顺序查找,唯一的区别就是,CDPATH 的第一字符就是一个冒号,是一个表示当前目录的空项,因为 cd 总是应该优先进入当前文件夹中的目录,所以如果当前目录下有个 red 文件夹,那么 cd red 就不会进入到 ~/works/red 下了。

chisiyuan 说:
2008年12月28日 08:17
我用bash写了function,也是方便cd命令的使用,相当于给当前目录加入书签,以后不管在哪个目录可以直接“cdk 书签名”进入。

Robinjack 说:
2020年12月28日 00:19

I like the helpful information you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn many new stuff right here! Good luck for the next! handyman asheville nc

안전놀이터 说:
2021年1月17日 19:51

Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish just about gossips and net and this is actually irritating. A good blog with interesting content, this is what I need. Thank you for keeping this website, I’ll be visiting it. Do you do newsletters? Can not find it.

Robinjack 说:
2021年2月17日 00:29

Can I say that of a relief to locate somebody that in fact knows what theyre talking about over the internet. You definitely know how to bring a challenge to light and produce it important. The diet ought to check out this and understand why side in the story. I cant believe youre not more well-liked when you absolutely provide the gift. retractable garage door screen kits

cmd368 说:
2021年4月06日 21:57

sometimes skinny jeans are not comfortable to wear, i would always prefer to use loos jeans,,

Robinjack 说:
2021年4月24日 20:17

Top-notch post it is definitely. My boss has been searching for this content. Monthly Income Review

80s clothing 说:
2021年5月26日 13:40

Wow, nice website you have here. I hope you will keep updating. Regards TJ

Robinjack 说:
2021年7月01日 18:40

dress shops that offer discounts are very common in our place and i always shop at them,. On line Baccarat Activity

登录 *

loading captcha image...
or Ctrl+Enter