|
1 | 1 | # The `cd` command |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 6 |
|
7 | | -### Examples of uses: |
| 7 | +### Syntax: |
| 8 | + |
| 9 | +``` |
| 10 | +cd [OPTIONS] [directory] |
| 11 | +``` |
| 12 | + |
| 13 | +### Basic Examples: |
8 | 14 |
|
9 | | -1. Change the current working directory: |
| 15 | +1. **Change to a specific directory:** |
10 | 16 | ``` |
11 | | -cd <specified_directory_path> |
| 17 | +cd /path/to/directory |
12 | 18 | ``` |
13 | 19 |
|
14 | | -2. Change the current working directory to the home directory: |
| 20 | +2. **Change to your home directory:** |
15 | 21 | ``` |
16 | 22 | cd ~ |
17 | 23 | ``` |
18 | | -OR |
| 24 | +OR simply: |
19 | 25 | ``` |
20 | 26 | cd |
21 | 27 | ``` |
22 | 28 |
|
23 | | -3. Change to the previous directory: |
| 29 | +3. **Change to the previous directory:** |
24 | 30 | ``` |
25 | 31 | cd - |
26 | 32 | ``` |
27 | | -This will also echo the absolute path of the previous directory. |
| 33 | +This will also print the absolute path of the previous directory. |
28 | 34 |
|
29 | | -4. Change the current working directory to the system's root directory: |
| 35 | +4. **Change to the system's root directory:** |
30 | 36 | ``` |
31 | 37 | cd / |
32 | 38 | ``` |
33 | 39 |
|
34 | | -### 💡 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):** |
37 | 41 | ``` |
38 | 42 | cd .. |
39 | 43 | ``` |
40 | | -This can also be done multiple times! For example, to move up three folders: |
| 44 | + |
| 45 | +6. **Move up multiple directory levels:** |
41 | 46 | ``` |
42 | | -cd ../../../ |
| 47 | +cd ../../.. |
43 | 48 | ``` |
| 49 | +This example moves up three levels. |
44 | 50 |
|
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 | +``` |
46 | 76 |
|
| 77 | +**Switch between two directories:** |
47 | 78 | ``` |
48 | | -cd [OPTIONS] directory |
| 79 | +cd /var/log |
| 80 | +cd /etc |
| 81 | +cd - # Returns to /var/log |
| 82 | +cd - # Returns to /etc |
49 | 83 | ``` |
50 | 84 |
|
51 | 85 | ### Additional Flags and Their Functionalities |
52 | 86 |
|
53 | 87 | |**Short flag** |**Long flag** |**Description** | |
54 | 88 | |:---|:---|:---| |
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