Relative and Absolute Paths Through the File System

Published on September 16, 2021 at 6:31 am by LEW

Introduction

Having used the terms relative and absolute on occasion when referring to file paths and locations, I thought I should take some time to explain what these terms mean. This will be specifically in regard to a Linux file system, but may be generally relevant to other Operating Systems.

File/Directory paths

When discussing a path to a file or directory (in Linux), a forward slash delineates the path through the file system. In a Linux file system the top level is represented by the forward slash or “/”. All directories reside under the forward slash. For example, the user home directories are kept in the top level home folder which would be written as “/home”. The home directory for user 1 wound then be represented as “/home/user1”, and user 2 would be “/home/user2”.

/
|_ home
|  |_ user1
|  |  |_ Documents
|  |  |  |_ Story.odt
|  |  |_ Pictures
|  |_ user2
|  |  |_Pictures
|  |  | |_ Flower.jpg

Note, for a generic overview of the Linux file system please refer to Linux File System Hierarchy Standard.

Note, on a Windows system a path would look more like “C:\User\User1”. Note the use of a hard drive letter, and the backward slash. In Linux hard drives are attached to a directory in the file tree, not called out separately.

Absolute path

An absolute path to a file is the full path, starting at the top of the file tree. In the above example, the absolute path to the Story.odt file is “/home/user1/Documents/Story.odt”.

When performing command line functions, absolute paths will always work (assuming you have permissions for the file which is another subject for another post). The downside to absolute paths is how long they can be. For example on a standard Debian system, if you need the gvim.svg icon, the absolute path from the top of the file tree would be “/usr/share/icons/hicolor/scalable/apps/gvim.svg”. That is a drill down though six directories to finally get to the wanted file.

Relative paths

A relative path is the file path between two locations in the file system. Linux uses two special directory types for this. If you do a ls -a, you will see two hidden directories that appear at the top of the file tree. These are labeled “.” and “..”. The first is representative of your current working directory, and can be used for things like running code from your current directory. The second one represents the parent directory of your current working directory.

If you are in the user1 directory from above, and you want to list the contents of the user2 directory using an absolute path, then you would enter “ls /home/user1”. However since both user1 and user2 have the same parent directory, /home, you could use the relative path “ls ../user2”.

Lets say you are working in the /home/user1/Documents directory on the file Story.odt, and you want to grab a copy of Flower.jpg from user2. To grab a copy using relative paths you would enter either absolute or relative  paths. Note that file/directory names in Linux are case sensitive.

cp /home/user2/Pictures/Flower.jpg /home/user1/Documents/Flower.jpg

or

cp ../user2/Pictures/Flower.jpg ./

The “..” brings us up one level to the home directory, from which we enter user2 and select the directory path and file to copy. Then for the destination we use ./which means our current working directory, and because of the way cp works, it copies the file with the same name to that directory. Note we could have provided a different name for the file if we wanted.

Conclusion

Both absolute and relative paths can technically be used interchangeably. The situation will determine which is easier to type (shorter). This brief outline should provide you with enough information to use both as needed. It is up to you to determine which one will be more efficient for any given situation.

Add New Comment

Your email address will not be published. Required fields are marked *