Changing the Current Working Directory
The current working directory is the directory in which the shell is currently operating. Changing the current working directory is a fundamental task in navigating the Linux file system.
The cd
Command
The primary command used to change the current working directory is cd
. The syntax for the cd
command is:
cd <directory>
where <directory>
is the path to the directory you want to change to.
Here are some examples of using the cd
command:
## Change to the /home/user/Documents directory
cd /home/user/Documents
## Change to the user's home directory
cd ~
## Change to the parent directory
cd ..
## Change to the root directory
cd /
Absolute and Relative Paths
When specifying a directory path with the cd
command, you can use either an absolute path or a relative path.
- Absolute Path: An absolute path is a complete and unambiguous reference to a directory, starting from the root directory (
/
). For example, /home/user/Documents
.
- Relative Path: A relative path is a reference to a directory that is relative to the current working directory. For example, if you are in the
/home/user
directory and you want to change to the Documents
subdirectory, you can use the relative path Documents
.
Tab Completion
To make directory navigation more efficient, you can use the tab completion feature in the Linux command line. When you start typing a directory name and press the Tab
key, the shell will automatically complete the name if it is unambiguous.
## Start typing a directory name and press Tab
cd Doc<Tab>
## The shell will complete the directory name to "Documents"
Understanding how to change the current working directory using the cd
command, as well as the concepts of absolute and relative paths, is essential for navigating the Linux file system from the command line.