00. 目录
01. 命令概述
cd命令是”change directory”中单词的首字母缩写,其英文释义是改变目录,所以该命令的功能是从当前目录切换到指定目录。
其中目录的路径可分为绝对路径和相对路径。若目录名称省略,则切换至使用者的用户目录(也就是刚登录时所在的目录)。
另外,“~”也表示为用户目录的意思,“.”则是表示目前所在的目录,“…”则表示当前目录位置的上一级目录。
cd 为最常用的命令,与 DOS 下的 cd 命令类似。
02. 命令格式
cd [选项] [目录名]
03. 常用选项
-p
如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录
-L
如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。
-
当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。
- 1
- 2
- 3
- 4
- 5
- 6
04. 参考示例
4.1 切换工作目录到当前工作目录(其实没有什么意义,主要介绍 .
表示当前目录)
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$ cd .
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
4.2 切换到当前目录的上一级目录(…表示上一级目录)
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$ cd ..
[deng@localhost home]$ pwd
/home
[deng@localhost home]$
- 1
- 2
- 3
- 4
- 5
- 6
4.3 返回上两级目录
[deng@localhost share]$ pwd
/home/deng/share
[deng@localhost share]$ cd ../..
[deng@localhost home]$ pwd
/home
[deng@localhost home]$
- 1
- 2
- 3
- 4
- 5
- 6
4.4 返回进入此目录之前所在的目录
[deng@localhost home]$ cd /home/deng
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$ cd share/
[deng@localhost share]$ pwd
/home/deng/share
[deng@localhost share]$ cd -
/home/deng
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4.5 进入到当前用户的家目录
[deng@localhost ~]$ cd /
[deng@localhost /]$ pwd
/
[deng@localhost /]$ cd ~
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
或者
[deng@localhost ~]$ cd /
[deng@localhost /]$ pwd
/
[deng@localhost /]$ cd
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4.6 使用相对路径切换到指定的目录
[deng@localhost ~]$ cd /
[deng@localhost /]$ pwd
/
[deng@localhost /]$ cd home/
[deng@localhost home]$ pwd
/home
[deng@localhost home]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4.7 使用绝对路径切换到指定的目录
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$ cd /tmp
[deng@localhost tmp]$ pwd
/tmp
[deng@localhost tmp]$
- 1
- 2
- 3
- 4
- 5
- 6
4.8 把上个命令的参数作为cd参数使用 !$表示
[deng@localhost ~]$ pwd
/home/deng
[deng@localhost ~]$ ls -ld share
drwxrwxr-x 4 deng deng 28 7月 11 09:47 share
[deng@localhost ~]$ cd !$
cd share
[deng@localhost share]$ pwd
/home/deng/share
[deng@localhost share]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.9 切换至带有空格的目录
[root@localhost ~]# mkdir "test dir"
[root@localhost ~]# cd test\ dir/
[root@localhost test dir]# pwd
/root/test dir
[root@localhost test dir]#
- 1
- 2
- 3
- 4
- 5
或者 使用单引号
[root@localhost ~]# cd 'test dir'
[root@localhost test dir]# pwd
/root/test dir
[root@localhost test dir]#
- 1
- 2
- 3
- 4
或者使用双引号
[root@localhost ~]# cd "test dir"
[root@localhost test dir]# pwd
/root/test dir
[root@localhost test dir]#
- 1
- 2
- 3
- 4
4.10 使用通配符(用得比较少)
[deng@localhost test]$ mkdir test1 test2 task
[deng@localhost test]$ cd t*
[deng@localhost task]$ pwd
/home/deng/test/task
[deng@localhost task]$
- 1
- 2
- 3
- 4
- 5
温馨提示:如果只有一个目录以 t 开头,将会移动到该目录;如果有很多目录以 t 开头,将会移动到第一个以 t 开头的目录(按照字典里字母的顺序)。
05. 附录
</div>