---===[ Message from the authors: This wiki might be taken down due to maintenance ]===----

Term:Absolute and relative Path

From Juneday education
Jump to: navigation, search


Imagine that you have a cave (or a map of a cave) which looks like this:

                  "/" <-- the first, top level, room is called "/" - 
                 /   \    slash, named after a guitar player
                1     2
               / \    |\
              3   4   5 6
                 / \     \
                7   8     9

You can start in room "/" and you would see signs to the next rooms, 1 and 2. From room 2 you see two signs leading "down" to 5 and 6 and a sign leading "up" to "/" again saying


Why .. - because this is the sign meaning "up". In this kind of cave, there is only one room "above" every room, so we'll use the short-hand .. to always signify "the only room above current room" (the top room / is a special case, you can't actually go up from there - you'll remain in the same room if you try to go up).

Every room has signs to the rooms below and the .. sign to the room above.

Rules for the example map

To describe the way, or a route, we use this syntax and these rules:

  • There are two kinds of signs in each room:
    • Going down: the room number for the next room down
    • Going up: ..
      • If you insist on going up from "/" you will actually stay in the same room
  • Going more than one room - use the rules above and add a slash between each sign direction
  • You can describe the route to any room using room "/" as the starting point, by starting with an empty /
    • Only routes starting from room "/" is allowed to use the / as the first character
    • Using the / as the first character could be thought of as "start all the way from the top and go only down"
  • Only using the signs from a particular room, is called a relative path, and such paths never start with "/"
  • Using the / as the first character in a path to describe a route means "start all the way from the top" and is called an absolute path
  • You cannot go up from room "/", only down
  • "/" is the only room which has a funny name, and which is allowed to in its name use the same character as the character to put between rooms in a route, which is exactly that, "/"
  • The path from a room to itself is called . (a single dot)


