cd Linux Command
Changes the current working directory to the specified path.
What does cd do?
Changes the current working directory to the specified path.
Syntax
cd [directory]Practical example
cd /etc/aptQuestions about cd
How do I go back to the previous directory?
Use `cd -` to switch back to the last directory you were in. This is useful when toggling between two locations.
How do I go to my home directory?
Use `cd` with no arguments, or `cd ~`, or `cd $HOME` to return to your home directory from anywhere.
How do I go up one directory level?
Use `cd ..` to move to the parent directory. Use `cd ../..` to go up two levels, and so on.
How do I cd to a directory with spaces?
Enclose the path in quotes: `cd "My Documents"` or escape the space: `cd My\ Documents`.
How do I cd to the directory of a file?
Use `dirname` with the file path: `cd $(dirname /path/to/file.txt)` to go to the directory containing that file.
What is the difference between cd and pushd?
`cd` simply changes directory. `pushd` changes directory and saves the previous one on a stack. Use `popd` to return to saved directories.
How do I cd to the root directory?
Use `cd /` to go directly to the root directory of the filesystem.
Related Navigation Commands
lsLists files and directories in the current directory.
cd ..Moves up one directory level (to the parent directory).
pwdPrints the current working directory (full path from root).
cd ~Changes to the current user's home directory.
dirsDisplays the directory stack (list of directories saved with pushd).
cd -Switches back to the previous working directory.