You need to use the cd command to move from your present directory to another directory. You must have execute or search permission in the specified directory. This page shows basic examples of how to change directory in Linux using the cd command.
|Requirements||Bash with the terminal application|
|Est. reading time||2 minutes|
How to change directory in Linux
The procedure changes a directory or folder in Linux as follows:
- Let us change to your home directory in Linux, run: cd
- If you want to change to the /etc/security/ directory on Linux, execute: cd /etc/security/
- Want to go up one level of the directory tree in Linux? Try: cd ..
Let us see some examples and usage in details.
All examples tested with GNU/bash version 5.0.17 running on Ubuntu Linux 20.04 LTS desktop.
Changes the current directory in Linux
Let us change the current working directory to the home (login) directory in Linux, run:
The default DIR is the value of the HOME shell variable. We can print that using the printf command/echo command as follows:
To print the current working directory, use the pwd command:
Next change to an arbitrary directory named /etc/ufw/, type:
At this stage, you may want to list the directory contents. Hence, try the ls command:
Say you need to go down one level of the directory tree (say you want to change to the applications.d), run:
How do I go back to my home folder or directory in Linux?
Simply type any one of the following command:
How to change a folder in Linux
Get list of directories only in the current directory using the following:
ls -d */
Once you know the directory names, change it as per your needs:
To move back to a parent directory directory, type:
Linux change directory and symbolic links
One can force symbolic links to be followed. The syntax is:
cd -L dir1
cd -L link2
To use the physical directory structure without following symbolic links:
cd -P link2
Shell environment variables
- CDPATH : The search path for the cd command. It is a : separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string. We can set it as follows:
## another example ##
Now go to /home/vivek/projects/aws, I can simply type
# OR #
- HOME : The name of the directory, used when no directory operand is specified.
- OLDPWD : The previous working directory as set by the cd command.
- PWD : The current working directory as set by the cd command.
A note about the autocd option
If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells. Turn it on:
shopt -s autocd
Now type directory or folder names such as bin in the $HOME and it will automatically convert to “cd $HOME/bin”:
shopt -u autocd
Autocorrect errors/typos of a folder name
Suppose cdspell is set to on, minor errors in the spelling of a directory component in a cd command will be corrected. For example:
shopt -s cdspell # Turn it on cd Downloadz shopt -u cdspell # unset it/ turn it off
This page explained the cd command that allows you to change directories. For more info type the following command at the terminal or read man page online:
Also do check your shell man page. For instance:
🐧 4 comments so far... add one ↓
|Category||List of Unix and Linux commands|
|Disk space analyzers||ncdu • pydf|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||NetHogs • dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|