The cd Commands You Will Use Most
cd /var/www
cd ..
cd -
cd ~
cd changes the folder your shell works in. The first line goes to an absolute path, cd .. goes up one level, cd - returns to the previous folder, and cd ~ goes to your home folder; cd with nothing after it does the same. pwd prints where you are now.
Absolute and Relative Paths
| Path | Starts from |
|---|---|
/var/log/nginx | The root folder |
log/nginx | The current folder |
../shared | The parent folder |
~/projects | Your home folder |
A path that starts with / means the same thing wherever you are. Any other path is relative to the current folder, so cd logs works only where a logs folder exists. Run pwd when a relative path does not lead where you expected. cd ../.. goes up two levels, and . stands for the current folder.
Go Back with cd -
cd /etc/nginx/sites-available
cd /var/log/nginx
cd -
cd - switches to the previous folder and prints its path. Run it again to switch back. The shell keeps the previous folder in the OLDPWD variable.
Home Folders and the Tilde
~ stands for your home folder, for example /home/deploy, and ~/backups is a folder inside it. ~deploy is the home folder of the user deploy. The home folder of root is /root, so cd ~ after sudo -i goes there.
Tab Completion and Names with Spaces
cd "/srv/client files"
cd /srv/client\ files
Type the first letters of a name and press Tab: the shell completes it and escapes any space for you. Press Tab twice to list the choices when several names match. Without quotes or a backslash, the shell splits client files into two words, and cd stops with an error about too many arguments.
A Folder Stack with pushd and popd
pushd /etc/nginx
pushd /var/log
dirs -v
popd
pushd changes the folder and puts the one you left on a stack; popd returns to the last folder on the stack. dirs -v shows the stack with numbers. This helps when one task moves you between several folders.
cd in Scripts and with sudo
- A script cannot change your shell's folder: a script runs in its own process, so its cd ends with it. Run it with
source script.shif it must change your folder. - Check cd in scripts: write
cd /var/www/app || exit 1, so that a failed cd stops the script before later commands run in the wrong folder. See how to write and run a bash script. - sudo cd does not work: cd is built into the shell, not a separate program, so sudo cannot run it. For a folder only root can enter, run the command you need with sudo, such as
sudo ls /root, or open a root shell withsudo -i. - CDPATH: this variable lists folders that cd searches for relative names. If it is set in your .bashrc, cd can land somewhere unexpected; leave it unset in scripts.
Symbolic Links
When you enter a folder through a symbolic link, the shell shows the link path. pwd -P prints the real path, and cd -P goes to it. See how to create a symlink in Linux.
Errors cd Prints
- No such file or directory: a typo, wrong letter case, or a relative path from the wrong folder. Check with
pwdandls; see No such file or directory. - Not a directory: the name is a file, or one part of the path is a file.
ls -lshows the type as the first character:dfor a folder. - Permission denied: entering a folder needs execute (
x) permission on it; read permission alone lets you list names but not enter.ls -ldshows the permissions, and chmod recursive explains directory modes.