Open In App

Absolute and Relative Pathnames in UNIX

Last Updated : 08 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Unix and Linux systems, paths define the location of files and directories within the filesystem. They are crucial for navigating and managing files efficiently. Paths can be classified into two types: absolute paths and relative paths. Let us have a better look in these paths.

What is an Absolute Path?

An absolute path is a full path that specifies the location of a file or directory from the root directory (‘/’). It provides a complete address that points directly to a file or directory, regardless of the current working directory. This path type always begins with the root directory, followed by subdirectories, and ends with the desired file or directory name.

Characteristics of Absolute Paths:

  • Starts with a slash (/).
  • Specifies a file location from the root directory.
  • Does not depend on the current directory.

For Example:

If you want to access a file named ‘abc.sql’ located in the directory ‘/home/kt’, you would use the following command:

$cat abc.sql

This command will work only if the file “abc.sql” exists in your current directory. However, if this file is not present in your working directory and is present somewhere else say in /home/kt , then this command will work only if you will use it like shown below:

cat /home/kt/abc.sql

In the above example, if the first character of a pathname is ‘/’, the file’s location must be determined with respect to root. When you have more than one / in a pathname, for each such /, you have to descend one level in the file system like in the above ‘kt’ is one level below home, and thus two levels below root.

What is a Relative Path?

A relative path specifies the location of a file or directory in relation to the current working directory (often abbreviated as pwd). It does not start with a slash (‘/’), and it utilizes navigational shortcuts to refer to the file or directory.

Characteristics of Relative Paths:

  • Does not begin with a slash (‘/’).
  • Dependent on the current directory.
  • Utilizes shortcuts like ‘.’ (current directory) and ‘..’ (parent directory) to navigate the filesystem.

Using . and .. in Relative Path-names

UNIX offers a shortcut in the relative pathname– that uses either the current or parent directory as reference and specifies the path relative to it. A relative path-name uses one of these cryptic symbols:

  • . (Dot): Represents the current directory.
  • .. (Double Dots): Represents the parent directory.

Now, what this actually means is that if we are currently in directory ‘/home/kt/abc’ and now you can use ‘..’ as an argument to ‘cd’ to move to the parent directory /home/kt as :

$pwd
/home/kt/abc
$cd ..               ***moves one level up***
$pwd
/home/kt

Note: Now ‘/ ‘ when used with ‘..’ has a different meaning; instead of moving down a level, it moves one level up:

$pwd
/home/kt/abc        ***moves two level up***
$cd ../..
$pwd
/home

Example of Absolute and Relative Path

Suppose you are currently located in ‘home/kt’ and you want to change your directory to ‘home/kt/abc’. Let’s see both the absolute and relative path concepts to do this:

1. Changing directory with relative path concept:

$pwd
/home/kt
$cd abc                   
$pwd
/home/kt/abc         

2. Changing directory with absolute path concept:

$pwd
/home/kt
$cd /home/kt/abc
$pwd
/home/kt/abc

Conclusion

Understanding absolute and relative paths is fundamental for anyone working with Unix or Linux systems. These paths not only facilitate precise file location references but also enhance efficient filesystem navigation. By mastering these concepts, users can improve their productivity and streamline their workflow in a Unix-like environment.



Article Tags :

Similar Reads

three90RightbarBannerImg