mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
parent
5797e76c67
commit
c94a7fba19
@ -0,0 +1,218 @@
|
||||
[#]: subject: "Terminal Basics Series #1: Changing Directories in Linux Terminal"
|
||||
[#]: via: "https://itsfoss.com/change-directories/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "ChatGPT"
|
||||
[#]: reviewer: "wxy"
|
||||
[#]: publisher: "wxy"
|
||||
[#]: url: "https://linux.cn/article-16304-1.html"
|
||||
|
||||
终端基础:Linux 终端中的目录切换
|
||||
======
|
||||
|
||||
![][0]
|
||||
|
||||
> 本篇文章作为终端基础教程系列的一部分,介绍如何在 Linux 命令行中,利用绝对路径和相对路径实现目录切换。
|
||||
|
||||
Linux 的 `cd` 命令让你可以轻松切换文件夹(即目录)。只需提供你要切换到的文件夹路径即可。
|
||||
|
||||
```
|
||||
cd path_to_directory
|
||||
```
|
||||
|
||||
然而对于 Linux 新人来说,可能会在路径的指定上有所困扰。
|
||||
|
||||
首先,让我们解决这个问题。
|
||||
|
||||
### 理解 Linux 中的路径
|
||||
|
||||
在 Linux 文件系统中,路径是用来追踪文件位置的信息。所有的路径都从根目录开始,然后向下延伸。
|
||||
|
||||
你可以通过下面的方式查看当前所在的位置:
|
||||
|
||||
```
|
||||
pwd
|
||||
```
|
||||
|
||||
结果可能是类似于 `/home/username` 的输出。注意,这里的 `username` 将会是你自己的用户名。
|
||||
|
||||
你可以注意到,路径是由 `/` 符号和目录名组成的。比如路径 `/home/abhishek/scripts`, 表示 `scripts` 是在文件夹 `abhishek` 之内,而文件夹 `abhishek` 在 `home` 文件夹之内。要注意,第一个 '/' 是指根目录(即文件系统的开始处),后面的 '/' 则作为目录的分隔符。
|
||||
|
||||
![Path in Linux][1]
|
||||
|
||||
> 🖥️ 在终端中键入 `ls /`,然后按回车。你将会看到根目录下的所有内容,试试看!
|
||||
|
||||
接下来,让我们学习两种常见的路径指定方式:绝对路径和相对路径。
|
||||
|
||||
**绝对路径**:这种路径从根开始,然后一直扩展到你需要的位置。如果一个路径是以 `/` 开头,那就说明它是一个绝对路径。
|
||||
|
||||
**相对路径**:这是相对于你文件系统中当前位置的路径。如果我当前位置在 `/home/abhishek`,并且我需要去 `/home/abhishek/Documents`, 我只需要简单地切换到 `Documents`,而不需要指定整个绝对路径 `/home/abhishek/Documents`。
|
||||
|
||||
在我演示这两种路径的区别之前,有必要先熟悉两个特殊的目录标识:
|
||||
|
||||
- `.` (单点)表示当前目录。
|
||||
- `..` (双点)表示上一级目录,也就是当前目录的母目录。
|
||||
|
||||
这里有一张图形化的表示。
|
||||
|
||||
![Absolute path vs relative path][2]
|
||||
|
||||
### 利用 cd 命令变更目录
|
||||
|
||||
在你已对路径概念有所了解之后,我们来了解如何切换目录。
|
||||
|
||||
> 🖥️ 如果你**仅键入 `cd` 并按回车键**,无论当前位置在哪,系统都会将你带回主目录。试一试吧。
|
||||
|
||||
敲入以下命令,你就能看到主目录里的所有文件夹:
|
||||
|
||||
```
|
||||
ls
|
||||
```
|
||||
|
||||
这是我看到的情况:
|
||||
|
||||
```
|
||||
abhishek@ituxedo:~$ ls
|
||||
Desktop Downloads Pictures Templates VirtualBoxVMs
|
||||
Documents Music Public Videos
|
||||
```
|
||||
|
||||
你的情况可能与此类似,但未必完全一样。
|
||||
|
||||
假如你希望跳转到 `Documents` 文件夹。由于它就在当前目录下,这里使用相对路径会比较方便:
|
||||
|
||||
```
|
||||
cd Documents
|
||||
```
|
||||
|
||||
> 💡 注意,大部分 Linux 发行版预设的终端模拟器会在提示符本身显示出当前所在的位置。因此你不必频繁使用 `pwd` 指令来确认自己的位置。
|
||||
|
||||
![Most Linux terminal prompts show the current location][3]
|
||||
|
||||
假如你希望切换到位于主目录里的 `Templates` 文件夹。
|
||||
|
||||
你可以使用相对路径 `../Templates`(`..` 会让你返回到上层目录,即 `/home/username`,然后你就可以进入 `Templates` 文件夹了)。
|
||||
|
||||
但这次我们尝试使用绝对路径。请把下面的 `abhishek` 替换成你的用户名。
|
||||
|
||||
```
|
||||
cd /home/abhishek/Templates
|
||||
```
|
||||
|
||||
此刻你已经在 `Templates` 文件夹里了。如何前往 `Downloads` 文件夹呢?这次我们再使用相对路径:
|
||||
|
||||
```
|
||||
cd ../Downloads
|
||||
```
|
||||
|
||||
下面的图片会回顾一下你刚才学到的所有或有关目录切换的范例。
|
||||
|
||||
![cd command example][4]
|
||||
|
||||
> 💡 别忘了你还可以使用终端的 `tab` 键自动补全功能。只需要键入命令或者文件夹名称的前几个字母,然后敲击 `tab` 键,系统就会尝试自动地补全命令或文件夹名称,或者给你显示出所有可能的选项。
|
||||
|
||||
### 故障解决
|
||||
|
||||
在 Linux 终端操作切换目录的过程中,你可能会遇到一些常见的错误。
|
||||
|
||||
#### 文件或目录不存在
|
||||
|
||||
如果在你尝试切换目录时,出现类似下面的错误信息:
|
||||
|
||||
> bash: cd: directory_name: No such file or directory
|
||||
|
||||
那么你可能在路径或目录名称上犯了误解。这里有几点你需要注意的:
|
||||
|
||||
- 请确定你输入的目录名中没有拼写错误。
|
||||
- Linux 系统对大小写敏感,因此,`Downloads` 和 `downloads` 会被识别为不同的目录。
|
||||
- 你可能未正确指定路径。可能你所在的位置与你预期的不同?或者你遗漏了绝对路径中的开头的 `/` 字符?
|
||||
|
||||
![Common examples of "no such file or directory" error][5]
|
||||
|
||||
#### 非目录错误
|
||||
|
||||
如果你看到像下面这样的错误提示:
|
||||
|
||||
> bash: cd: filename: Not a directory
|
||||
|
||||
这表示你尝试使用 `cd` 命令对一个文件进行操作,而不是一个目录(文件夹)。很明显,你不能像进入文件夹那样“进入”一个文件,因此会出现这样的错误。
|
||||
|
||||
![Not a directory error with the cd command][6]
|
||||
|
||||
#### 参数过多
|
||||
|
||||
这是 Linux 新手常犯的另一个错误:
|
||||
|
||||
> bash: cd: too many arguments
|
||||
|
||||
`cd` 命令只接受一个参数。也就是说,你只能对命令指定一个目录。
|
||||
|
||||
如果你指定了超过一个的参数,或者在路径中误加了空格,你就会看到这个错误。
|
||||
|
||||
![Too many arguments error in Linux terminal][7]
|
||||
|
||||
> 🏋🏻 如果你输入 `cd -`,它将会把你带到前一个目录。当你在两个相隔较远的地方切换时非常方便,可以避免再次输入长路径。
|
||||
|
||||
### 特殊目录符号
|
||||
|
||||
在结束这个教程之前,我想快速告诉你关于特殊符号 `~`。在 Linux 中,`~` 是用户主目录的捷径。
|
||||
|
||||
如果用户 `abhi` 运行它,`~` 就会代表 `/home/abhi`,如果用户 `prakash` 运行,`~` 就意味着 `/home/prakash`。
|
||||
|
||||
总结一下你在这个基础教程系列中学到的所有特殊目录标识:
|
||||
|
||||
| 符号 | 描述 |
|
||||
| :- | :- |
|
||||
| `.` | 当前目录 |
|
||||
| `..` | 上级目录 |
|
||||
| `~` | 主目录 |
|
||||
| `-` | 前一个目录 |
|
||||
|
||||
### 测试你的知识
|
||||
|
||||
下面是一些简单的练习,用来测试你刚刚学到的关于路径和 `cd` 命令的知识。
|
||||
|
||||
移动到你的主目录,并使用这个命令创建一个嵌套的目录结构:
|
||||
|
||||
```
|
||||
mkdir -p sample/dir1/dir2/dir3
|
||||
```
|
||||
|
||||
然后,一步步来试试这个:
|
||||
|
||||
- 使用绝对路径或相对路径进入 `dir3`
|
||||
- 使用相对路径移动到 `dir1`
|
||||
- 使用你能想象到的最短路径进入 `dir2`
|
||||
- 使用绝对路径切换到 `sample` 目录
|
||||
- 返回你的主目录
|
||||
|
||||
> 🔑 想知道你是否全都做对了吗?欢迎分享你的答案。
|
||||
|
||||
现在你知道如何切换目录,是不是应该学习一下如何创建它们呢?
|
||||
|
||||
我强烈推荐你阅读这篇文章,了解一些关于终端和命令的小技巧。
|
||||
|
||||
如果你想了解 Linux 命令行的基础知识,记得关注我们的 Linux 终端基础系列教程的更多章节。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/change-directories/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[ChatGPT](https://linux.cn/lctt/ChatGPT)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [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/content/images/2023/02/path-linux.webp
|
||||
[2]: https://itsfoss.com/content/images/2023/02/absolute-and-relative-path.png
|
||||
[3]: https://itsfoss.com/content/images/2023/02/linux-terminal-prompt.png
|
||||
[4]: https://itsfoss.com/content/images/2023/02/cd-command-example.svg
|
||||
[5]: https://itsfoss.com/content/images/2023/02/common-errors-with-cd.png
|
||||
[6]: https://itsfoss.com/content/images/2023/02/not-a-directory-error-linux.png
|
||||
[7]: https://itsfoss.com/content/images/2023/02/too-many-arguments.png
|
||||
[8]: https://itsfoss.community/t/exercise-in-changing-directories-in-linux-terminal/10177?ref=its-foss
|
||||
[0]: https://img.linux.net.cn/data/attachment/album/202310/21/062234mz9zymqc6om5924m.jpg
|
@ -1,215 +0,0 @@
|
||||
[#]: subject: "Terminal Basics Series #1: Changing Directories in Linux Terminal"
|
||||
[#]: via: "https://itsfoss.com/change-directories/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "Cubik65536"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Terminal Basics Series #1: Changing Directories in Linux Terminal
|
||||
======
|
||||
|
||||
The cd command in Linux allows you to change directories (folders). You just have to give the path to the directory.
|
||||
|
||||
```
|
||||
cd path_to_directory
|
||||
```
|
||||
|
||||
And here comes the first challenge if you are new to Linux. You are probably not sure about the path.
|
||||
|
||||
Let's tackle that first.
|
||||
|
||||
### Understanding paths in Linux
|
||||
|
||||
The path traces the location in the Linux directory structure. Everything starts at the root and then goes from there.
|
||||
|
||||
You can check your current location with the following:
|
||||
|
||||
```
|
||||
pwd
|
||||
```
|
||||
|
||||
It should show an output like /home/username. Of course, it will be your username.
|
||||
|
||||
As you can see, paths are composed of / and directory names. Path `/home/abhishek/scripts` means the folder scripts is inside the folder `abhishek`, which is inside the folder `home`. The first `/` is for root (from where the filesystem starts), the trailing / are separators for the directories.
|
||||
|
||||
![Path in Linux][1]
|
||||
|
||||
> 🖥️ Type `ls /` in the terminal and press enter. It will show you the content of the root directory. Try it.
|
||||
|
||||
Now, there are two ways to specify a path: absolute and relative.
|
||||
|
||||
**Absolute path**: It starts with the root and then traces the location from there. If a path starts with /, it is an absolute path.
|
||||
|
||||
**Relative path**: This path originates from your current location in the filesystem. If I am in the location /home/abhishek and I have to go to /home/abhishek/Documents, I can simply go to Documents instead of specifying the absolute path /home/abhishek/Documents.
|
||||
|
||||
Before I show you the difference between the two, you should get familiar with two special directory notations:
|
||||
|
||||
- . (single dot) denotes the current directory.
|
||||
- .. (two dots) denote the parent directory taking you one directory above the current one.
|
||||
|
||||
Here's a pictorial representation.
|
||||
|
||||
![Absolute path vs relative path][2]
|
||||
|
||||
### Changing directory with cd command
|
||||
|
||||
Now that you are familiar with the concept of path, let's see how you can change the directory.
|
||||
|
||||
> 🖥️ If you **just type cd and press enter**, it will take you to your home directory from any location. Go on, try it.
|
||||
|
||||
Enter the following command to see the directories inside your home directories:
|
||||
|
||||
```
|
||||
ls
|
||||
```
|
||||
|
||||
This is what it shows to me:
|
||||
|
||||
```
|
||||
[email protected]:~$ ls
|
||||
Desktop Downloads Pictures Templates VirtualBoxVMs
|
||||
Documents Music Public Videos
|
||||
```
|
||||
|
||||
Yours may be similar but not exactly the same.
|
||||
|
||||
Let's say you want to go to the Documents directory. Since it is available under the current directory, it will be easier to use the relative path here:
|
||||
|
||||
```
|
||||
cd Documents
|
||||
```
|
||||
|
||||
> 💡 The default terminal emulators of most Linux distributions show you the current location in the prompt itself. You don't have to use pwd all the time just to know where you are.
|
||||
|
||||
![Most Linux terminal prompts show the current location][3]
|
||||
|
||||
Now, let's say you want to switch to the Templates directory that was located in your home directory.
|
||||
|
||||
You can use the relative path `../Templates` (.. takes you to the one directory above Documents to /home/username and from there you go to Templates).
|
||||
|
||||
But let's go for the absolute path instead. Please change 'abhishek' with your username.
|
||||
|
||||
```
|
||||
cd /home/abhishek/Templates
|
||||
```
|
||||
|
||||
Now you are in the Templates directory. How about going to the Downloads directory? Use the relative path this time:
|
||||
|
||||
```
|
||||
cd ../Templates
|
||||
```
|
||||
|
||||
Here's a replay of all the above directory change examples you just read.
|
||||
|
||||
![cd command example][4]
|
||||
|
||||
> 💡 Utilize the tab completion in the terminal. Start typing a few letters of the command and directory and hit the tab key. It will try to autocomplete or show you the possible options.
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
You may encounter a few common errors while changing the directories in Linux terminal.
|
||||
|
||||
#### No such file or directory
|
||||
|
||||
If you see an error like this while changing the directories:
|
||||
|
||||
> bash: cd: directory_name: No such file or directory
|
||||
|
||||
Then you made mistake with the path or name of the directories. Here are a few things to note.
|
||||
|
||||
- Make sure there is no typo in the directory name.
|
||||
- Linux is case sensitive. Downloads and downloads are not the same.
|
||||
- You are not specifying the correct path. Perhaps you are in some other location? Or did you miss the first / in the absolute path?
|
||||
|
||||
![Common examples of "no such file or directory" error][5]
|
||||
|
||||
#### Not a directory
|
||||
|
||||
If you see an error like this:
|
||||
|
||||
> bash: cd: filename: Not a directory
|
||||
|
||||
It means that you are trying to use the cd command with a file, not a directory (folder). Clearly, you cannot enter a file the same way you enter a folder and hence this error.
|
||||
|
||||
![Not a directory error with the cd command][6]
|
||||
|
||||
#### Too many arguments
|
||||
|
||||
Another common rookie Linux mistake:
|
||||
|
||||
> bash: cd: too many arguments
|
||||
|
||||
The cd commands take only one argument. That means that you can only specify one directory to the command.
|
||||
|
||||
If you specify more than one or mistyped a path by adding a space to the path, you'll see this error.
|
||||
|
||||
![Too many arguments error in Linux terminal][7]
|
||||
|
||||
> 🏋🏻 If you press `cd -`, it will take you to your previous directory. It's quite handy when you are switching between two distant locations. You don't have to type the long paths again.
|
||||
|
||||
### Special directory notations
|
||||
|
||||
Before ending this tutorial, let me quickly tell you about the special notation `~`. In Linux, ~ is a shortcut for the user's home directory.
|
||||
|
||||
If user `abhi` is running it, ~ would mean `/home/abhi` and if user `prakash` was running it, it would mean `/home/prakash`.
|
||||
|
||||
To summarize all the special directory notations you learned in this chapter of the terminal basics series:
|
||||
|
||||
| Notation | Description |
|
||||
| :- | :- |
|
||||
| . | Current directory |
|
||||
| .. | Parent directory |
|
||||
| ~ | Home directory |
|
||||
| - | Previous directory |
|
||||
|
||||
### Test your knowledge
|
||||
|
||||
Here are a few simple exercises to test your newly learned knowledge of the path and the cd command.
|
||||
|
||||
Move to your home directory and create a nested directory structure with this command:
|
||||
|
||||
```
|
||||
mkdir -p sample/dir1/dir2/dir3
|
||||
```
|
||||
|
||||
Now, try this one by one:
|
||||
|
||||
- Go to the dir3 using either absolute or relative path
|
||||
- Move to dir1 using relative path
|
||||
- Now go to dir2 using the shortest path you can imagine
|
||||
- Change to the sample directory using absolute path
|
||||
- Go back to your home directory
|
||||
|
||||
> 🔑 Want to know if you got all of them right or not? Feel free to [share your answers in the It's FOSS Community][8].
|
||||
|
||||
Now that you know how to change directories, how about you learn about creating them?
|
||||
|
||||
I highly recommend reading this article to learn small but useful things about the terminals and the commands.
|
||||
|
||||
Stay tuned for more chapters in the Linux Terminal Basics series if you want to learn the essentials of the Linux command line.
|
||||
|
||||
And, of course, your feedback on this new series is welcome. What can I do to improve it?
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/change-directories/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[Cubik65536](https://github.com/Cubik65536)
|
||||
校对:[校对者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/content/images/2023/02/path-linux.webp
|
||||
[2]: https://itsfoss.com/content/images/2023/02/absolute-and-relative-path.png
|
||||
[3]: https://itsfoss.com/content/images/2023/02/linux-terminal-prompt.png
|
||||
[4]: https://itsfoss.com/content/images/2023/02/cd-command-example.svg
|
||||
[5]: https://itsfoss.com/content/images/2023/02/common-errors-with-cd.png
|
||||
[6]: https://itsfoss.com/content/images/2023/02/not-a-directory-error-linux.png
|
||||
[7]: https://itsfoss.com/content/images/2023/02/too-many-arguments.png
|
||||
[8]: https://itsfoss.community/t/exercise-in-changing-directories-in-linux-terminal/10177?ref=its-foss
|
Loading…
Reference in New Issue
Block a user