Linux中cd命令

时间:2020-03-05 15:26:03  来源:igfitidea点击:

命令cd用于在Linux中的目录之间导航。
实际上,cd代表“更改目录”。

它使我们可以将工作目录从当前目录更改为想要导航到的所需目录。

cd命令的语法如下:

cd [option] <directory>

[option]用于控制命令的输出。
大多数情况下,我们将不会使用这些选项。

cd命令的可用选项与符号链接有关:

  • -P:不遵循符号链接。
  • -L:遵循符号链接。

<directory>是我们指定要导航到的所需目录的路径的位置。

在开始深入研究cd命令之前,让我们重新收集另外两个命令pwd和ls。
在Linux中处理目录时,这些命令很重要。

什么是pwd命令?

Linux中的命令pwd用于确定我们正在使用的目录。

pwd

对我来说,命令的输出如下所示。

Hyman@theitroad:~/parent$pwd
/home/abhi/parent

什么是ls命令?

Linux中的lscommand用于列出当前工作目录的内容。

ls

命令的输出如下所示。

Hyman@theitroad:~/parent$ls
child1 'child directory'

现在,我们已经简要了解了cd,pwd和ls命令,让我们看一下cd命令的一些示例。

Linux中cd命令的7个基本示例

这是cd命令的最常见用法。
其中一些我们可能已经知道。
其中一些不是很流行,但非常有用。

Tip: When typing a directory name, just hit tab after typing a few letters. It will show you all the options starting with those letters. Tab completion is a must use Linux terminal shortcut.

1.切换到根目录:

根目录是Linux文件系统中最重要的目录。
它是文件系统中所有其他目录的父目录。
用/表示。
我们可以使用以下命令从任何其他目录导航到根目录。

cd /

输出:

Hyman@theitroad:~/parent$cd /
Hyman@theitroad:/$pwd
/
Hyman@theitroad:/$

2.切换到子目录:

另一个目录中存在的目录称为子目录。
包含子目录的目录是父目录。
我们可以使用以下命令导航到子目录:

cd <child directory name>

输出:

Hyman@theitroad:~/parent$ls
child 'child directory'
Hyman@theitroad:~/parent$cd child1
Hyman@theitroad:~/parent/child1$pwd
/home/abhi/parent/child1
Hyman@theitroad:~/parent/child1$

注意:如果目录名称包含两个或者多个单词,则将目录名称括在“”内。

Hyman@theitroad:~/parent$ls
child1 'child directory'
Hyman@theitroad:~/parent$cd "child directory"
/home/abhi/parent/child directory
Hyman@theitroad:~/parent/child directory$

3.使用绝对路径名

从根目录(/)开始的路径名称为绝对路径名。
通过跟踪从根目录到目标目录的路径,可以获得绝对路径名。
绝对路径名始终从根目录开始。

Hyman@theitroad:/$cd /home/abhi/parent
Hyman@theitroad:~/parent$pwd
/home/abhi/parent
Hyman@theitroad:~/parent$

4.使用相对路径名

从当前工作目录开始的路径名称为相对路径名。
通过跟踪从当前工作目录到目标目录的路径,可以获得相对路径名。
相对路径名始终从当前工作目录开始。

Hyman@theitroad:~/parent$ls
child1 'child directory'
Hyman@theitroad:~/parent$cd child1
Hyman@theitroad:~/parent/child1$

5.使用..进入目录

..是每个目录中都有一个特殊的链接,它指向其父目录。
..是一个隐藏的链接。
要导航到子目录上一级的父目录,可以使用以下命令。

cd ..

这是输出:

Hyman@theitroad:~/parent/child directory$pwd
/home/abhi/parent/child directory
Hyman@theitroad:~/parent/child directory$cd ..
Hyman@theitroad:~/parent$pwd
/home/abhi/parent
Hyman@theitroad:~/parent$

我们也可以使用..所需的次数导航到任何更高级别的目录。
以下示例显示了从当前工作目录导航到更高级别的两级目录。

Hyman@theitroad:~/parent/child1/child2$pwd
/home/abhi/parent/child1/child2
Hyman@theitroad:~/parent/child1/child2$cd ../..
Hyman@theitroad:~/parent$pwd
/home/abhi/parent
Hyman@theitroad:~/parent$

6.切换回上一个目录

当需要从当前工作目录导航回到上一个工作目录时,可以使用–选项。

cd 

输出为:

Hyman@theitroad:~/parent/child1/child2$pwd
/home/abhi/parent/child1/child2
Hyman@theitroad:~/parent/child1/child2$cd ../..
Hyman@theitroad:~/parent$pwd
/home/abhi/parent
Hyman@theitroad:~/parent$cd 
/home/abhi/parent/child1/child2
Hyman@theitroad:~/parent/child1/child2$pwd
/home/abhi/parent/child1/child2
Hyman@theitroad:~/parent/child1/child2$

7.切换回主目录

~用于从任何其他目录导航回到主目录。

cd ~

输出为:

Hyman@theitroad:~/parent/child1/child2$cd ~
Hyman@theitroad:~$pwd
 /home/abhi
Hyman@theitroad:~$

实际上,在许多Linux发行版中,我们只需键入cd并输入即可返回到主目录。