mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-28 01:01:09 +08:00
commit
e7500027ce
@ -1,186 +0,0 @@
|
||||
[#]: subject: "Using cd Command in Linux"
|
||||
[#]: via: "https://itsfoss.com/cd-command/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Using cd Command in Linux
|
||||
======
|
||||
|
||||
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][1] 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][2] without getting confused.
|
||||
|
||||
Here's a quick recall of absolute and relative paths.
|
||||
|
||||
![Absolute vs relative path in Linux][3]
|
||||
|
||||
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][4]
|
||||
|
||||
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][5]
|
||||
|
||||
### Use relative path to change directories
|
||||
|
||||
Let's take the same example but this time, I'll take the relative path.
|
||||
|
||||
![Relative path example][6]
|
||||
|
||||
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][7]
|
||||
|
||||
### 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`?.
|
||||
|
||||
![][8]
|
||||
|
||||
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][9]
|
||||
|
||||
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][10]
|
||||
|
||||
### 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][11]
|
||||
|
||||
### 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][12]
|
||||
|
||||
### 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][13]
|
||||
|
||||
But the smart approach is to use this:
|
||||
|
||||
![Use cd - to go back to previous directory][14]
|
||||
|
||||
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][15]. 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.
|
||||
|
||||
Let me know if you have questions or suggestions.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/cd-command/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/essential-ubuntu-commands/
|
||||
[2]: https://linuxhandbook.com:443/linux-directory-structure/
|
||||
[3]: https://linuxhandbook.com/content/images/2021/04/absolute-vs-relative-path-linux.png
|
||||
[4]: https://itsfoss.com/content/images/2023/06/absolute-path-cd-1.png
|
||||
[5]: https://itsfoss.com/content/images/2023/06/cd-absolute-path.png
|
||||
[6]: https://itsfoss.com/content/images/2023/06/absolute-path-cd-2.png
|
||||
[7]: https://itsfoss.com/content/images/2023/06/cd-relative-path.png
|
||||
[8]: https://itsfoss.com/content/images/2023/06/relative-path-cd.png
|
||||
[9]: https://itsfoss.com/content/images/2023/06/cd-up-directory.png
|
||||
[10]: https://itsfoss.com/content/images/2023/06/go-up-directory-cd-command.png
|
||||
[11]: https://itsfoss.com/content/images/2023/06/cd-go-back-home.png
|
||||
[12]: https://linuxhandbook.com/content/images/2021/04/path-linux.png
|
||||
[13]: https://itsfoss.com/content/images/2023/06/cd-previous-directory.png
|
||||
[14]: https://itsfoss.com/content/images/2023/06/use-cd-shortcut-to-previous-directory-1.png
|
||||
[15]: https://itsfoss.com/list-directory-content/
|
189
translated/tech/20230620.0 ⭐️ Using cd Command in Linux.md
Normal file
189
translated/tech/20230620.0 ⭐️ Using cd Command in Linux.md
Normal file
@ -0,0 +1,189 @@
|
||||
[#]: subject: "Using cd Command in Linux"
|
||||
[#]: via: "https://itsfoss.com/cd-command/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
在 Linux 中使用 cd 命令
|
||||
======
|
||||
|
||||
Linux 中的 cd 命令用于更改目录。cd 实际上是更改目录的缩写。
|
||||
|
||||
这是你必须了解的[基本 Linux 命令][1]之一。
|
||||
|
||||
使用 cd 命令非常简单:
|
||||
|
||||
```
|
||||
cd path_to_directory
|
||||
```
|
||||
|
||||
事情不可能比这更简单,不是吗?
|
||||
|
||||
然而,你应该理解它的路径部分,以便轻松地浏览[文件系统][2]而不会感到困惑。
|
||||
|
||||
这是绝对路径和相对路径的快速回顾。
|
||||
|
||||
![Absolute vs relative path in Linux][3]
|
||||
|
||||
让我们看一些使用 cd 命令的示例。
|
||||
|
||||
### 使用绝对路径更改目录
|
||||
|
||||
从视觉上看会更容易理解。看下图。
|
||||
|
||||
![Absolute path travel to the python directory][4]
|
||||
|
||||
我当前的位置是我的主目录(`/home/abhishek`),我必须进入 `scripts` 目录中的 `python` 目录。
|
||||
|
||||
假设我想使用绝对路径。python 目录的绝对路径是 `/home/abhishek/scripts/python`。
|
||||
|
||||
```
|
||||
cd /home/abhishek/scripts/python
|
||||
```
|
||||
|
||||
![cd command with absolute path][5]
|
||||
|
||||
### 使用相对路径更改目录
|
||||
|
||||
让我们举同样的例子,但这次我将采用相对路径。
|
||||
|
||||
![Relative path example][6]
|
||||
|
||||
我的主目录到 `python` 目录的相对路径是 `scripts/python`。让我们用这个:
|
||||
|
||||
```
|
||||
cd scripts/python
|
||||
```
|
||||
|
||||
![cd command with relative path][7]
|
||||
|
||||
### 进入上级目录
|
||||
|
||||
到目前为止,你一直在进入下级。如果你必须进入上级目录怎么办?
|
||||
|
||||
假设你位于 `/home/abhishek/scripts/python` 中,并且必须将目录添加到 `scripts`?
|
||||
|
||||
![][8]
|
||||
|
||||
使用绝对路径始终是一种选择,但它相当冗长。相反,你可以使用特殊的目录符号 `..`。双点 (..) 表示父目录或上一级目录。单点(.)表示当前目录。
|
||||
|
||||
```
|
||||
cd ..
|
||||
```
|
||||
|
||||
这是一个例子:
|
||||
|
||||
![cd up a directory][9]
|
||||
|
||||
你可以使用 `..` 在 Linux 文件系统层次结构中向上移动路径。
|
||||
|
||||
假设我在上图中的 `python` 目录中,想要进入 `code` 目录。这是我能做的:
|
||||
|
||||
```
|
||||
cd ../../code
|
||||
```
|
||||
|
||||
![Go up the directory using cd command][10]
|
||||
|
||||
### 进入主目录
|
||||
|
||||
如果你在所有这些目录切换中感到迷失并想回到主目录,有很多简单的快捷方式。
|
||||
|
||||
事实上,最简单的就是使用不带任何选项的 cd 命令。
|
||||
|
||||
```
|
||||
cd
|
||||
```
|
||||
|
||||
这将使你从文件系统上的任何位置返回主目录。
|
||||
|
||||
或者,你可以使用 `~` 符号,表示主目录。
|
||||
|
||||
```
|
||||
cd ~
|
||||
```
|
||||
|
||||
![Use cd to go back home][11]
|
||||
|
||||
### 进入根目录
|
||||
|
||||
尽管你不会像前一个那样经常使用它,但了解一下仍然有好处。
|
||||
|
||||
如果你想返回文件系统开始的根目录,请使用以下命令:
|
||||
|
||||
```
|
||||
cd /
|
||||
```
|
||||
|
||||
这里不涉及“魔法”。当在路径开头使用时,`/` 表示根。不要将它与路径分隔符混淆。
|
||||
|
||||
![Paths in Linux][12]
|
||||
|
||||
### 切换回上一级目录
|
||||
|
||||
这是一个救命稻草,或者我应该说是省时稻草。当你深入目录结构,然后转到另一个目录,然后你觉得需要返回到以前的位置时,此快捷方式会有所帮助。
|
||||
|
||||
```
|
||||
cd -
|
||||
```
|
||||
|
||||
还不清楚吗? 让我举个例子。
|
||||
|
||||
我位于 `/etc/apt/sources.list.d`。从这里,进入 `/home/abhishek/scripts/python` 来处理我的代码。然后我意识到我必须再次检查 `/etc/apt/sources.list.d` 目录中的某些内容。
|
||||
|
||||
通常的方法是这样做,这让我再次输入所有路径:
|
||||
|
||||
![Go back to previous directory][13]
|
||||
|
||||
但聪明的方法是使用这个:
|
||||
|
||||
![Use cd - to go back to previous directory][14]
|
||||
|
||||
看吧,无需再次输入冗长的路径。如期工作!
|
||||
|
||||
### 🏋️ 练习时间
|
||||
|
||||
如果你想练习 cd 命令,这里有一些练习供你使用。
|
||||
|
||||
- 打开终端并进入 `/var/log` 目录。[检查目录内容][15]。你看到了什么?
|
||||
- 现在,进入 `/var` 目录。这是一个上级目录。
|
||||
- 从这里返回你的主目录。
|
||||
|
||||
这些内容足以让你熟悉 cd 命令。以下是你应该了解的其他一些重要命令。
|
||||
|
||||
[每个Ubuntu用户都应该知道的31条Linux命令][16]
|
||||
|
||||
如果你有疑问或建议,请告诉我。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/cd-command/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/essential-ubuntu-commands/
|
||||
[2]: https://linuxhandbook.com:443/linux-directory-structure/
|
||||
[3]: https://linuxhandbook.com/content/images/2021/04/absolute-vs-relative-path-linux.png
|
||||
[4]: https://itsfoss.com/content/images/2023/06/absolute-path-cd-1.png
|
||||
[5]: https://itsfoss.com/content/images/2023/06/cd-absolute-path.png
|
||||
[6]: https://itsfoss.com/content/images/2023/06/absolute-path-cd-2.png
|
||||
[7]: https://itsfoss.com/content/images/2023/06/cd-relative-path.png
|
||||
[8]: https://itsfoss.com/content/images/2023/06/relative-path-cd.png
|
||||
[9]: https://itsfoss.com/content/images/2023/06/cd-up-directory.png
|
||||
[10]: https://itsfoss.com/content/images/2023/06/go-up-directory-cd-command.png
|
||||
[11]: https://itsfoss.com/content/images/2023/06/cd-go-back-home.png
|
||||
[12]: https://linuxhandbook.com/content/images/2021/04/path-linux.png
|
||||
[13]: https://itsfoss.com/content/images/2023/06/cd-previous-directory.png
|
||||
[14]: https://itsfoss.com/content/images/2023/06/use-cd-shortcut-to-previous-directory-1.png
|
||||
[15]: https://itsfoss.com/list-directory-content/
|
||||
[16]: https://itsfoss.com/essential-ubuntu-commands/
|
Loading…
Reference in New Issue
Block a user