8 Ways to Use the Linux Command cd

When using Linux, you will inevitably use the terminal, and the cd command is indispensable. Let’s explore how to use the Linux command cd.

Basic Usage of the Linux Command cd

The cd command stands for “change directory” and is used to change the current working directory to a specified directory.

cd [option] [directory]
ShellScript

Move to Subdirectory

The following command changes the working directory to the subdirectory test of the current working directory.

cd test
ShellScript
Figure 1. Linux command cd: Move to Subdirectory
Figure 1. Linux command cd: Move to Subdirectory

Move to Parent Directory

The parent directory is denoted by two dots ‘..‘. cd .. moves to the parent directory in the directory hierarchy.

cd ..
ShellScript
Figure 2. Linux command cd: Move to Parent Directory
Figure 2. Linux command cd: Move to Parent Directory

Move with Relative Path

This moves to the Downloads directory located under the parent directory relative to the current directory.

cd ../relative/path
ShellScript
Figure 3. Linux command cd: Move with Relative Path
Figure 3. Linux command cd: Move with Relative Path

Move with Absolute Path

To change the working directory to an absolute path, simply type the full path after the cd command.

cd /absolute/path
ShellScript
Figure 4. Linux command cd: Move with Absolute Path
Figure 4. Linux command cd: Move with Absolute Path

Move to Previous Directory

This moves to the last working directory where the cd command was previously used.

cd -
ShellScript

In the example below, I moved from the ~/linux directory to the /etc/init.d directory, then back to the previous directory ~/linux, and then back again to the last directory /etc/init.d. The cd command provides the full path of the previous working directory before moving.

Figure 5. Linux command cd: Move to Previous Directory
Figure 5. Linux command cd: Move to Previous Directory

Move to Home Directory

In Linux, the home directory is represented by a tilde ~. The following command moves to the home directory.

cd ~
ShellScript

This command moves to the user’s home directory regardless of the current working directory.

Figure 6. Linux command cd: Move to Home Directory
Figure 6. Linux command cd: Move to Home Directory

Using Options

The cd command has special options, namely the -L and -P options, used when moving to the parent directory (..). By default, the -L option is applied.

The -L option stands for logical parent directory, and the -P option stands for physical parent directory. Here are some examples of when they can be applied.

ln -s ~/Pictures/Screenshots shots
cd shots
cd ..
cd shots
cd -P ..
ShellScript
  • Line 1: The ln command creates a symbolic link named shots in the home directory pointing to the ~/Pictures/Screenshots directory.
  • Line 2: Move to the symbolic link shots.
  • Line 3: Move to the logical parent directory, which is the home directory.
  • Line 4: Move back to the symbolic link shots.
  • Line 5: Move to the physical parent directory ~/Pictures, which means moving based on the original path of the symbolic link, not the link itself.

The result of entering the above commands is shown in the figure below.

Figure 7. Using options with the Linux cd command
Figure 7. Using options with the Linux cd command

Using Environment Variables

You can change directories using environment variables set with the export command.

export MYDIR=/path/to/directory
cd $MYDIR
ShellScript

The figure below shows an example of changing directories using an environment variable.

Figure 8. Using environment variables with the Linux cd command
Figure 8. Using environment variables with the Linux cd command

Remember All Previous Paths: pushd & popd

The cd command cannot remember all previous paths, only toggling between the last two.

Using pushd, you can add paths to a directory stack and popd allows you to return to the last directory saved in the stack.

pushd

The following example shows how to add paths to the stack each time you change the directory using pushd.

Figure 9. Adding paths to the directory stack: pushd command
Figure 9. Adding paths to the directory stack: pushd command

dirs

The dirs command shows the paths stored in the stack.

Figure 10. Viewing paths in the directory stack: dirs
Figure 10. Viewing paths in the directory stack: dirs

popd

Each time popd is entered, the top of the stack is removed and the last saved directory is returned to.

Figure 11. Returning to the last path in the stack: popd
Figure 11. Returning to the last path in the stack: popd

This method is useful for moving between multiple paths while working and needing to return through those paths.

Precautions

The Linux operating system, unlike Windows, is case-sensitive. Therefore, when entering paths, you must input uppercase and lowercase letters accurately. If you don’t, you will encounter the “No such file or directory” message as shown below.

Figure 12. Message when the path is not entered accurately
Figure 12. Message when the path is not entered accurately

Summary

We explored in detail how to use the cd command to change the working directory. From simple path changes to the use of special options, and even the unique pushd and popd commands. Understanding these aspects will help you use the Linux file system more efficiently. If you have any further questions or need more specific examples, feel free to ask! 🙂

Reference

Leave a Comment