cd stands for change directory. It moves your shell to another place in the filesystem. It is built into the shell, because a separate program could not change the shell's own working directory.
Going somewhere
Give cd the directory you want. An absolute path starts with / and works from anywhere:
$ cd /var/log
$ pwd
/var/logA successful cd prints nothing. Use pwd to confirm where you landed.
A relative path starts from where you are now:
$ cd /home/pete
$ cd Documents/reports
$ pwd
/home/pete/Documents/reportsSpecial names
Every directory contains two special entries. . means the directory itself, and .. means its parent, one level up.
$ pwd
/home/pete/Documents/reports
$ cd ..
$ pwd
/home/pete/Documents
$ cd ../..
$ pwd
/homeShortcuts
cdwith no argument takes you to your home directory.cd ~does the same.~/DocumentsmeansDocumentsinside your home.cd -returns to the previous directory and prints its path.
$ cd /etc
$ cd /tmp
$ cd -
/etcSpaces and errors
A space separates arguments, so quote or escape names that contain one:
$ cd "My Photos"
$ cd My\ PhotosEither line works from the directory that holds My Photos. If the target does not exist, or is a file, you stay where you are and see an error:
$ cd nothere
bash: cd: nothere: No such file or directory
$ cd notes.txt
bash: cd: notes.txt: Not a directoryThe wording differs slightly in other shells. Pressing the Tab key completes directory names for you, which saves typing and avoids spelling mistakes.
Practise moving around in the shell navigation lab.
Try it
- Run
cd /usr, thenpwd. - Go up one level with
cd ..and check again. - Return home with plain
cd. - Use
cd -twice and watch it switch between two directories.
Check your understanding
cd (Change Directory)
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.