cd Command in Linux
The cd (change directory) command in Linux is used to navigate between directories in the terminal. Since Linux is based on a hierarchical file system, moving between directories is a fundamental skill.
It allows users to move from one directory to another in a hierarchical file system.
---
Syntax :
cd [directory] or [directory_path]
If no directory is specified, cd moves to the user's home directory ($HOME).
[directory_path] can be:
An absolute path (starting from /, the root directory)
A relative path (based on the current working directory)
If no directory is provided, cd defaults to the user's home directory.
---
Common Use Cases & Examples
1. Move to a Specific Directory
Example:
cd /home/user/Documents
Output (if successful, no output is shown)
To verify the current directory:
pwd
Output:
/home/user/Documents
---
2. Move to Home Directory
Example:
cd
or
cd ~
Output:
Moves to /home/username (home directory).
---
3.
Move to Parent Directory (..)
cd ..
Explanation:
Moves one level up in the directory structure.
Example:
cd ..
Output:
Moves to the parent directory of the current location.
---
4. Move to the Root Directory (/)
Explanation:
Moves directly to the root of the file system.
Example:
cd /
Output:
Moves to the root of the filesystem.
---
5. Navigate to the Previous Directory
Example:
cd -
Output:
Moves to the previous working directory.
---
6. Move to a Relative Path
Example:
cd Documents/Projects
Output:
Moves into Projects inside Documents (if it exists).
---
7. Use cd with Spaces in Directory Name
Example:
cd "My Documents"
or
cd My\ Documents
Output:
Moves to the directory My Documents.
---
8. Combining cd with Other Commands
Using && to Run Commands After Changing Directory
cd /var/log && ls
Explanation:
Changes to /var/log and immediately lists files.
9 .Using cd with Environment Variables
Example: Change to a Path Stored in a Variable
export PROJECTS=/home/user/Documents/Projects
cd $PROJECTS
Explanation:
PROJECTS is an environment variable storing a directory path.
$PROJECTS is used to navigate there.
---
10 . Common Errors and Fixes
1. Directory Not Found
cd /wrong/path
Error:
bash: cd: /wrong/path: No such file or directory
Fix:
Use ls to check if the directory exists:
ls /wrong
Ensure proper spelling and case sensitivity.
---
2. Permission Denied
cd /root
Error:
bash: cd: /root: Permission denied
Fix:
Use sudo if you have administrative privileges:
sudo cd /root
(However, sudo cd doesn’t work directly; use sudo su first or cd inside a root shell.)
11. Combining cd with Other Commands
You can use cd in combination with other commands using && or ;:
Example 1: Navigate and List Files
cd /var/log && ls -lh
Explanation:
&& ensures ls -lh runs only if cd succeeds.
12 . PNavigate and Print Current Directory
cd /etc; pwd
Explanation:
; allows running pwd regardless of cd success.
13 . Using cd in Scripts
You can use cd in Bash scripts for automation.
Example: Backup Script
#!/bin/bash
cd /home/user/Documents || { echo "Directory not found! Exiting..."; exit 1; }
tar -czf backup.tar.gz *
echo "Backup completed successfully!"
Explanation:
If cd fails, {} ensures the script exits with an error message.
14 . Using cd with Wildcards (*)
Example: Move to a Matching Directory
cd /home/user/Doc*
Explanation:
Moves into the first directory that matches Doc* (e.g., Documents).
15. Combining cd with Other Commands
You can use cd in combination with other commands using && or ;:
Example 15.1: Navigate and List Files
cd /var/log && ls -lh
Explanation:
&& ensures ls -lh runs only if cd succeeds.
Example 15.2: Navigate and Print Current Directory
cd /etc; pwd
Explanation:
; allows running pwd regardless of cd success.
----------------
-----------------
Conclusion
cd is essential for navigating the Linux file system.
Always use pwd to check the current directory.
Use absolute (/home/user/docs) or relative (../docs) paths to move efficiently.