在 Linux 中使用 cd 命令
cd
![](/data/attachment/album/202307/04/154137kxcv8y8z854555mp.jpg)



> 
> 了解如何使用用于切换目录的一个基本但必不可少的 Linux 命令。
> 
> 
> 


Linux 中的 `cd` 命令用于更改目录。`cd` 实际上是“<ruby> 更改目录 <rt>  change directories </rt></ruby>”的缩写。


这是你必须了解的 [基本 Linux 命令](https://itsfoss.com/essential-ubuntu-commands/)之一。


使用 `cd` 命令非常简单：



```
cd path_to_directory

```

不可能比这更简单了，对吧。


然而，你应该理解它的路径部分，以便轻松地浏览 [文件系统](https://linuxhandbook.com:443/linux-directory-structure/) 而不会感到困惑。


这是绝对路径和相对路径的快速回顾：


![Absolute vs relative path in Linux](/data/attachment/album/202307/04/154203jcoaxmn57oncxxjq.png)


如果你需要更多细节，我建议你阅读这篇文章：



> 
> **[Linux 上的绝对路径和相对路径有什么不同](https://linuxhandbook.com/absolute-vs-relative-path/?ref=itsfoss.com)**
> 
> 
> 


让我们看一些使用 `cd` 命令的示例。


### 使用绝对路径更改目录


从视觉上看会更容易理解。看下图。


![Absolute path travel to the python directory](/data/attachment/album/202307/04/154203ar8l8xhmje1mt14h.png)


我当前的位置是我的主目录（`/home/abhishek`），我必须进入 `scripts` 目录中的 `python` 目录。


假设我想使用绝对路径。`python` 目录的绝对路径是 `/home/abhishek/scripts/python`。



```
cd /home/abhishek/scripts/python

```

![cd command with absolute path](/data/attachment/album/202307/04/154204jk5iccsqawiat27v.png)


### 使用相对路径更改目录


让我们举同样的例子，但这次我将采用相对路径。


![Relative path example](/data/attachment/album/202307/04/154205gzzv5fjy9jzjyyko.png)


我的主目录到 `python` 目录的相对路径是 `scripts/python`。让我们用这个：



```
cd scripts/python

```

![cd command with relative path](/data/attachment/album/202307/04/154205n9bf0cdfcgzsllgx.png)


### 进入上级目录


到目前为止，你一直在进入下级。如果你必须进入上级目录怎么办？


假设你位于 `/home/abhishek/scripts/python` 中，并且必须将目录添加到 `scripts`？


![](/data/attachment/album/202307/04/154206gvjv6qj2wwwxyzip.png)


使用绝对路径始终是一种选择，但它相当冗长。相反，你可以使用特殊的目录符号 `..`。双点 （`..`）表示父目录或上一级目录。单点（`.`）表示当前目录。



```
cd ..

```

这是一个例子：


![cd up a directory](/data/attachment/album/202307/04/154206qgk02gq3lnyhdlcu.png)


你可以使用 `..` 在 Linux 文件系统层次结构中向上移动路径。


假设我在上图中的 `python` 目录中，想要进入 `code` 目录。这是我能做的：



```
cd ../../code

```

![Go up the directory using cd command](/data/attachment/album/202307/04/154207xiti11gugmgn4g8g.png)


### 进入主目录


如果你在所有这些目录切换中感到迷失并想回到主目录，有很多简单的快捷方式。


事实上，最简单的就是使用不带任何选项的 `cd` 命令。



```
cd

```

这将使你从文件系统上的任何位置返回主目录。


或者，你可以使用 `~` 符号，表示主目录。



```
cd ~

```

![Use cd to go back home](/data/attachment/album/202307/04/154207cjzjnun2my992soj.png)


### 进入根目录


尽管你不会像前一个那样经常使用它，但了解一下仍然有好处。


如果你想返回文件系统开始的根目录，请使用以下命令：



```
cd /

```

这里不涉及“魔法”。当放在路径开头使用时，`/` 表示根。不要将它与路径分隔符混淆。


![Paths in Linux](/data/attachment/album/202307/04/154209baue55gzzttc5g9x.png)


### 切换回上一级目录


这是一个救命稻草，或者我应该说是“省时稻草”。当你深入目录结构，然后转到另一个目录，然后你觉得需要返回到以前的位置时，此快捷方式会有所帮助。



```
cd -

```

还不清楚吗？ 让我举个例子。


我位于 `/etc/apt/sources.list.d`。从这里，进入 `/home/abhishek/scripts/python` 来处理我的代码。然后我意识到我必须再次检查 `/etc/apt/sources.list.d` 目录中的某些内容。


通常的方法是这样做，这让我再次输入所有路径：


![Go back to previous directory](/data/attachment/album/202307/04/154209a2szpzzhc27tu3u3.png)


但聪明的方法是使用这个：


![Use cd - to go back to previous directory](/data/attachment/album/202307/04/154209oolowwulfirtrooo.png)


看吧，无需再次输入冗长的路径。如期工作！


### ?️ 练习时间


如果你想练习 `cd` 命令，这里有一些练习供你使用。


* 打开终端并进入 `/var/log` 目录。[检查目录内容](https://itsfoss.com/list-directory-content/)。你看到了什么？
* 现在，进入 `/var` 目录。这是一个上级目录。
* 从这里返回你的主目录。


这些内容足以让你熟悉 `cd` 命令。以下是你应该了解的其他一些重要命令。



> 
> **[每个 Ubuntu 用户都应该知道的 31 条 Linux 命令](https://itsfoss.com/essential-ubuntu-commands/)**
> 
> 
> 


如果你有疑问或建议，请告诉我。


*（题图：MJ/6fbaa345-651a-4cb9-a752-130eda922790）*




---


via: <https://itsfoss.com/cd-command/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

The cd command in Linux is used for changing directories. cd is actually short for change directories.

It's one of the [essential Linux commands](https://itsfoss.com/essential-ubuntu-commands/) that you must know.

Using the cd command is quite simple:

`cd path_to_directory`


It cannot go any simple than this, can it?

However, it's the path part that you should understand to easily travel through the [filesystem](https://linuxhandbook.com/linux-directory-structure/) without getting confused.

Here's a quick recall of absolute and relative paths.

![Absolute vs relative path in Linux](https://linuxhandbook.com/content/images/2021/04/absolute-vs-relative-path-linux.png)

And if you need more details, I recommend reading this article.

[Absolute vs Relative Path in Linux: What’s the Difference?In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What’s the difference between them and which one should you use.](https://linuxhandbook.com/absolute-vs-relative-path/)![](https://linuxhandbook.com/content/images/2021/04/absolute-relative-path-linux.png)


![](https://linuxhandbook.com/content/images/2021/04/absolute-relative-path-linux.png)

Let's see some examples of using the cd command.

## Use absolute path to change the directory

It will be easier to understand visually. Look at the image below.

![Absolute path travel to the python directory](https://itsfoss.com/content/images/2023/06/absolute-path-cd-1.png)

My current location is my home directory (`/home/abhishek`

) and I have to go to the `python`

directory inside the `scripts`

directory.

Let's say I want to use the absolute path. The absolute path to the pyth`/home/abhishek/scripts/python`

.

`cd /home/abhishek/scripts/python`


![cd command with absolute path](https://itsfoss.com/content/images/2023/06/cd-absolute-path.png)

## Use relative path to change directories

Let's take the same example but this time, I'll take the relative path.

![Relative path example](https://itsfoss.com/content/images/2023/06/absolute-path-cd-2.png)

The relative path to the `python`

directory from my home directory is `scripts/python`

. Let's use this:

`cd scripts/python`


![cd command with relative path](https://itsfoss.com/content/images/2023/06/cd-relative-path.png)

## Go up the directory

So far, you are going down the 'flow'. What if you have to go up a directory?

Let's say, you are in `/home/abhishek/scripts/python`

and you have to up a directory to `scripts`

?.

![](https://itsfoss.com/content/images/2023/06/relative-path-cd.png)

Using the absolute path is always an option but it is quite lengthy. Instead, you can use the special directory notation `..`

. The double dots (..) mean parent directory or up a directory. Single dot (.) means the current directory.

`cd ..`


Here's an example:

![cd up a directory](https://itsfoss.com/content/images/2023/06/cd-up-directory.png)

You can use the `..`

to travel up the path in the Linux filesystem hierarchy.

Suppose I am in the `python`

directory in the above image and want to go to the `code`

directory. Here's what I could do:

`cd ../../code`


![Go up the directory using cd command](https://itsfoss.com/content/images/2023/06/go-up-directory-cd-command.png)

## Go to the home directory

If you feel lost in all these directory travels and want to go back home, there are so many simple shortcuts.

In fact, the simplest of them is to use the cd command without any option.

`cd`


That will take you back to your home directory from anywhere on the filesystem.

Alternatively, you can use the `~`

notation which means home directory.

`cd ~`


![Use cd to go back home](https://itsfoss.com/content/images/2023/06/cd-go-back-home.png)

## Go to the root directory

Though you won't use it as often as the previous one, it is still good to know.

If you want to go back to the root directory from where the filesystem begins, use this:

`cd /`


There is no 'magic' involved here. `/`

denotes root when used at the beginning of a path. Don't confuse it with path separators.

![Paths in Linux](https://linuxhandbook.com/content/images/2021/04/path-linux.png)

## Switch back to the previous directory

This is a lifesaver or should I say timesaver. When you are deep inside a directory structure and then go to another directory and then you feel the need to go back to the previous location, this shortcut helps.

`cd -`


Not clear yet? Let me show an example.

I am in the location `/etc/apt/sources.list.d`

. From here, I go to `/home/abhishek/scripts/python`

to work on my code. And then I realized that I have to check something again in `/etc/apt/sources.list.d`

directory.

The usual approach would be to do this which makes me type all the path again:

![Go back to previous directory](https://itsfoss.com/content/images/2023/06/cd-previous-directory.png)

But the smart approach is to use this:

![Use cd - to go back to previous directory](https://itsfoss.com/content/images/2023/06/use-cd-shortcut-to-previous-directory-1.png)

See, no need to type the lengthy path again. Works wonder!

## 🏋️ Exercise time

If you want to practice the cd command, here's a little practice exercise for you.

- Open a terminal and go to the
`/var/log`

directory.[Check the directory contents](https://itsfoss.com/list-directory-content/). What do you see? - Now, go to
`/var`

directory. This is up a directory. - From here, go back to your home directory.

And that's good enough content for you to get familiar with the cd command. Here are some other important commands you should know about.

[31 Basic Yet Essential Ubuntu CommandsAn extensive list of essential Linux commands that every Ubuntu user will find helpful in their Linux journey.](https://itsfoss.com/essential-ubuntu-commands/)![](https://itsfoss.com/content/images/wordpress/2022/11/must-know-linux-commands-for-ubuntu-user.png)


![](https://itsfoss.com/content/images/wordpress/2022/11/must-know-linux-commands-for-ubuntu-user.png)

Let me know if you have questions or suggestions.