Linux Basics

12 / 107

Relative and Absolute path

There are two ways to represent a file/directory path:

Absolute Path:

This way of representing a file/directory is independent of the current directory of the user. Such paths start with the root directory (/). So, as we know cd / takes us to the root directory, and it is the top-level directory, absolute path always starts with /, which means we basically first go to the root directory and then follow the path specified by the user because any directory will always have a path from the root directory.

So it doesn't matter wherever we are, because we'll have to go first to the root directory and traverse from there, so the absolute path of a directory remains constant.

Relative Path:

This way of representing a file/directory is dependent on the current directory of the user. We don't start these paths with /, instead we follow the path relative to our current directory. And as we can be in different directories at different times, the relative path is not constant.

For example, considering the previous lecture's figure, suppose we are currently in the sandeep directory and we want to go to the john directory. So, as an absolute path is independent of the current directory, it will always be the same for a directory. So, for john, it will always be /home/john.

On the other hand, it's relative path will be ../john. So by .., we first traverse to our parent directory home and from there we go to directory john.

If we would have been in the project directory, the absolute path would have remained the same because it always starts with the root directory. But the relative path would have changed to ../../john. That means we first go to the parent directory of project, which is sandeep, and then we have to go to home and then we traverse to john from there.

So, if we would have been in sandeep directory, and we wished to go to project directory, so using absolute path, we could have done that by:

cd /home/sandeep/project

and using relative path, it would have been:

cd project

because we are already in the directory sandeep and project is a sub-directory of sandeep.


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...