The terminal is a powerful tool that makes interacting with any Linux-based operating system easy. One such task that every user needs to do is navigating the file system. In Linux, to change the directory from the terminal, you can use the cd (change directory) command. This may seem like a daunting task for beginners, but fear not, as we are here to help. In this article, we explain the process to change directory in the Linux terminal.
cd Command in Linux: Syntax & Options
The cd command (change directory) is one of the most basic Linux commands. It is used to switch over to a different directory from your current directory throughout the file system. The basic syntax to use the cd command is:
cd <options> <path>
In the above syntax, you can specify the <path> as either the absolute path, where the paths start from the root directory, or as a relative path, where the paths start from the current working directory.
Some of the common options to pair with the Linux cd command are:
Options Description -L Follows symbolic links as normal directories. -P Switch over to the directory only if the actual directory exists and not as a symbolic link. -e Used with the -P flag when the current working directory cannot be determined and tells the command to exit with an error.
Change Directory in Linux Using Absolute Path
As mentioned above, the complete path is described right from the root directory. This method is easier for beginners to learn but entering the complete path becomes cumbersome to change into a directory that’s buried deep in your Linux file system.
Let’s say you need to access the Documents directory inside the home directory. The complete path will become:
To change inside the Downloads directory using the absolute path. Your shell prompt will now display the newly changed path.
Change Directory in Linux Using Relative Path
While the absolute path works fine for the cd command to change the directory inside the Linux file system, the path tends to become long for the directories that are buried deep inside the system. This is where the relative path comes into play.
In the relative mode, the path originates from the current working directory. The current directory is specified as a single dot “.” and the parent directory as double dots “..” here. So if you are in the Documents directory, and you need to access the Downloads directory inside the Home directory, the path becomes:
Seems complicated? The “..” climbs up the directory structure to the Home directory, and then, it will search for the Downloads directory. When found, it changes to the Downloads directory in the Linux Terminal.
To switch over to the bin directory inside the user directory, use this:
Fun Fact: You can use a series of “..” to climb up to the root directory and then travel to any directory of your choice. This is known as a “Local File Inclusion” (LFI) Vulnerability and is often used to access sensitive files like the /etc/passwd.