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

3,056 阅读3分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

欢迎评论指正文中的不足之处!

linux极简小知识:41、最常用的cd命令详解,cd -、cd ~和切换空格目录等的介绍

本篇是 linux极简小知识 系列的第41篇,介绍真实有用的linux知识,欢迎阅读查看!

cd 命令

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 ~usernamecd ~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 参数使用