命令 光盘 用于在 Linux 中的目录之间导航。 它代表“更改目录”。
它使您能够将工作目录从当前目录更改为您希望导航到的所需目录。
cd 命令的语法如下:
cd [option] <directory>
[option] 用于控制命令的输出。 大多数时候您不会使用这些选项。
cd 命令的可用选项与符号链接有关:
- -P:不遵循符号链接。
- -L:跟随符号链接。
在我们开始深入研究 cd 命令之前,我建议重新审视 Linux 中绝对路径和相对路径的概念。 您将通过 cd 命令大量使用它们。
Linux 中 cd 命令的 7 个基本示例
以下是 cd 命令最常见的用法。 其中一些你可能已经知道了。 其中一些不是那么受欢迎但非常有用。
提示:输入目录名称时,只需在输入几个字母后按 Tab。 它将向您显示以这些字母开头的所有选项。 Tab 补全是必须使用的 Linux 终端快捷方式。
1.切换到根目录
根目录是Linux文件系统中最重要的目录。 它是文件系统中所有其他目录的父目录。 它用 / 表示。 您可以使用以下命令从任何其他目录导航到根目录。
cd /
输出:
[email protected]:~/parent$ cd /
[email protected]:/$ pwd
/
[email protected]:/$
2.切换到子目录
存在于另一个目录中的目录称为子目录。 包含子目录的目录是父目录。 您可以使用以下命令导航到子目录:
cd <child directory name>
输出:
[email protected]:~/parent$ ls
child 'child directory'
[email protected]:~/parent$ cd child1
[email protected]:~/parent/child1$ pwd
/home/abhi/parent/child1
[email protected]:~/parent/child1$
笔记: 当目录名称有两个或多个单词时,将目录名称括在“”中。
[email protected]:~/parent$ ls
child1 'child directory'
[email protected]:~/parent$ cd "child directory"
/home/abhi/parent/child directory
[email protected]:~/parent/child directory$
3.使用绝对路径名
从根目录 (/) 开始的路径名称为绝对路径名。 您可以通过跟踪从根目录到目标目录的路径来获得绝对路径名。 绝对路径名总是从根目录开始。
[email protected]:/$ cd /home/abhi/parent
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$
4.使用相对路径名
从当前工作目录开始的路径名称为相对路径名。 您可以通过跟踪从当前工作目录到目标目录的路径来获得相对路径名。 相对路径名总是从当前工作目录开始。
[email protected]:~/parent$ ls
child1 'child directory'
[email protected]:~/parent$ cd child1
[email protected]:~/parent/child1$
5.使用..上目录
.. 是每个目录中存在的一个特殊链接,它指向其父目录。 ..是一个隐藏的链接。 要导航到比子目录高一级的父目录,您可以使用以下命令。
cd ..
这是输出:
[email protected]:~/parent/child directory$ pwd
/home/abhi/parent/child directory
[email protected]:~/parent/child directory$ cd ..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$
您还可以使用 .. required 次数导航到任何更高级别的目录。 下列的 example 显示从当前工作目录到两级更高目录的导航。
[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ cd ../..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$
6.切换回上一个目录(很有用)
当您需要从当前工作目录导航回上一个工作目录时,您可以使用 – 选项。
cd -
输出是:
[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ cd ../..
[email protected]:~/parent$ pwd
/home/abhi/parent
[email protected]:~/parent$ cd -
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$ pwd
/home/abhi/parent/child1/child2
[email protected]:~/parent/child1/child2$
7.切换回主目录
~ 用于从任何其他目录导航回主目录。
cd ~
输出是:
[email protected]:~/parent/child1/child2$ cd ~
[email protected]:~$ pwd
/home/abhi
[email protected]:~$
事实上,在许多 Linux 发行版中,您只需键入 cd 并回车即可返回您的主目录。
我希望您对这些 cd 命令示例有更好的理解。 如果您对 cd 命令有任何疑问,请随时在下面的评论部分提出您的问题!