
语法
cd [-L|[-P [-e]]] [dir]
改变shell当前工作目录。仅对执行命令所在的shell有效。
参数
-L 按符号链接进入目录。
-P 按物理链接进入目录
-e 如果指定-P,且路径不存在,则返回-e后指定的退出码。
说明
cd =change directory
进入主目录。不带参数dir,就默认进入当前用户的主目录。等价命令还有 cd ~ 或 cd $HOME。
进入上一次切换前的工作目录。cd - 或 cd $OLDPWD。如果反复执行,则会在最后两次工作目录间来回切换。
cd的默认行为,等于cd <=> cd -L。如果路径中包含符号连接,则跟着符号链接走。
CDPATH环境变量
CDPATH环境变量为cd 提供一组备选的当前目录。也就是说,如果你输入的不是/开头的绝对路径,则进入的目录的绝对路径是CDPATH中的目录+你的目录。举例来说,
架设你当前目录是/usr,myhome在你的/home下。
export CDPATH=/root:/home/:/etc
cd myhome
执行以上命令后,bash会依次将CDPATH中的目录+myhome来进入,最终进入到/home/myhome目录下。【CDPATH中多个路径下都有dir怎么办?】
进入你刚创建的目录
mkdir <dir> && cd $_
不论你创建的目录是否你当前工作目录下的,这个命令都能用。
创建只有一个下划线名字的目录
mkdir ./_
help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the
HOME shell variable. The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used. If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR. Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status The default is to follow symbolic links, as if `-L' were specified. Exit Status:
Returns if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.