You can now give directions to some one for how to get to any room in the cave.

  • To go from room "/" (down) to room 9, the directions would be: 2/6/9 - you are only using the signs here - so this is a relative path from "/" to "9" - You don't need to include the current room in a relative path.
  • To go from room 9 to room 5 requires two ups and one down: ../../5 (go up (to room 6), then up (to room 2) and (down) to room 5, using only the signs in each room) - also a relative path
  • Going from room 8 to room 9 using the special syntax for "start all the way from the top": /2/6/9 - which is an absolute path
  • Going from room 7 to room 9 using the special syntax for "start all the way from the top": /2/6/9 - which is an absolute path
  • Going from room 6 to room 9 using the special syntax for "start all the way from the top": /2/6/9 - which is an absolute path
  • Going from room "x" (any frakkin' room!) to room 9 using the special syntax for "start all the way from the top": /2/6/9 - which is an absolute path
  • Going from room 8 to room 9 using the signs from room 8: ../../../2/6/9 - a relative path

As you see, once again, absolute paths always start with / since they describe the route all the way from (and including) the root room called /. You might also have noticed that absolute paths to a specific room are always the same!

And as you also see, relative paths start with either .. or the name of a room directly below the current room. It is actually also possible to include the special relative path to the same room you are in, as the first part of a relative path, . as in the following alternative to a relative path from room 8 to room 9:

./../../../2/6/9 ("stay here and then", "up", "up", "up", 2, 6, 9)

Or the relative path from 2 to 3:


This is to say, a relative path can always start with ./

Terminology - absolute and relative paths

Using the signs is what we call a relative path. It means, "navigate from here by reading the signs in each room until you see the sign with the room you want to go to".

Using the start over from the top, strategy with the / for the first character is what we call an absolute path. Absolute paths are always the same to any room. The absolute path to:

  • 9 is always /2/6/9
  • 8 is always /1/4/8
  • 7 is always /1/4/7
  • 5 is always /2/5
  • "/" is always simlpy /
  • etc

The absolute path is always the same, because there is only one shortest path to any room from "/".

We can also call "/" the "root" room. From the root stem all the paths to all the rooms.

File system paths work like the example

Now, if we look at the file system of a *nix operating system (Unix, or GNU/Linux for instance) we'll see that it is organized just like the map of rooms above. The top directory in the file system is called the "root directory" or "/" for short. And to navigate down from the root directory, you just say the name of the directory directly below it, or if it is far away, the directory below it leading to the destination, followed by a slash until you have described the path.

Just as with the map above, in a file system, we also use the terms absolute path and relative path, and in the same ways as described above.

On a typical GNU/Linux installation, you will have your user's files in your "home directory", which typically has the absolute path /home/{user name} - for instance, the user rikard would have the home directory at a location described by the absolute path:


In the home directory, there's typically a directory called Documents (or similar). The absolute path for rikard's Documents would then be:


There is also typically a directory called Desktop in the home directory. For rikard, it would be located using the following absolute path:


Now, the relative path from rikard's Documents folder to rikard's Desktop folder would be:


This is because Desktop and Documents are siblings (they share the same parent directory /home/rikard ).

Example uses of relative and absolute paths for moving a file

Relative paths

For this reason, we can use a relative path as well as an absolute path for, let's say, moving a file from the Desktop to the Documents folder. Let's say we have the file move-me.txt at the location /home/rikard/Desktop and we are using a terminal and we happen to be in that same folder, /home/rikard/Desktop. We'll use the command mv to move the file to its new location:

$ mv move-me.txt ../Documents/

Notice the trailing slash after Documents - this is not mandatory but it is safe to always put it there - to make sure that the destination really is a directory and not a file (which otherwise would be overwritten by the move-me.txt).

Absolute paths

But could we then use an absolute path for moving the file (assuming it is till there, in Desktop)? Yes, as we said above, you can always use an absolute path to point to any place in the file system - and by that we mean that an absolute path can be used to describe any shortest (direct) path from the root to a directory somewhere. The syntax would then be (provided we still are in the Desktop directory) for the destination folder as an absolute path:

$ mv move-me.txt /home/rikard/Documents/

But why not use an absolute path for both the source file and the destination folder?

$ mv /home/rikard/Desktop/move-me.txt /home/rikard/Documents/

Is there any difference between absolute and relative paths?

What is the difference, then?

The difference is that relative paths (for either the source file or the destination folder) only work from places for which the path is relative. Saying ../Documents only describes /home/rikard/Documents from directories which are siblings to /home/rikard/Documents (like, for instance, /home/rikard/Desktop</code> ).

But using absolute paths for both the file to be moved and for the destination directory, will work from any directory in the whole file system! (If you have the permission to move the file because of ownership is a totally different question, however...)

Current directory expressed as a relative path

What about current directory, then?

In modern shells and environments, you can always include "." ( a dot ) as a symbol meaning "current directory". For instance, if we were in the dirctory /home/rikard/Documents/ - that is, having that directory as the "current directory", we could move the file move-me.txt from the Desktop to the current directory using either:

$ mv /home/rikard/Desktop/move-me.txt .


$ mv ../Desktop/move-me.txt .

You may read the dot out loud as "here": move ../Desktop/move-me.txt here

Using "." as an alias for the current directory is always a relative path. It is in fact legal (and some times handy) to include the dot as the first part of any relative path - since any relative path starts in the current directory!

If I have /home/rikard/Desktop as the current directory - that is, I'm using a terminal and I have used cd to navigate to that directory, then I could move the file move-me.txt to the Documents directory in rikard's home like this:

$ mv ./move-me.txt ../Documents/


$ mv ./move-me.txt /home/rikard/Documents/

Sometimes you need to include the current directory in a path

When running executable files, it is sometimes necessary to include the dot before an executable command even if we are in the same directory. This is because most sensible operating systems and shells only allow the execution of unqualified executable file names in some directories specified in an environment variable called PATH . If we create an executable - like a script run-me.sh, for instance, and that script is in our current directory, and our current directory isn't in our PATH, then we'd have to say:

$ ./run-me.sh

This is because sensible shells only accept commands whose executables are in one of the directories listed in the PATH variable.

You need to understand paths when programming

If you program in Java, you'll also have to know what absolute and relative paths are, because if you are e.g. compiling a source code file in Java, and that source code file uses a "Java class" in some other directory, the compiler must know how to find also that "Java class" or the corresponding source code file for it. This is handled via a flag to the javac compiler which provides a list of files for the so called "class path" where the compiler should look for "classes" used in the file it is currently compiling.


Absolute path

Absolute path: The canonical, exact and unique path to a file or folder from the root directory "/". Example:


The above absolute path works from any folder in the filesystem in order to point out that file.

Relative path

Relative path: The path from some folder or some folders to a file and folder. Example:


The above relative path only works from directories which are siblings to /home/rikard/programming/basics/ .