Linux / Commands / cd / Command to move to another directory

In Linux, the cd command changes the working directory of the current shell. For example, run cd /etc to move to that directory, then run pwd to check your current location.

Check current location and move

cd is short for change directory and is a command built into shells like Bash. You specify the directory you want to move to as an argument. The following command moves to /etc and displays that location.

cd /etc
pwd

If the move is successful, the cd command itself generally does not output anything. Make sure pwd shows /etc. Moving does not involve moving files or changing contents, but rather changes the reference location of the current shell. The working directory of other terminals does not change.

Absolute paths and relative paths

Absolute paths start from the root directory /. For example, the following command specifies /var/log regardless of your current location. The directory must exist and be accessible.

cd /var/log
pwd

Relative paths are based on the current location. For example, to move to /var and then into log inside it, execute as follows.

cd /var
cd ./log
pwd

If successful, /var/log will be displayed. . refers to the current directory, so ./log is log relative to the current location. If CDPATH is set in Bash, cd log can use alternative search paths, but cd ./log explicitly refers to the current directory. If the move fails, resolve the error before executing the next command.

Moving to the parent directory and the previous location

.. refers to the parent directory. If the current location is /var/log, the following command moves to /var.

cd ..
pwd

To return to the previously visited directory, use the following command.

cd -

cd - in Bash moves to the previous working directory and prints its path. It may fail if the previous location has not been set or is inaccessible. In the example above, if you moved from /var/log to /var immediately before, it will return to /var/log.

Moving to the home directory

If you run cd without a path, Bash moves to the home directory set in the HOME variable. ~ is also usually expanded to the current user's home directory.

cd
pwd

The home directory location differs depending on the account and settings, so do not always assume it is /home/user. cd ~/Documents moves under the home to Documents, and if that directory does not exist, it will fail. cd ~user in Bash is a way to replace user with the actual account name to indicate the home path of that account. It does not log in as another user or grant permissions, and you must have access rights to the target.

Paths with spaces and symbolic links

If a directory name contains spaces, enclose the path in quotes. The example below uses it when my folder is in the current directory.

cd "my folder"

When using the home path, you can place ~ outside the quotes and enclose the rest. The following is an example when my folder is under the home directory.

cd ~/"my folder"

If you moved via a symbolic link, the displayed path and the actual directory path may differ. Bash's pwd -P shows the physical path. cd -P moves based on the physical path interpreted from the link, and the result processed by .. may differ from the default behavior. For more details, check Bash's cd and pwd explanation.

Check when movement is not possible

  • Path not found: Check the spelling and case sensitivity, as well as your current location, and use ls to see if the name exists. The file is not a directory, so you cannot enter it with cd.
  • Permission denied: Make sure you have the search permission to access the target and its parent directories. Instead of arbitrarily changing permissions, request the necessary access from the administrator.
  • Unexpected location: Compare pwd and pwd -P, and check symbolic links or CDPATH settings.

You can view the help for built-in commands in Bash with help cd. In scripts, you need to check the exit status to ensure that cd does not continue with subsequent operations when it fails.