Skip to content

Linux Foundations / Find your way around Linux ยท Step 14 of 63

Lesson: cd (Change Directory)Switch
โ† Linux Foundations

Shell and files

cd (Change Directory)

Move around the Linux filesystem with cd using absolute paths, relative paths and shortcuts.

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/log

A 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/reports

Special 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
/home

Shortcuts

  • cd with no argument takes you to your home directory.
  • cd ~ does the same. ~/Documents means Documents inside your home.
  • cd - returns to the previous directory and prints its path.
$ cd /etc
$ cd /tmp
$ cd -
/etc

Spaces and errors

A space separates arguments, so quote or escape names that contain one:

$ cd "My Photos"
$ cd My\ Photos

Either 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 directory

The 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

  1. Run cd /usr, then pwd.
  2. Go up one level with cd .. and check again.
  3. Return home with plain cd.
  4. 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.