mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
216 lines
8.1 KiB
Markdown
216 lines
8.1 KiB
Markdown
[#]: 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: " "
|
||
[#]: 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]
|
||
译者:[译者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/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
|