Skip to content

Commit e5e4f0d

Browse files
authored
docs(cd): enhance documentation with additional examples and troubleshooting tips (#395)
1 parent 84a7534 commit e5e4f0d

File tree

1 file changed

+93
-19
lines changed

1 file changed

+93
-19
lines changed
Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,130 @@
11
# The `cd` command
22

3-
The `cd` command is used to change the current working directory *(i.e., in which the current user is working)*. The "cd" stands for "**c**hange **d**irectory" and it is one of the most frequently used commands in the Linux terminal.
3+
The `cd` command is used to change the current working directory *(i.e., the directory in which the current user is working)*. The "cd" stands for "**c**hange **d**irectory" and it is one of the most frequently used commands in the Linux terminal.
44

5-
The `cd` command is often combined with the `ls` command (see chapter 1) when navigating through a system, however, you can also press the `TAB` key two times to list the contents of the new directory you just changed to.
5+
The `cd` command is often combined with the `ls` command (see chapter 1) when navigating through a system. You can also press the `TAB` key to auto-complete directory names, or press `TAB` twice to list available directories in the current location.
66

7-
### Examples of uses:
7+
### Syntax:
8+
9+
```
10+
cd [OPTIONS] [directory]
11+
```
12+
13+
### Basic Examples:
814

9-
1. Change the current working directory:
15+
1. **Change to a specific directory:**
1016
```
11-
cd <specified_directory_path>
17+
cd /path/to/directory
1218
```
1319

14-
2. Change the current working directory to the home directory:
20+
2. **Change to your home directory:**
1521
```
1622
cd ~
1723
```
18-
OR
24+
OR simply:
1925
```
2026
cd
2127
```
2228

23-
3. Change to the previous directory:
29+
3. **Change to the previous directory:**
2430
```
2531
cd -
2632
```
27-
This will also echo the absolute path of the previous directory.
33+
This will also print the absolute path of the previous directory.
2834

29-
4. Change the current working directory to the system's root directory:
35+
4. **Change to the system's root directory:**
3036
```
3137
cd /
3238
```
3339

34-
### &#x1F4A1; Quick Tips
35-
36-
Adding a `..` as a directory will allow you to move "up" from a folder:
40+
5. **Move up one directory level (parent directory):**
3741
```
3842
cd ..
3943
```
40-
This can also be done multiple times! For example, to move up three folders:
44+
45+
6. **Move up multiple directory levels:**
4146
```
42-
cd ../../../
47+
cd ../../..
4348
```
49+
This example moves up three levels.
4450

45-
### Syntax:
51+
### Practical Examples:
52+
53+
**Using relative paths:**
54+
```
55+
cd Documents/Projects/MyApp
56+
```
57+
58+
**Using absolute paths:**
59+
```
60+
cd /usr/local/bin
61+
```
62+
63+
**Combining with home directory shortcut:**
64+
```
65+
cd ~/Downloads
66+
```
67+
68+
**Navigate to a directory with spaces in the name:**
69+
```
70+
cd "My Documents"
71+
```
72+
OR
73+
```
74+
cd My\ Documents
75+
```
4676

77+
**Switch between two directories:**
4778
```
48-
cd [OPTIONS] directory
79+
cd /var/log
80+
cd /etc
81+
cd - # Returns to /var/log
82+
cd - # Returns to /etc
4983
```
5084

5185
### Additional Flags and Their Functionalities
5286

5387
|**Short flag** |**Long flag** |**Description** |
5488
|:---|:---|:---|
55-
|`-L`|<center>-</center>|Follow symbolic links. By default,`cd` behaves as if the `-L` option is specified.|
56-
|`-P`|<center>-</center>|Don’t follow symbolic links.|
89+
|`-L`|<center>-</center>|Follow symbolic links (default behavior). The `cd` command will follow symlinks and update the working directory to the target location.|
90+
|`-P`|<center>-</center>|Use the physical directory structure without following symbolic links. Shows the actual path instead of the symlink path.|
91+
92+
**Example of `-L` vs `-P` with symbolic links:**
93+
```
94+
# Assume /var/www is a symlink to /home/user/web
95+
cd -L /var/www # Working directory shows as /var/www
96+
pwd # Outputs: /var/www
97+
98+
cd -P /var/www # Working directory resolves to actual path
99+
pwd # Outputs: /home/user/web
100+
```
101+
102+
### Common Errors and Troubleshooting
103+
104+
**Permission denied:**
105+
```
106+
cd /root
107+
# bash: cd: /root: Permission denied
108+
```
109+
Solution: You need appropriate permissions to access the directory. Try using `sudo` if necessary.
110+
111+
**No such file or directory:**
112+
```
113+
cd /invalid/path
114+
# bash: cd: /invalid/path: No such file or directory
115+
```
116+
Solution: Verify the path exists using `ls` or check for typos. Remember that paths are case-sensitive.
117+
118+
**Not a directory:**
119+
```
120+
cd /etc/passwd
121+
# bash: cd: /etc/passwd: Not a directory
122+
```
123+
Solution: Ensure you're navigating to a directory, not a file.
124+
125+
### Important Notes:
126+
127+
- **Case sensitivity:** Linux file systems are case-sensitive. `cd Documents` is different from `cd documents`.
128+
- **Special characters:** Directory names with spaces or special characters need to be quoted or escaped.
129+
- **The `cd` command is a shell built-in:** It's not an external program, which is why it can change the shell's current directory.
130+
- **No output on success:** By default, `cd` produces no output when successful (except `cd -` which prints the new path).

0 commit comments

Comments
 (0)