Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

cd

Change the current folder of your shell.

Level
RHCSA exam
Group
Files and folders
Needs root
No
Package
bash (built-in)

What it does

cd (change directory) moves your shell to another folder. It is built into the shell, because only the shell itself can change its own current folder.

Special targets: cd alone or cd ~ = your home folder, cd - = the previous folder, cd .. = one level up, cd / = the root.

The variable CDPATH can hold a list of base folders that cd also searches.

How to write it

cd [-L|[-P [-e]]] [DIRECTORY]

Options 6

Most used

DIRECTORY
The folder to go to. Without it: your home folder.
-
Go back to the previous folder (and print its name).
-L
Follow symbolic links logically (the default): pwd shows the link path.
-P
Use the physical folder: resolve symbolic links.
-e
With -P: return an error if the current folder cannot be found.
-@
On systems that support it: show extended attributes as a folder.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Go to a folder

    cd /var/log

    An absolute path works from anywhere.

  2. 2Go back to your home folder

    cd

    Same as cd ~ or cd $HOME.

  3. 3Switch between two folders

    cd /etc/nginx/conf.d
    cd /var/log/nginx
    cd -

    cd - jumps back and forth. Very handy while you edit config and read logs.

  4. 4Go up one or two levels

    cd ..
    cd ../..

    .. is the parent folder.

  5. 5Go to the home folder of another user

    cd ~alice

    Works for root or when you have the rights to that folder.

  6. 6Go to the real folder behind a link

    cd -P /etc/alternatives/java
    pwd

    Shows the real path of the link target.

  7. 7Stop a script when cd fails

    cd /srv/app || exit 1

    Without this check, the next commands (like rm -rf *) would run in the wrong folder.

  8. 8Use the folder of the script itself

    cd "$(dirname "$0")"

    Makes a script work from any place it is called from.

  9. 9Use a short base path list

    export CDPATH=.:/srv/apps:/etc
    cd ssh

    cd ssh now finds /etc/ssh. Put CDPATH in ~/.bashrc if you like it.

  10. 10Remember folders on a stack

    pushd /etc/ssh
    pushd /var/log
    popd

    pushd and popd keep a list of folders. dirs -v shows it.

  11. 11Go to a folder with spaces in its name

    cd "/mnt/backup/Project Files"

    Quotes (or a backslash before the space) keep the name together.

Watch out

  • cd in a script changes only the folder of that script, not of your shell. Use source if you need that.
  • sudo cd /root does not work (cd is a built-in). Use sudo -i and then cd.

Tips to remember

  • cd = home, cd - = back, cd .. = up.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man cd