Bash Change Directory
Master the cd command, including absolute and relative paths and the ~ and .. shortcuts, to move around the filesystem with confidence.
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/logMoving 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
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