Linux commands: cd
By Flavio
Published · Updated
Tagged: CLI
Learn how the Linux cd command changes the directory you are in, using a folder name, the .. parent and . current shortcuts, or an absolute path from /.
Once you have a folder, you can move into it using the cd command. cd means change directory. You invoke it specifying a folder to move into. You can specify a folder name, or an entire path.
Example:
mkdir fruits
cd fruits
Now you are into the fruits folder.
You can use the .. special path to indicate the parent folder:
cd .. #back to the home folder
The # character indicates the start of the comment, which lasts for the entire line after it’s found.
You can use it to form a path:
mkdir fruits
mkdir cars
cd fruits
cd ../cars
There is another special path indicator which is ., and indicates the current folder.
You can also use absolute paths, which start from the root folder /:
cd /etc
Use pwd to print the folder you are in:
pwd
# /etc
Go back to your home folder
Run cd with no arguments to go to your home folder, from anywhere:
cd
~ is a shortcut for the home folder, so this does the same thing:
cd ~
You can use ~ to start a path, too:
cd ~/Documents
Go back to the previous folder
cd - takes you back to the folder you were in before. Run it again and you jump back. It’s great to switch between two folders:
cd /etc
cd ~/projects/site
cd - # back in /etc
cd - # back in ~/projects/site
It prints the folder it moved to. The previous folder is stored in the OLDPWD environment variable:
echo $OLDPWD
Folder names with spaces
This doesn’t work:
cd my folder
The shell splits the command on spaces, so cd gets two arguments, my and folder, and you get an error. In zsh it’s an odd one:
cd: string not in pwd: my
That’s because in zsh cd old new replaces old with new in the current path. It’s a zsh feature, not what you wanted here.
Wrap the name in quotes, or escape the space with a backslash:
cd "my folder"
cd my\ folder
Even easier, type the first letters and press Tab. The shell completes the name and adds the escapes for you.
Common errors
If the folder doesn’t exist, or you made a typo, you get:
cd: nope: No such file or directory
Names are case sensitive on Linux, so cd documents fails if the folder is Documents. On macOS the default file system is not case sensitive, so it works there.
If you don’t have permission to enter the folder, you get:
cd: noperm: Permission denied
To enter a folder you need the execute (x) permission on it. Check with ls -l and change it with chmod, or use sudo -i to get a root shell if it’s a system folder. sudo cd doesn’t help: cd is built into your shell, and sudo runs commands in a new process, so your shell’s folder never changes.
Why cd in a script doesn’t change your folder
If you put cd in a script and run it, your terminal stays in the same folder:
# go.sh
cd /tmp
bash go.sh
pwd # still where you were
The script runs in a new shell process. It changes the folder of that process, which ends when the script ends.
To run the script in your current shell, use source (or .):
source go.sh
pwd # /tmp
For a shortcut you use often, an alias or a shell function in your .bashrc or .zshrc is simpler. I explain scripts in more detail in my introduction to Bash scripting.
cd and symbolic links
If you cd into a symbolic link, the shell remembers the path you used, not where the link points:
cd link
pwd # /home/flavio/link
pwd -P # /home/flavio/real
Use cd -P to resolve the link and go to the real folder:
cd -P link
pwd # /home/flavio/real
Here’s how to create links with ln.
Jump around with pushd and popd
pushd works like cd, but it saves the current folder on a stack. popd takes you back to the last saved folder:
pushd /etc
pushd /var/log
dirs # /var/log /etc ~
popd # back in /etc
popd # back in ~
This is useful when you need to visit a few folders and come back to where you started.
Shortcuts in zsh
zsh, the default shell on macOS, has an option called AUTO_CD. With it, typing a folder name alone moves into it, without cd:
setopt auto_cd
projects # same as cd projects
Add setopt auto_cd to your ~/.zshrc to keep it on.
Both bash and zsh also support the CDPATH variable. It’s a list of folders that cd searches when the name you type isn’t in the current folder:
export CDPATH=.:~/projects
cd site # goes to ~/projects/site from anywhere
Keep . as the first entry, so that folders in your current location still win.
This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
Want me to talk about your product? You can sponsor this site.
Related posts about cli: