不可错过的cd命令,你知道cd -、cd ~和切换空格目录等内容吗?_centos

cd 命令详细介绍

cd (​​change directory​​)改变目录,即,切换目录。用于从当前目录切换到另一个指定的目录。

cd 后面的目录路径,可以是相对目录,也可以是绝对目录。

​./​​​ 相对路径,表示当前目录;​​../​​​ 相对路径,表示上一级目录。​​/xxx​​ 反斜杠开头的表示绝对路径。


​/​​​ 表示根目录,所有的内容都从根目录开始;​​/root​​ 表示root用户的家目录。

对于家目录(​​Home Directory​​​),除了root用户之外,所有用户的家目录都位于 ​​/home/​​​ 下(​​/home/<user_name>​​)。



  • Absolute Path(绝对路径):从根目录开始的路径。
  • Relative Path(相对路径):相对于当前位置开始的路径,如 ​​./​​ 、​​../​​。

cd切换命令演示

Linux中涉及到目录的所有命令,都可以通过 tab 键对路径补全。即输入路径名的一部分,按 tab 键补全其余名称。

​cd ../​​​ 或 ​​cd ../​​ 切换到相对当前的上一级目录

[root_test@VM_0_15_centos test]$ cd ../[root_test@VM_0_15_centos ~]$ 复制代码

​./​​开头,或直接以目录名(文件名)开头,表示当前目录。

如下切换到当前目录下的test目录中。

[root_test@VM_0_15_centos ~]$ cd test/[root_test@VM_0_15_centos test]$ cd ../[root_test@VM_0_15_centos ~]$ cd ./test/[root_test@VM_0_15_centos test]$复制代码

切换到绝对目录 ​​/etc/my.cnf.d​​。

[root_test@VM_0_15_centos test]$ cd /etc/my.cnf.d[root_test@VM_0_15_centos my.cnf.d]$复制代码

help 查看 cd 命令的帮助


cd命令是shell的内置命令,使用 man 查看时会显示 shell 的帮助信息,而不是 cd 的。

$ man cd
BASH_BUILTINS(1) General Commands Manual BASH_BUILTINS(1)

NAME
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command,
compgen, complete, compopt, continue, declare, dirs, disown, echo,
enable, eval, exec, exit, export, false, fc, fg, getopts, hash, help,
history, jobs, kill, let, local, logout, mapfile, popd, printf, pushd,
pwd, read, readonly, return, set, shift, shopt, source, suspend, test,
times, trap, true, type, typeset, ulimit, umask, unalias, unset, wait -
bash built-in commands, see bash(1)

BASH BUILTIN COMMANDS
......
复制代码


​help cd​​ 查看帮助命令:

$ 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.
......
复制代码

cd - 切换到上一次的目录

​cd -​​ 命令用于直接切换到上一次的目录,即两个目录之前来回切换,非常方便。

[root_test@VM_0_15_centos ~]$ cd /etc/sysconfig/network-scripts/
[root_test@VM_0_15_centos network-scripts]$ pwd
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$ cd -
/home/root_test
[root_test@VM_0_15_centos ~]$ cd -
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$
复制代码

​​cd ~​​ 或 ​​cd $home​​ 返回家目录

​cd ~​​​ 命令用于快速回到用户家目录(Home Directory)。对于 root 用户为​​/root​​​;对于其他用户为​​/home/user_name​​。

[root_test@VM_0_15_centos tmp]$ pwd
/var/tmp
[root_test@VM_0_15_centos tmp]$ cd ~
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
复制代码


​$​​​ 或 ​​#​​ 前面的 ~ 即表示当前是家目录。

~(波浪号——tilde)是家目录的缩写。


​cd $home​​ 同样用于回到家目录。

root 用户使用 cd ~username 切换到指定用户的家目录

cd ~username​cd ~username/​ 用于切换到指定用户的家目录,但是,只能root用户执行,具有管理员权限的用户,使用 sudo 执行也无效。

比如,root_test用户,尝试切换到 root_test1 和 root_test2 用户,均失败!

[root_test@VM_0_15_centos ~]$ cd ~root_test2
-bash: cd: /home/root_test2: Permission denied
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test1
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test2
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
复制代码

root用户可以正常切换。

[root@VM_0_15_centos ~]# pwd
/root
[root@VM_0_15_centos ~]# cd ~root_test2
[root@VM_0_15_centos root_test2]# pwd
/home/root_test2
复制代码

三种方法切换到有空格的目录名中

如下,存在一个有空格的目录 ​​my space dir​​:

$ ls -l
total 12
-rw-rw-r-- 1 root_test root_test 2 Oct 29 08:52 123
drwxrwxr-x 2 root_test root_test 4096 Oct 29 08:52 my
drwxrwxr-x 2 root_test root_test 4096 Oct 30 10:28 my space dir
-rw-rw-r-- 1 root_test root_test 0 Oct 26 21:52 my.txt
复制代码

一下三种方式均可使用 tab 键补全。

使用 \ 转义字符

[root_test@VM_0_15_centos test]$ cd my\ space\ dir/
[root_test@VM_0_15_centos my space dir]$ pwd
/home/root_test/test/my space dir
复制代码

使用双引号包含目录名

[root_test@VM_0_15_centos test]$ cd "my space dir"/
[root_test@VM_0_15_centos my space dir]$
复制代码

使用单引号

[root_test@VM_0_15_centos test]$ cd 'my space dir'/
[root_test@VM_0_15_centos my space dir]$
复制代码

另,​​cd !$​​,表示把上个命令的参数作为 cd 参数使用

作者:代码迷途