The basic way to explore the Linux file system is to use the cd, pwd and ls commands. 1
cd stands for change directory and will take you to the directory you specify as argument. 2
For example cd /var/log will take me to the log directory that resides in var which in turn resides 3
in the root directory. 4
If you get confused just run pwd, which stands for print working directory, and it will tell 5
you which directory you are in. 6
The current directory is also part of the prompt. 7
To see the contents of any directory run ls and the directory as argument. 8
If you run ls without any argument it will display the contents of the current working directory 9
This is the contents of /var/log. 10
To become proficient in working with the Linux file system, which, by the way, is extremely important 11
for any effective system administrator, you need to deeply understand what is a path and how to use 12
it. In my opinion 13
this is one of the most important concepts a beginner should learn. 14
Unfortunately, many times it brings a lot of confusion. 15
Take your time to exercise how to work with paths and do not go further until you master it. 16
Let's see what is a path! 17
A path is a unique location to a file or a directory in the file system of an operating system. In Linux 18
there are two types of path names: absolute and relative path names. 19
An absolute path is defined by specifying the location of a file or directly from the root directory, 20
which is /. 21
It's a complete path and always starts with /. 22
This is, for example, an absolute path because it starts with a forward slash. 23
To write an absolute path 24
start at the root directory and go deep into the filesystem, writing a slash after every directory 25
name. Linux uses forward slashes to separate directory names and does not use backslashes like Windows does. 26
Let's see some examples of absolute paths. 27
This is an absolute path, another one. 28
They are absolute because they start with a slash; use the TAB key for auto completion. 29
This will help you save a lot of time and also prevent you from making mistakes. 30
Or cd /home/student/Desktop written with capital D. 31
This is the desktop directory, it's similar to ~/desktop. 32
It's the same; remember that ~ 33
means the user's home directory. 34
so /home/student. 35
And by the way, in Linux, the desktop directory is called Desktop, written with a capital D and is 36
located in the user's home directory. 37
I'm creating a new folder on the desktop using the graphical interface and then displaying it in the 38
terminal. 39
It was created, the folder is on my desktop and I'm displaying it using the ls command. 40
It's there. 41
Let's move on and see another absolute path. 42
/var/log/boot.log I'm displaying the last lines of a file as root. 43
Let's go ahead and see what is the relative path. 44
A relative path is defined as the path related to the current working directly. 45
It begins at your current directory and never starts with a forward slash. 46
Before going on with the relative path, let's understand the special meanings of single dot and the 47
double dot used in a relative path. Simply put, a single dot (.) represents the directory you are in 48
and two successive dots or double dot (..) represents the parent directory. On a Linux system 49
these items are automatically created in every directory, and can be seen by using the ls command 50
with the -a option. -a means 51
show hidden files as well. 52
And any file or directories that starts with a dot is hidden; for example ls -la 53
I have displayed the contents of current working directory, my desktop, and we see there the 54
current directory, the single dot, the parent directory, double dot 55
or ~, this is T~, the user's home directory and folder1, the directory I've just created 56
ls- a ~. 57
I'm listing the contents of ~, the user's home directory and we see there are both the dot and double dot. 58
Using a single or double dot, we can build the relative path to any file or directory from the current 59
working directory. 60
Let's try some examples. 61
I'm moving to/etc directory. 62
I've used an absolute path. I'm printing the current working directory by running pwd 63
and it’s showing as being /etc If I want to see the contents of a file named hosts in the current 64
working directory, I'll simply run cat, or other command used for displaying files, and the name of 65
the file like cat/hosts. 66
This is the relative path that uses the current working directory as reference, it doesn't start with 67
a forward slash. 68
Another way to use a relative path to the same file would be cat ./hosts 69
It's the same! note that I've used a dot character to represent the current working directory, which 70
is the default 71
anyway. And the forward slash is the directory separator. 72
In other words, what I'm saying is print at console the contents of the hosts file which resides in the 73
current working directory and the single dot directory. 74
The same command using an absolute bath would be 75
cat /etc/hosts 76
Next, I want to see the contents of cron.daily directory using a relative path. And the command is: 77
ls cron.daily 78
The same command using an absolute bath would be: ls /etc/cron.daily 79
Now I want to display the logrotate file which resides in cron.daily using a relative path 80
I'm running: cat cron.daily 81
/ the directory separator and the name of the file logrotate 82
And the same command using an absolute path would be: 83
cat /etc/cron.daily/logrotate 84
This is an absolute path. 85
Let's see some more difficult examples. 86
I'm moving to my home directory using an absolute path. 87
I could have moved to the same directory by running cd without any argument or by running cd ~ 88
In Linux ~ represents the home directory of the current user. 89
Keep this in mind because it's important. From that directory 90
I want to list the contents of the /home directory using a relative path. 91
As I said earlier 2 successive dots or double dot represents the parent directory which is /home 92
so to see its content I run: ls .. 93
This is the contents of / home, the parent directory. 94
Now, if I want to see the contents of the root directory I run 95
ls ../.. 96
This is the contents of the root directory, of /. 97
I'm moving up 2 levels. 98
And a really complicated one: let's display the contents of logrotate, which results in 99
/etc/crone.daily using a relative path, so cat, this is the home directory, 100
This is the / directory. 101
../../etc/cron.daily and the file /logrotate 102
You notice how the relative path has changed, according to the current working directory; the absolute 103
path to that directory always remains the same. 104
This is the absolute path. Maybe you wonder if there's any difference between an absolute and a relative 105
path or when to use each of them. 106
My answer is that there is no difference; when you have to do something like remove a file or create 107
new directory 108
it doesn't matter if you are using an absolute or relative path. 109
It’s more like a matter of convenience. If you are already in /etc/cron.daily is more convenient 110
to refer to logrotate using a relative path than an absolute one. 111
It's easier to refer to the file this way, then this way. 112
On the other hand, an absolute path is more reliable than a relative one. I can not say: move to the cron.daily 113
directory by typing cd cron.daily. 114
This is a mistake. 115
This path is a correct one only if the current directory is / etc. If the user is in any other directory 116
that path will be wrong. 117
It's correct only if the user is in the /etc directory. 118
Another nice command you can use to get used to paths is tree. Tree is a recursive directory listing tool 119
that helps you see easier and clearer the parent and children directories . 120
Unfortunately, tree is not installed by default on Ubuntu. 121
Let's install it quickly. 122
sudo apt install tree 123
It was successfully installed. 124
If you run it with no arguments it will display the contents of the working directory recursively 125
showing sub-directories and files in tree-like format. 126
When a directory is given as argument, tree lists all the files and directories found in the given directory. 127
tree /etc 128
It's listing all the files and directories that reside in /etc; upon completion of listing all 129
files and directories tree returns 130
the total number of files and directories listed. Some useful options -d to print out only 131
the directories and -f to print out the absolute path for each file and directory. It's printing 132
only the directories. 133
Printing only the directories and an absolute path and it's printing both of the files and directories using 134
an absolute path. 135
At the end, I want to stress again the importance of relative and absolute paths. Practice as much as 136
possible and don't go ahead with the course without a very good understanding of how to use paths in 137
Linux.