cd – Change Directory
The cd command stands for change directory and is used (case-sensitive!) to change to a specified directory or subdirectory. It is a shell builtin command, essential for navigating the filesystem.
Syntax
cd [OPTION] DIRECTORY
- If no directory is specified,
cdchanges to the user’s home directory.
Options
| Option | Description |
|---|---|
-L |
Follow the logical directory structure (default behavior) |
-P |
Follow the physical directory structure (resolves symlinks) |
These options mostly affect behavior when symbolic links are involved. In everyday use,
cdwithout options uses the logical path (-L).
Practical Examples
1. Change to the home directory
cd ~
or simply
cd
Both commands will take you to your home directory. The home directory is defined by the environment variable $HOME and is not necessarily /home/username.
2. Change to an absolute directory
cd /usr/local
Changes directly to /usr/local.
3. Change to a relative directory (subdirectory)
cd bin
If your current directory is /usr/local, this moves into /usr/local/bin.
4. Change to the parent directory
cd ..
Moves one directory up (to the parent directory).
5. Using physical path resolution
cd -P /path/to/symlinked/directory
Follows the actual physical directory structure, resolving symbolic links.
Tips
- Use
pwdaftercdto print your current directory and verify navigation. cd -switches to the previous directory you were in.- Tab completion often helps auto-complete directory names.
- When scripting, using
cdcarefully can avoid errors related to unexpected directories.
Summary
The cd command is the most fundamental way to navigate the filesystem in the shell. It supports logical vs physical path navigation, but is most commonly used without any options to quickly move around directories.
See Also
pwd– print working directoryls– list directory contents- Shell environment variables like
$HOME,$PWD, and$OLDPWD