Absolute and Relative Pathnames in UNIX
Last Updated :
08 Sep, 2024
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.
Similar Reads
Soft and Hard links in Unix/Linux
A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere. There are two types of li
6 min read
Bash Pathname Expansion in Linux
When you type a command and press enter, bash performs several processes upon the text before carrying out our command as a command. The process that makes this happen is called expansion. For example, suppose you use the echo command for standard output. echo hi Output hi If you use echo with aster
3 min read
List out all the Shells Using Linux Commands
When you're operating out of a Linux environment, the shell is your primary tool for interacting with the OS. It's your command interpreter â it translates what you type into what the OS can interpret and carry out. From basic operations like looking at files to running complex scripts, the shell ma
4 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and print the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the 'dirname' command is a useful tool for e
3 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting o
8 min read
How to Exclude Certain Paths With the find Command in Linux
The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in
3 min read
How to Get the Full Path of a File in Linux
While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let's consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem.Before w
3 min read
basename Command in Linux with examples
The 'basename' command in Linux is a fundamental utility used in file manipulation and script writing. It simplifies file paths by stripping directory information and optional suffixes from file names. Here a detailed overview of the 'basename' command, including its syntax, options, and practical u
3 min read
env command in Linux with Examples
The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the s
3 min read
File Management in Linux
In Linux, most of the operations are performed on files. And to handle these files Linux has directories also known as folders which are maintained in a tree-like structure. Though, these directories are also a type of file themselves. Linux has 3 types of files: Regular Files: It is the common file
4 min read