cd – change directory

cd command
cd command

cd changes your current working directory. It’s one of the most fundamental commands for navigating the filesystem.

Synopsis

cd [DIRECTORY]

Common Usage

CommandEffect
cd /path/to/dirGo to absolute path
cd dirnameGo to relative path
cd or cd ~Go to home directory
cd ..Go up one level (parent directory)
cd -Go to previous directory
cd ~/DocumentsGo to Documents in home

Examples

$ pwd
/home/greys
$ cd /etc
$ pwd
/etc
$ pwd
/home/greys
$ cd projects/ansible
$ pwd
/home/greys/projects/ansible

Go to home directory

$ cd
$ pwd
/home/greys

# Or explicitly
$ cd ~
$ pwd
/home/greys

Go up one level

$ pwd
/home/greys/projects
$ cd ..
$ pwd
/home/greys

Go up multiple levels

$ pwd
/home/greys/projects/ansible/roles
$ cd ../../..
$ pwd
/home/greys

Return to previous directory

$ pwd
/home/greys
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/greys
$ pwd
/home/greys

Use tilde for other users’ homes

$ cd ~admin
$ pwd
/home/admin

Special Directory Entries

Every directory contains two special entries:

EntryMeaning
.Current directory
..Parent directory
$ ls -la
drwxr-xr-x  5 greys staff 4096 Jan 15 10:00 .
drwxr-xr-x  3 greys staff 4096 Jan 15 09:00 ..

Permission Denied

If you can’t access a directory, you’ll see an error:

$ cd /root
bash: cd: /root: Permission denied

To enter a directory, you need execute permission (x) on it:

$ ls -ld /root
drwx------ 4 root root 4096 Jan 15 10:00 /root

Tips

  • Tab completion: Type cd pro<TAB> to autocomplete directory names
  • Case sensitive: cd Documentscd documents on Linux
  • Spaces in names: Use quotes cd "My Documents" or escape cd My\ Documents
  • Use pushd/popd: For navigating between multiple directories
  • CDPATH variable: Set common parent directories for quick access

Set up CDPATH

# Add to ~/.bashrc
export CDPATH=.:~:~/projects

# Now you can cd to any project from anywhere
$ cd ansible    # Goes to ~/projects/ansible

See Also

  • pwd — Print current working directory
  • ls — List directory contents
  • pushd/popd — Directory stack navigation

Tutorials