Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 93 additions & 19 deletions ebook/en/content/002-the-cd-command.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,130 @@
# The `cd` command

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.
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.

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.
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.

### Examples of uses:
### Syntax:

```
cd [OPTIONS] [directory]
```

### Basic Examples:

1. Change the current working directory:
1. **Change to a specific directory:**
```
cd <specified_directory_path>
cd /path/to/directory
```

2. Change the current working directory to the home directory:
2. **Change to your home directory:**
```
cd ~
```
OR
OR simply:
```
cd
```

3. Change to the previous directory:
3. **Change to the previous directory:**
```
cd -
```
This will also echo the absolute path of the previous directory.
This will also print the absolute path of the previous directory.

4. Change the current working directory to the system's root directory:
4. **Change to the system's root directory:**
```
cd /
```

### &#x1F4A1; Quick Tips

Adding a `..` as a directory will allow you to move "up" from a folder:
5. **Move up one directory level (parent directory):**
```
cd ..
```
This can also be done multiple times! For example, to move up three folders:

6. **Move up multiple directory levels:**
```
cd ../../../
cd ../../..
```
This example moves up three levels.

### Syntax:
### Practical Examples:

**Using relative paths:**
```
cd Documents/Projects/MyApp
```

**Using absolute paths:**
```
cd /usr/local/bin
```

**Combining with home directory shortcut:**
```
cd ~/Downloads
```

**Navigate to a directory with spaces in the name:**
```
cd "My Documents"
```
OR
```
cd My\ Documents
```

**Switch between two directories:**
```
cd [OPTIONS] directory
cd /var/log
cd /etc
cd - # Returns to /var/log
cd - # Returns to /etc
```

### Additional Flags and Their Functionalities

|**Short flag** |**Long flag** |**Description** |
|:---|:---|:---|
|`-L`|<center>-</center>|Follow symbolic links. By default,`cd` behaves as if the `-L` option is specified.|
|`-P`|<center>-</center>|Don’t follow symbolic links.|
|`-L`|<center>-</center>|Follow symbolic links (default behavior). The `cd` command will follow symlinks and update the working directory to the target location.|
|`-P`|<center>-</center>|Use the physical directory structure without following symbolic links. Shows the actual path instead of the symlink path.|

**Example of `-L` vs `-P` with symbolic links:**
```
# Assume /var/www is a symlink to /home/user/web
cd -L /var/www # Working directory shows as /var/www
pwd # Outputs: /var/www

cd -P /var/www # Working directory resolves to actual path
pwd # Outputs: /home/user/web
```

### Common Errors and Troubleshooting

**Permission denied:**
```
cd /root
# bash: cd: /root: Permission denied
```
Solution: You need appropriate permissions to access the directory. Try using `sudo` if necessary.

**No such file or directory:**
```
cd /invalid/path
# bash: cd: /invalid/path: No such file or directory
```
Solution: Verify the path exists using `ls` or check for typos. Remember that paths are case-sensitive.

**Not a directory:**
```
cd /etc/passwd
# bash: cd: /etc/passwd: Not a directory
```
Solution: Ensure you're navigating to a directory, not a file.

### Important Notes:

- **Case sensitivity:** Linux file systems are case-sensitive. `cd Documents` is different from `cd documents`.
- **Special characters:** Directory names with spaces or special characters need to be quoted or escaped.
- **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.
- **No output on success:** By default, `cd` produces no output when successful (except `cd -` which prints the new path).
Loading