How to understand absolute and relative paths in Linux: Difference between revisions
Line 7: | Line 7: | ||
/home/user.name/my_data/my_test.dat | /home/user.name/my_data/my_test.dat | ||
This path is absolute, as it starts at the very top, | This path is absolute, as it starts at the very top, <code>/</code>, the '''root directory'''. | ||
Users on ARC do not have permissions to write to | Users on ARC do not have permissions to write to <code>/</code>, | ||
but they do have permissions to write to their own | but they do have permissions to write to their own <code>/home/user.name/</code> home directory, | ||
and everything below it. | |||
While it is '''very precise and clear''' | While it is '''very precise and clear''' when absolute paths are used, | ||
they '''tend to be long''' and also they really ''' | they '''tend to be long''' and also they really '''hard-wire the locations''' of the files | ||
to the specific directory and the directory cannot be moved easily to a different storage location without changing any references to it. | to the specific directory and the directory cannot be moved easily to a different storage location without changing any references to it. | ||
Line 25: | Line 26: | ||
if we change the command line to the directory <code>my_data</code>, then the relative path becomes simply | if we change the command line to the directory <code>my_data</code>, then the relative path becomes simply | ||
my_test.dat | my_test.dat | ||
There are two very commonly used relative paths that have to be mentioned, <code>.</code>, the dot path, means '''this directory''', | |||
and the <code>..</code>, the double dot path, which means '''the parent directory''', or '''the directory above'''. | |||
Thus, the <code>./my_test.dat</code> is fully equivalent to simply <code>my_test.dat</code>. | |||
= <code>rsync</code> and <code>scp</code> Remote Paths = | = <code>rsync</code> and <code>scp</code> Remote Paths = |
Revision as of 21:31, 7 March 2023
Absolute and Relative Paths
In Linux there are two ways to address a file, (1) using a relative path, and (2) using absolute path.
If you have a file in the subdirectory my_data
and the file name is my_test.dat
.
Then you can use the absolute path
/home/user.name/my_data/my_test.dat
This path is absolute, as it starts at the very top, /
, the root directory.
Users on ARC do not have permissions to write to /
,
but they do have permissions to write to their own /home/user.name/
home directory,
and everything below it.
While it is very precise and clear when absolute paths are used, they tend to be long and also they really hard-wire the locations of the files to the specific directory and the directory cannot be moved easily to a different storage location without changing any references to it.
The relative paths can be really a convenient alternative, therefore. They are always relative to some other path.
For example,
if a user just logged into his/her account the bash command line starts in the home directory,
which is /home/user.name
, therefore, the relative path to that file will be
my_data/my_test.dat
if we change the command line to the directory my_data
, then the relative path becomes simply
my_test.dat
There are two very commonly used relative paths that have to be mentioned, .
, the dot path, means this directory,
and the ..
, the double dot path, which means the parent directory, or the directory above.
Thus, the ./my_test.dat
is fully equivalent to simply my_test.dat
.
rsync
and scp
Remote Paths
When we transfer files using rsync
or scp
commands,
we provide the account details as user.name@arc.ucalgary.ca
,
which means, the user user.name
on the computer arc.ucalgary.ca
.
Then the path follows after the ":
" separator,
but it always starts at the user's home location, therefore the path
user.name@arc.ucalgary.ca:
simply means user's home directory. Clearly, the same directory can also be pointed as
user.name@arc.ucalgary.ca:/home/user.name/
but it is longer, contains redundant information (the name is given twice), and it means exactly the same thing.
Naturally, the file my_test.dat
, could be referenced, when using the relative path as
user.name@arc.ucalgary.ca:my_data/my_test.dat
and using the absolute path as
user.name@arc.ucalgary.ca:/ home/user.name/my_data/my_test.dat