Linux中的cd命令可能比你想象中的知识点要多一些

10次阅读

共计 4143 个字符,预计需要花费 11 分钟才能阅读完成。

问题出现

如果有人问你,使用最多的 Linux 命令。很多程序员都会说出 cd 命令。毕竟,不管你干啥,首先得进入到对应目录吧。那有没有想过下面几个问题。

1.Linux 中的 cd 命令是内置命令还是外部命令?

2.Linux 中如何获取内置命令的帮助文档?

3. 有没有仔细了解过 cd 命令的帮助文档?

问题解答一

我们在使用 Linux 系统的时候,去执行某个命令。有时候是自己写的 shell 脚本命令。有的时候是去执行 cd/echo/kill 这些命令。从这里,就可以将 shell 命令分为两类,一类是外部命令 (自己写的 shell 脚本),一类是 shell 自带的命令 (cd/echo/kill), 也称为内置命令。

内置命令的效率是优于外部命令的。内置命令所对应的代码是随着 shell 的启动被加载到内存中,而外部命令运行是需要新开一个进程,所以使用内置命令是非常快速的。

如何判断是否是内置命令

1. 如果是内置命令

[root@VM_0_8_centos ~]# type cd 
cd is a shell builtin

builtin 的英文翻译就是内置命令。

2. 如果是自己写的 shell 脚本

[root@VM_0_8_centos ask]# type /ask/test.sh 
/ask/test.sh is /ask/test.sh

自己写的 shell 脚本是外部命令,它的位置是 /ask/test.sh

3. 常见的 find 命令

[root@VM_0_8_centos ask]# type find
find is /usr/bin/find

find 是一个外部命令,它的位置是 /usr/bin/find

问题解答二

bash 为每一个 shell 内置命令都提供了一个内置的帮助文档。使用 help 命令。然后跟着 shell 内置命令的名称即可查看。

[root@VM_0_8_centos ~]# 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 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

问题解答三

我们来一点点解答 cd 命令的帮助文档,并逐一做个验证。纸上得来终觉浅,绝知此事要躬行。

1.cd 命令的表示法

cd: cd [-L|[-P [-e]]] [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.

cd 命令后面可能有一个 - L 参数,也可能是 - P 参数。cd 的默认行为,等于 cd <=> cd -L。如果路径中包含符号连接,则跟着符号链接走。

1.1 创建符号链接引用目录

[root@VM_0_8_centos ask]# ln -s test test-sym
[root@VM_0_8_centos ask]# ls -l
total 1104
-rw-r--r-- 1 root root    4444 Mar 16 21:49 info.1
drwxr-xr-x 2 root root    4096 May 30 08:48 test
lrwxrwxrwx 1 root root       4 May 30 09:17 test-sym -> test

1.2 使用 cd 命令与 - L 组合

[root@VM_0_8_centos ask]# cd -L test-sym/
/ask/test-sym
[root@VM_0_8_centos test-sym]# pwd
/ask/test-sym

如果路径中包含符号连接,则跟着符号链接走。

1.3 使用 cd 命令与 - P 组合

[root@VM_0_8_centos ask]# cd -P test-sym/
/ask/test-sym/
[root@VM_0_8_centos test]# pwd
/ask/test

使用的是物理地址,不会则跟着符号链接走。

2.cd 命令默认的文件路径

 Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

也就是说,cd 命令后面如果不跟任何参数,默认就是进入用户的家目录。等价于 cd ~

[root@VM_0_8_centos ask]# cd 
[root@VM_0_8_centos ~]# pwd
/root

3.cd 命令的 CDPATH 变量使用

 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.

如果你输入的不是以根目录 / 开头的路径,cd 进入目录的时候,会根据你的 CDPATH 环境变量配置的目录去寻找,进入的目录即变成 CDPATH 中的目录 + 你的目录。

3.1 在.bash_profile 配置 CDPATH 环境变量

[root@VM_0_8_centos ~]# echo $CDPATH
/ask:/home/:/etc

3.2 在 ask 目录下创建 test 目录

[root@VM_0_8_centos ask]# mkdir test
[root@VM_0_8_centos ask]#

3.3 使用 cd 命令进入 test 目录

[root@VM_0_8_centos usr]# cd test
/ask/test
[root@VM_0_8_centos test]# pwd
/ask/test

我是在 /usr/ 目录下,切换到 test 目录的,它即是根据 CDPATH 环境变量去寻找的第一个存在 test 目录的。

4.cd 命令中的 cdable_vars 属性


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.

这段的意思是,如果一个目录名称在环境中没有,但是 cdable_vars 是开启的状态,如果你定义一个别名目录,那 cd 也可以识别进入这个目录。

4.1 环境中 cdable_vars 目前是 off 状态

shopt 命令用于显示和设置 shell 中的行为选项

[root@VM_0_8_centos test]# shopt cdable_vars
cdable_vars        off

4.2 自定义一个目录变量名称

[root@VM_0_8_centos usr]# askcto=/ask/test
[root@VM_0_8_centos usr]# cd askcto
-bash: cd: askcto: No such file or directory

发现 cd 命令识别该目录名称,提示没有这个目录

4.3 将 cdable_vars 激活为 on

[root@VM_0_8_centos usr]# shopt -s cdable_vars
[root@VM_0_8_centos usr]# shopt cdable_vars
cdable_vars        on

4.4 再次自定义一个目录变量名称

[root@VM_0_8_centos usr]# askcto=/ask/test
[root@VM_0_8_centos usr]# cd askcto
/ask/test
[root@VM_0_8_centos test]#

成功识别了自定义的目录名称 askcto

回顾总结

1.Linux 中的 cd 命令是内置命令,查看内置命令可以使用 help 命令 + 内置命令名称即可。

2. 讲解了 cd 命令 - L 与 - P 参数的区别。

3. 讲解了 cd 命令与 CDPATH 环境变量设置的关系以及 shell 中 cdable_vars 选项的设置对 cd 命令的影响。

正文完
 0