Debian 8 file-system: Relative and absolute paths

Introduction

Using this article I’d like to explain some basics around relative and absolute paths. Understanding paths is crucial when navigating the file-system or performing other file-system operations like copying or moving files. I’ll explain the difference between absolute and relative paths and provide some practical examples that you can use to practice with on your Debian 8 installation. Understanding the root directory structure can come in handy, if you’re unfamiliar with this you can read my previous article about the root directory structure here, it opens in a new tab.

Relative paths

To get to a file somewhere in your file-system you need something that’s called a path. A path is a notation of sequentially written folders with delimiters that you can use to navigate through the file-system. There are two typical types of a path, the absolute path and the relative path. It doesn’t really matter what type you use to get a certain job done, but if you’re working a lot with files on the command line sometimes people seem to favor one over the other, or even combine them. Lets start with relative paths because those are the most simple to start with and to explain as well. We’ll do a little experiment. Make sure you’ve opened up a terminal on your Linux system and type the following:

This lands you in your home directory. We’re going to create a couple of directories now, and traverse into them using cd a couple of times.

Within folder-1, we do the same again, but name them folder-2 so:

To accurately explain absolute and relative paths we’re going to do this once more:

Now, without knowing it, you’ve already been using the relative path scheme to get into folder-3 by systematically traversing down folder by folder. Let’s illustrate this a bit.

So, our absolute path now looks like this, remember with absolute paths we always start with /

There are several methods that can be used to get back to your home folder and one of them is using relative paths. We’ll change back one directory and then use pwd to check where we are in out files-system.

The absolute path will be returned to you and looks like this:

We can also chain folders, we’re going to do that now to get back into /home/username/

The output that’s returned now reads:

This is essentially how relative paths work. You traverse one directory at a time up or down the file-system tree, / being the most upper and whatever folder you’re in the most lower. You can chain adjacent folders as a longer relative path to traverse up or down multiple folders at a time.

Absolute paths

You’ve already seen examples of an Absolute path. The /home/username/folder-1/folder-2/folder-3 path is the absolute path to folder-3. I’ll illustrate absolute paths using the folders we’ve created previously during experimenting with relative paths. First, make sure you’re back in your home folder.

Now, say we need a file that’s somewhere between other files that are inside our folder-3 directory. We could first enter folder-1 then folder-2 and then folder-3 but if you’ve got to change folders a lot that could become annoying very fast. We already memorized the path to folder-3 so we could just as well use an absolute path to get there. It works like this:

This gets you directly into folder-3. You could also use an absolute path right back to your home folder like so:

The instructions are indeed longer, but you’ve got less instructions to deal with which may simplify file-system operations. This is how absolute paths works next to relative paths. It’s good to practice with this a bit because reading alone is just showing you the door, you walk through that door by actually typing the commands yourself and see what happens. Also, don’t be afraid to experiment a little, changing directories doesn’t hurt.

Some practical examples

There are several practical examples I’d like to show you. You will use paths a lot when you start to copy or move files around using the command line. Over the years I’ve been using the terminal to create backups and restore them, it’s fast, easy and stable.

As mentioned earlier there are a couple of methods to copy files. The first example i’m going to describe is copying files from a source to a destination when being inside the source directory. Let’s use our previously created folders again, folder-1, folder-2 and folder-3. First make sure you’re inside your home folder again.

Lets move into the directory folder-1 and create a file there. We will use this as our source file that we want to copy to the directory folder-3.

Now we write some content to that file.

Always verify the result

This should return a line that reads some content. We’re now ready to copy the file to folder-3 using relative paths.

Cp should return output because you’ve enabled the verbosity flag, it looks like this.

You’ve now copied the file from source to destination while your working directory is the source. There is nothing from stopping you to do this completely backwards, you can copy a file from source to destination while your working directory is the destination folder. In a previous article I’ve mentioned the ‘.’ and ‘..’ file where the ‘..’ means “go back one directory”, the ‘.’ file actually means ‘.’ “right here”. This is where we’re going to use this. First lets head into the folder-3 directory to delete the file we copied there, assuming your working directory is folder-1.

Now delete the file you’ve copied there.

Confirm deletion and hit enter. You should now have an empty folder-3. We can now copy the file again using a different source destination directory scheme.

Again, since you’ve enabled the verbosity flag you’ll see cp return some output. It looks like this.

The program cp always needs its first argument to be the source location and it’s second argument always needs to be the destination location. Notice how we employed the “..” and the “.” here. In the source we used the “..” two times to traverse up two directories to select some-file in folder-1. As a destination we simply used the “.” to state we want the destination to be “right here in this working directory”. Neat isn’t it?

There’s one more possible method to copy files. You can also copy files from a source location to a destination location while your working directory is completely arbitrary, this is where you use absolute paths when copying files. Assuming you’re still in the directory folder-3 you should first remove the file again:

Now let’s jump to a completely arbitrary location in the file-system.

It doesn’t really matter where you are in the file-system for our instruction to work. What we’re going to do is we’re going to use two absolute paths to copy some-file from folder-1 to folder-3. You obviously have to replace username within the paths with your systems username.

So there you have it, some experience with copying files using different methods. Just play around with this or create some backups that are long overdue.

Homework

Your homework assignment is to practice creating, moving and copying files around. Familiarize yourself with basic file-system navigating and learn to recognize when to use absolute or relative paths. As always thank you for reading and be sure to check out my other articles.

Leave a Reply