Home » Linux » Tips » Absolute Path Linux

Getting the Absolute (Full) and Relative Path In Linux

This article explains absolute paths and how they differ from relative paths, getting them, and how symbolic links are handled.

FileSystem Paths

path is the location of a file in a file system. It’s the directions to the file in the folder it is located.

A path consists of a string of characters. Some represent directory names, and a separator character separates the directory names from the file name and extension.

Consider the below path:

/path/to/my/file.txt

It consists of:

  • Forward slashes (/) to separate directories from their subdirectories
  • Directory Names – the text between the forward slashes
  • The file name – in this case, file.txt

Relative Paths

Relative paths are paths that are defined in relation to your current position in the file system.

You can find your current position using the pwd command:

Here’s how to use the pwd command.

Relative paths begin without a forward slash, or a . or ...

  • Paths beginning without a / or with a . start in the current directory
  • Paths beginning with .. start in the directory above the current directory (the parent of the current directory)

Example

If you are in the directory /home/user:

cd /home/user

…and it contains a file called test.txt, you need only type:

cat text.txt

…to view the contents of the file, as you are in the same directory as that file, and can access it using its relative path.

In the above example, the cd command is used to change the directory, and the cat command reads the contents of the file to the screen.

Absolute Paths

Absolute paths can be used from anywhere on the filesystem. They represent the full path of the file from the root (the very top) of the file system hierarchy. They are absolute because it’s the absolute location of the file on the file system. It is not relative to where you are or where you might be; the path will work when called from any location.

Example

Consider again that we are in the directory /home/user:

cd /home/user

…and there is a file called another.txt located at /var/temp. Attempting to open it by running:

cat another.txt

…will fail because that file doesn’t exist at /home/user. We can, however, access it at its absolute path:

cat /var/temp/another.txt

As it provides the full location of the file and does not rely on the relative path we are currently situated.

Paths and Hard / Soft Links (symlinks)

Soft links (symlinks) are just files that point to another file. The absolute path is still the path to the symlink. If you want the path to the linked file itself, you will need to use the readlink command to find the absolute path of the linked file:

readlink -f /path/to/symlink

Find out more about readlink here.

Hard links are also absolute paths – the data exists at multiple absolute paths but in only one location on the physical disk.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment