Navigating the File System with 'cd'
Absolute Paths vs. Relative Paths
When using the cd
command, you can specify the directory you want to navigate to using either an absolute path or a relative path.
An absolute path is the complete path from the root directory (/
) to the target directory. For example, /home/user/documents
is an absolute path.
A relative path is a path that is relative to the current working directory. For example, if you are currently in the /home/user
directory and you want to navigate to the documents
subdirectory, you can use the relative path documents
.
Navigating Up the Directory Tree
To navigate up the directory tree, you can use the ..
(two dots) shortcut. This represents the parent directory of the current directory. For example, if you are in the /home/user/documents
directory and you want to navigate to the /home/user
directory, you can use the following command:
$ cd ..
You can also use multiple ..
to navigate up multiple levels of the directory tree. For example, cd ../../
would move you up two levels, from /home/user/documents
to /home
.
Navigating to the Root Directory
To navigate directly to the root directory (/
), you can use the following command:
$ cd /
This will change the current working directory to the root directory, regardless of your current location in the file system.
Navigating to the Previous Directory
To navigate back to the previous directory you were in, you can use the -
(hyphen) shortcut:
$ cd -
This will change the current working directory to the previous directory you were in.
Navigating to the Home Directory
As mentioned in the previous section, you can quickly navigate to your home directory by using the cd
command without any arguments:
$ cd
This will change the current working directory to your home directory.
Navigating with Tab Completion
Linux shells, such as Bash, offer tab completion for the cd
command. This means that you can start typing a directory name and then press the Tab key, and the shell will automatically complete the path for you. This can save you time and reduce the risk of typing errors.
$ cd doc[Tab]
This will complete the path to the documents
directory, if it exists in the current working directory.
By mastering these techniques for navigating the file system with the cd
command, you can become more efficient and productive when working in a Linux environment. In the next section, we will explore some advanced cd
techniques and shortcuts.