Bash Change Directory

Master the cd command, including absolute and relative paths and the ~ and .. shortcuts, to move around the filesystem with confidence.

#Jobseeker

Find matching jobs & Auto apply with AI

Hyring
Jobseeker using Hyring to search, apply, and prepare with AI

The cd Command

cd changes the shell's current working directory. Unlike ls or pwd, cd is a shell builtin rather than a separate program, because an external program cannot change the working directory of the shell that launched it.

Absolute vs. Relative Paths

An absolute path always starts with a forward slash / and describes the full route from the filesystem root, so it works no matter where you currently are. A relative path has no leading slash and is interpreted starting from your current working directory.

Moving with an Absolute Path

$ cd /var/log
$ pwd
/var/log

Moving with a Relative Path

$ pwd
/home/alex
$ cd Documents/Projects
$ pwd
/home/alex/Documents/Projects
  • ~ jumps straight to your own home directory
  • ~alice jumps to another user's home directory, if you have permission
  • '.' refers to the current directory
  • '..' refers to the parent of the current directory
  • - jumps back to whatever directory you were in before your last cd
Note: Typing cd - by itself is a fast way to toggle between two directories you are switching between repeatedly, such as a source folder and a build output folder.

Chaining .. and ~

You can combine .. and / to climb multiple levels in one command, and you can mix ~ with a relative tail to jump home and then descend into a specific folder, all without ever typing an absolute path.

Combining Shortcuts

$ pwd
/home/alex/Documents/Projects/app
$ cd ../../..
$ pwd
/home/alex
$ cd ~/Downloads
$ pwd
/home/alex/Downloads
ShortcutMeaning
~Current user's home directory
~aliceUser alice's home directory
.Current directory
..Parent directory
-Previous working directory
/Filesystem root