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
.
Table of Contents
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]
ShellScriptMove to Subdirectory
The following command changes the working directory to the subdirectory test
of the current working directory.
cd test
ShellScriptMove to Parent Directory
The parent directory is denoted by two dots ‘..
‘. cd ..
moves to the parent directory in the directory hierarchy.
cd ..
ShellScriptMove with Relative Path
This moves to the Downloads
directory located under the parent directory relative to the current directory.
cd ../relative/path
ShellScriptMove with Absolute Path
To change the working directory to an absolute path, simply type the full path after the cd
command.
cd /absolute/path
ShellScriptMove to Previous Directory
This moves to the last working directory where the cd
command was previously used.
cd -
ShellScriptIn 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.
Move to Home Directory
In Linux, the home directory is represented by a tilde ~
. The following command moves to the home directory.
cd ~
ShellScriptThis command moves to the user’s home directory regardless of the current working 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 namedshots
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.
Using Environment Variables
You can change directories using environment variables set with the export
command.
export MYDIR=/path/to/directory
cd $MYDIR
ShellScriptThe figure below shows an example of changing directories using an environment variable.
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
.
dirs
The dirs
command shows the paths stored in the stack.
popd
Each time popd
is entered, the top of the stack is removed and the last saved directory is returned to.
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.
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! 🙂