Once we know the current directory in which the commands are execute, How do we change it? Or more precisely, How can we reference files and folders in the interpreter?
Linux organizes files and folders in a tree-like structure. A file is always in a folder, and a folder may contain files and subfolders. The top level folder in this hierarchy in Linux has the name “/”. Beware of the inclination in this bar because the symbol “\” has a different meaning.
Every file or folder has an absolute
path which is the sequence of folders traversed from the root
separated by the symbol “/” and terminating with its own
name. For example, /dirA/dirB/dirC/file.txt
is the
absolute path for the file.txt
stored in folder
dirC
itself contained in the
dirB
folder, itself contained in the
dirA
folder which is stored at the root of the
system. The konsole program that opens the window with
the command interpreter is configured to show the absolute path of the
current directory on its title bar as shown in Figure 13.1.
A folder is assigned to any user in Linux to store all his/her files. The interpreter abbreviates the path to that directory by the symbol “~”. The ls command with no additional data lists the files in the current directory, but if given the name of a folder after the command name, it shows the files on that folder. Thus, the command ls ~ shows the files in the user folder.
Aside from the files and folders created by the user or programs, every Linux folder has two subfolders defined by default:
“..”: Is the folder in the previous level in the file hierarchy containing this one. This subfolder is also present in the root of the file system (the folder with absolute path “/”) but it points to itself.
“.”: It is the folder itself that appears as if it were also a subfolder. You may consider this as a “self reference” or pointer to itself.
These folders are regular folder in all contexts, and
therefore can be used in paths. For example, the path
/dirA/dirB/dirC/../file2.txt
refers to the file
stored in the dirB
folder because from
dirC
the folder ..
refers to its
predecessor, dirB
. The following figure
shows how this path is interpreted as
well as the effect of the “..” and “.”
folders.
As a consequence of the existence of the “.”
and “..” folders, a file may have several equivalent absolute
paths. For example, the following absolute paths all refer to the
file.txt
of Figure 13.2:
/dirA/dirB/dirC/../dirC/file.txt
,
/dirA/dirB/dirC/./././file.txt
,
/dirA/dirB/dirC/../../../dirA/dirB/dirC/./file.txt
,
etc. Use the ls to show on the screen the content of
the folders in the levels above your current directory up to the root
folder.
The absolute path unequivocally identifies any file or folder in the system, but it may be very large and therefore inconvenient to type as part of a command in the interpreter. The alternative is to use relative paths.
A “relative path” is a sequence of folder
names separated by the “/” symbol not starting by
“/” (otherwise it would be an absolute path) that is
interpreted using the current directory as prefix. Assuming that the
current directory in the interpreter is dirC
, the
following figure shows the interpretation of different
relative paths. Note that none of these paths starts with
“/”.