Alright, let's do more climbing. For example, let's climb to the /home directory to see how many users we have on the system. You can do that by simply running the cd /home command:
elliot@ubuntu-linux:~$ cd /home
elliot@ubuntu-linux:/home$
Notice how your command prompt changes as it's now showing that you are at the home directory.
Now let's run ls to view the contents of the /home directory:
elliot@ubuntu-linux:/home$ ls
angela elliot
These are the two users on my system (besides the root user). The /root is the home directory for the root user. You probably have only one user in /home; you will learn later in the book how to add other users to your system.
The root user is a superuser who is allowed to do anything on the system. The root user can install software, add users, manage disk partitions, etc. The home directory of the root user is /root, which is NOT to be confused with / (the root of the filesystem).
If you want proof that you are currently at the /home directory, you can run the pwd command:
elliot@ubuntu-linux:/home$ pwd
/home
Sure enough! We are at the /home directory. Now let's climb to the home directory of user elliot. Now, believe it or not, there are two ways to navigate to elliot's home directory. You can simply run the cd elliot command:
elliot@ubuntu-linux:/home$ cd elliot
elliot@ubuntu-linux:~$ pwd
/home/elliot
Or you can run the cd /home/elliot command:
elliot@ubuntu-linux:/home$ cd /home/elliot
elliot@ubuntu-linux:~$ pwd
/home/elliot
Notice that both commands have landed us in elliot's home directory. However, running cd elliot is much easier than running cd /home/elliot, of course.
Well, think about it, we were initially at the /home directory, and that's why we were able to run cd elliot to land in /home/elliot.
However, in other situations, we would be forced to use the full path (absolute path) /home/elliot to reach our destination. To demonstrate, let's first change to the /etc directory:
elliot@ubuntu-linux:~$ cd /etc
elliot@ubuntu-linux:/etc$ pwd
/etc
Figures 4 and 5 help you visualize it. You are at /etc and you want to go to /home/elliot. To get to elliot's home directory, we can no longer use a short path (relative path) by running the cd elliot command:
elliot@ubuntu-linux:/etc$ cd elliot
bash: cd: elliot: No such file or directory
As you can see, the Shell got mad and returned an error bash: cd: elliot: No such file or directory. In this case, we have to use the full path (absolute path)/home/elliot:
elliot@ubuntu-linux:/etc$ cd /home/elliot
elliot@ubuntu-linux:~$ pwd
/home/elliot
In case you haven't noticed by now, we have been using the forward slash (/) as a directory separator.
In Linux, the forward slash (/) is the directory separator or sometimes referred to as the path separator. In Windows, it's the other way around because a backward slash (\) is used instead as a directory separator. However, be careful since the leading forward slash is the root of our filesystem. For example, in /home/elliot/Desktop, only the second and third forward slashes are directory separators, but the first forward slash represents the root of the filesystem.
It's crucial to realize the difference between absolute paths and relative paths.
An absolute path of a file is simply the full path of that file and, it ALWAYS begins with a leading forward slash. For example, /opt/- google/chrome is an example of an absolute path.
On the other hand, a relative path of a file never starts with the root directory and is always relative to the current working directory. For example, if you are currently at /var, then log/boot.log is a valid relative path.
As a rule of thumb, if you want to distinguish between a relative path and an absolute path, look and see if the path starts with the root directory (forward slash); if it does, then you can conclude the path is absolute, otherwise, the path is relative.
The following diagram shows you the relative path Desktop/hello.txt and will only work if your current working directory is /home/elliot.
The following image shows you the absolute path /home/elliot/Desktop and will always work regardless of your current working directory.
Now let's climb to Elliot's Desktop directory to see what he has there. We will use an absolute path:
elliot@ubuntu-linux:/$ cd /home/elliot/Desktop
elliot@ubuntu-linux:~/Desktop$ pwd
/home/elliot/Desktop
We follow it with a pwd to confirm that we are indeed in the desired directory. Now let's run ls to view the contents of Elliot's desktop:
elliot@ubuntu-linux:~/Desktop$ ls
hello.txt
Notice that the file hello.txt is on Elliot's desktop, so we can actually see it right there on the desktop.
As you can see in the preceding image, there is a file named hello.txt on Elliot's desktop. You can use the cat command to view the contents of a text file:
elliot@ubuntu-linux:~/Desktop$ cat hello.txt
Hello Friend!
Are you from fsociety?
If you open the file hello.txt on the desktop, you will see the same contents, of course, as you can see in the following screenshot.