One of the most basic concepts that we should know when we use an operating system are the so-called routes.
With the paths we can indicate the exact location of a file or folder
, following a directory structure or directory
tree
. There are different types of routes, but today we are going to dedicate an article to talk about
relative and absolute routes
, at the same time we will point out their differences and characteristics.
Virtually all operating systems have a file system in which we can use the paths to locate a file or folder. In many cases the word "path" is used as a synonym for the route and in some cases it is simply called a directory.
In this tutorial we are going to focus on conventional operating systems, that is, Windows and Linux, where there are small differences when it comes to marking the routes, but that we will see as we go.
What are the routes for?
The routes serve to locate a resource.
Its use is standardized
and we can find resources located with their corresponding routes in:
web pages, mobile applications, desktop software, network protocols, cloud storage systems, etc.
Dissecting a path to locate a file on Windows and Linux.
Next we will present two examples of routes to examine and understand all the elements that compose it. An example will be of a route in a Linux operating system and the other will be a route of the Windows operating system
(also valid in MS-DOS)
.
Sample path in Windows: C: \ Windows \ System32
In this route we must differentiate the following basic elements:
-
Separator
: the separator is a delimiter character that serves to separate the different elements that make up the route. In the case of Windows, the separator is the backslash character
\
.
-
Drive
: consists of a letter with which you can identify a storage unit or disk
(physical or virtual)
. The drive letter is always followed by a colon followed by the delimiter character.
The sample path points to a folder, but the paths can also point to files. The following example path would point to the Windows Notepad executable:
C:\Windows\System32\notepad.exe
Sample path in Linux: / home / zeokat
In the case of Linux, the first thing we should observe is that the separator is the slash character
/
. In addition, we see that the drive letters do not exist and this is because in Linux another nomenclature is used to identify the disks.
Following the paths in Linux, these can also point to files, for example the following
/home/zeokat/TechnoWikis.txt
points to the text file
TechnoWikis.txt
.
Relative and absolute routes, characteristic differences.
So far, in the examples we have used we have only used absolute paths, but in both Windows and Linux there is the possibility of working with relative paths. In this section we will see the differences between both types of routes and some advantages and disadvantages that their use entails.
To get an idea of "‹"‹the differences that exist between a relative and an absolute route, just keep in mind that
the complete route of the resource is represented
in
an absolute route
. In other words,
it starts from the root directory until it reaches the resource
.
In contrast,
only a part of the route is represented on relative routes
. This is possible because
the current working directory is taken into account in relative paths
.
Let's imagine that we are in Linux and that our current working directory is
/home/zeokat
, if we want to access the file located in
/home/zeokat/TechnoWikis/file.txt
we can do it in two ways:
-
Using the absolute path:
/home/zeokat/TechnoWikis/file.txt
-
Using the relative path:
TechnoWikis/file.txt
We see that relative routes do not have a separator at the beginning of the route.
Personal directory path aliases on Unix systems.
On Unix operating systems there is the relative path
~/
that points directly to the path of the user's personal folder. For example, if our user's personal folder is
/home/TechnoWikis
, we can refer this route with the alias
~/
.
To verify that what has been said before is true, just execute the commands
ls /home/zeokat
and
ls ~/
to verify that the output of the
ls
command is exactly the same.
User directory aliases in Windows.
In Windows there is also an environment variable that defines the path of the user folder, it is
%HOMEPATH%
. Imagine that our user's folder is
c:\users\TechnoWikis
, so we can execute the commands
dir c:\users\TechnoWikis
and
dir %HOMEPATH%
to verify that the same path is referenced.
Advantages and disadvantages of relative and absolute routes.
The reality is that there are no advantages or disadvantages associated with the use of relative or absolute routes.
To be more exact, it is about choosing in each case the type of route that suits us best.
For example, if we have a program that makes use of a configuration file, the best option is to refer to this file using a relative path. In this way, we can change the program folder and we will not lose access to the configuration file.
Absolute paths are very useful when we point to operating system files or specific libraries. Many operating systems have a
standardized directory tree
, so we can use absolute paths to locate specific resources.