How to navigate between directories using Terminal on Ubuntu/Linux machine?
Use the cd
command to navigate between directories, which works on all Linux distributions.
The word "cd" stands for Change Directory, which is commonly used to change the current working directory to the new path provided along with the command.
What do we mean by the Current Working Directory?
The Current Working Directory is a directory or folder that a user is currently working on.
Before we look at the commands, let's consider the below directory structure.
- The root is represented using
/
- The directory
var
is one directory away from the root directory/
- The directory
www
is one directory away from var, and two directories away from the root directory/
- The directory
html
is one directory away fromwww
, two directories away fromvar
, and three directories away from the root directory/
/
| - var/
| | - www/
| | - html
| ...
| ...
...
Use the below command to change the current working directory to www
from the root directory.
cd var/www
Similarly, use the below command to change the current working directory to html
from the root directory.
cd var/www/html
Use the below command to change the current working directory to www
from any directory on the machine.
NOTE: The path becomes an absolute path by adding a slash before the path as shown in the below command. Otherwise, it is considered a relative path.
cd /var/www
Similarly, use the below command to change the current working directory to html
from any directory on the machine.
cd /var/www/html
Similarly, use the below command to change the directory to the root directory from anywhere on the machine.
cd /
Use the below command to change the current working directory to html
from the www
directory.
cd html
Use the below command to navigate up one directory.
cd ..
Similarly, use the below command to navigate up two directories.
cd ../../
Use the below command to switch back to the previous directory you were working on earlier, which may not be close to the current working directory.
cd -
Use the below command to navigate to the user's home directory from anywhere on the machine.
On Ubuntu/Linux machine, every user has a home directory with their login username, which is considered the user's home directory.
cd ~
Use the below command to find the current working directory.
When we start working with a terminal, we need to navigate between directories using commands on the terminal.
If the current working directory is not too far from the root directory, we can easily identify the current working directory on the terminal itself. But it's difficult to identify on the terminal if it is too far from the root directory. In such a case, the below command is useful in finding the current working directory path.
pwd
Here the word pwd
stands for Print Working Directory.
Use the below command to list the files in the current working directory.
ls