cd Command: 10 Examples with DetailsThe cd (change directory) command in Linux is used to navigate between directories in the file system. Below are 10 practical examples demonstrating various uses of the cd command, complete with explanations and scenarios.
Navigate to a directory by specifying its path.
cd /home/user/documents
Explanation: This command changes the current working directory to /home/user/documents. The absolute path ensures you move to the exact location regardless of your current directory.
Use Case: Accessing a directory where your project files are stored.
Move to the user's home directory using the tilde (~) shorthand.
cd ~
Explanation: The tilde (~) represents the current user's home directory (e.g., /home/user). Running cd ~ takes you there directly.
Use Case: Quickly returning to your home directory after working in other parts of the file system.
Navigate to the parent directory of the current directory.
cd ..
Explanation: The double dot (..) refers to the parent directory. For example, if you're in /home/user/documents, this command moves you to /home/user.
Use Case: Moving up one level in the directory hierarchy to access sibling directories.
Navigate up multiple levels in the directory structure.
cd ../../..
Explanation: Each .. moves up one directory. If you're in /home/user/documents/projects, cd ../../.. moves you to /home (three levels up).
Use Case: Accessing a higher-level directory without typing the full path.
Navigate to the root directory of the file system.
cd /
Explanation: The single forward slash (/) represents the root directory, the topmost directory in the Linux file system.
Use Case: Accessing system-wide directories like /etc or /var.
Navigate to a directory with spaces in its name.
cd "My Documents"
Explanation: Enclose directory names with spaces in quotes (or escape spaces with a backslash, e.g., My\ Documents). This command moves to the My Documents directory.
Use Case: Working with directories that have spaces in their names, common in user-created folders.
Switch back to the last directory you were in.
cd -
Explanation: The hyphen (-) takes you to the previous working directory stored in the OLDPWD environment variable. For example, if you were in /home/user and moved to /etc, cd - returns you to /home/user.
Use Case: Toggling between two directories during a task.
Navigate to a subdirectory relative to the current directory.
cd projects/code
Explanation: This command moves to the code directory inside the projects directory, assuming projects is in your current directory. Relative paths are shorter than absolute paths when you're already nearby.
Use Case: Navigating to a subdirectory within your current project folder.
Navigate to another user's home directory.
cd ~username
Explanation: Replace username with the target user's name (e.g., cd ~john moves to /home/john). This requires appropriate permissions.
Use Case: Accessing another user's files (e.g., for administrative tasks), assuming you have access.
Use an environment variable to navigate to a directory.
cd $HOME
Explanation: The $HOME environment variable stores the path to the user's home directory (e.g., /home/user). This command is equivalent to cd ~.
Use Case: Scripting or automation where environment variables define directory paths.
pwd command to verify your current directory after using cd.cd with other commands (e.g., ls) to explore directories after navigation.