In the Ubuntu system, we often need to switch between different folders (directories), and the cd command is specifically designed for this task. It’s similar to clicking on different folders in the Windows system, helping us quickly navigate to the directory we need to operate on.
I. Basic Usage of the cd Command¶
The cd command has a very simple format: just add the name of the target directory after cd.
Basic Syntax: cd 目标目录
1. Enter a Subdirectory in the Current Directory¶
If the target directory is a subdirectory of the current directory, simply write the directory name.
For example, assuming your current directory is your “home directory” (similar to the Windows “User” folder), which is usually represented by ~. If you want to enter the Documents folder under the home directory, you can type:
cd Documents
2. Enter Another User’s Home Directory¶
To enter the home directory of another user (e.g., the root user) in the system, you can use the format ~用户名:
cd ~root # Enter the root user's home directory (requires administrator privileges)
II. Relative Path vs. Absolute Path¶
The Ubuntu directory structure is hierarchical (similar to nested folders), so it’s necessary to distinguish between relative paths and absolute paths:
1. Relative Path: Starting from the “Current Directory”¶
A relative path is relative to the directory you are currently in and does not need to start from the root directory (/).
- ..: Represents the “parent directory”
For example, if you are in the ~/Documents directory and want to go back to the parent directory (i.e., the ~ directory), you can type:
cd ..
- Directly write the subdirectory name: If the target subdirectory exists in the current directory, just write its name.
For example, in the~/Downloadsdirectory, enter theVideosfolder inside it:
cd Videos
2. Absolute Path: Starting from the “Root Directory”¶
An absolute path starts from the root directory / and is a complete path, which is more direct but may be longer.
- Use ~ to replace the home directory: ~ represents the current user’s home directory (e.g., /home/your_username). So to enter the Pictures folder under the home directory:
cd ~/Pictures
- Directly write the full path: For example, to enter the system folder
/usr/share/doc:
cd /usr/share/doc
III. Common cd Command Tips¶
In addition to basic usage, cd has several practical “shortcuts”:
1. cd -: Quickly Return to the Previous Directory¶
If you just switched from directory A to directory B and now want to go back to A, simply type cd -:
# Suppose you first enter Documents, then enter Reports
cd Documents
cd Reports
# Now you want to return to Documents, type
cd - # Directly return to the previous directory (i.e., Documents)
2. cd ~: Directly Return to the Home Directory¶
No matter which directory you are currently in, typing cd ~ will immediately take you back to the home directory:
cd ~ # Returns to ~/ regardless of your current location
3. cd ..: Return to the Parent Directory¶
As mentioned earlier, cd .. is the most common way to return to the parent directory (e.g., from /home/user/Documents to /home/user).
IV. Common Problems and Solutions¶
-
“No such file or directory” Error
If you entercd 目标目录and there is no response, it may be due to a misspelled directory name or the directory does not exist.
- Check if the directory name is correct (Ubuntu is case-sensitive! For example,Documentsanddocumentsare different).
- Confirm that the target directory actually exists: You can first use thelscommand to list the files/folders in the current directory and check if the target directory is present. -
Path Contains Spaces or Special Characters
If the directory name contains spaces (e.g.,my docs), typing it directly will cause an error. In this case, wrap the directory name in quotes:
cd "my docs" # Use double quotes to enclose directory names with spaces
Alternatively, you can escape the space using a backslash \:
cd my\ docs
- Permission Denied
If you try to enter a system-level directory (e.g.,/root) or another user’s directory, you may need administrator privileges. In this case, you can try usingsudo(butcdgenerally does not require it unless the target directory requires root permissions):
sudo cd /root # Try to enter the root directory (requires password input)
Note: In most cases, ordinary users do not need to enter system-level directories. It is recommended to prioritize operations in the user directory (under the home directory).
V. Summary¶
The cd command is one of the most basic and commonly used commands in Ubuntu. Mastering it requires understanding the difference between relative paths and absolute paths, as well as several practical tricks (such as cd -, cd ~, cd ..). With more practice switching between different directories, you will quickly become proficient in using cd!
Finally, if you want to confirm the current directory, you can use the pwd command (Print Working Directory), which will display the current path.