Linux cd Command: 10 Examples with Details

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

Example 1: Change to a Specific Directory

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.

Example 2: Change to the Home Directory

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.

Example 3: Move Up One Directory

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.

Example 4: Move Up Multiple 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.

Example 5: Change to the Root Directory

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.

Example 6: Change to a Directory with Spaces

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.

Example 7: Return to the Previous Directory

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.

Example 8: Change to a Directory Using a Relative Path

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.

Example 9: Change to Another User's Home Directory

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.

Example 10: Change to a Directory Using an Environment Variable

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.

Additional Notes