mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-28 23:20:10 +08:00
187 lines
5.9 KiB
Markdown
187 lines
5.9 KiB
Markdown
[#]: 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/
|