Book Image

Learn Linux Quickly

By : Ahmed AlKabary
Book Image

Learn Linux Quickly

By: Ahmed AlKabary

Overview of this book

Linux is one of the most sought-after skills in the IT industry, with jobs involving Linux being increasingly in demand. Linux is by far the most popular operating system deployed in both public and private clouds; it is the processing power behind the majority of IoT and embedded devices. Do you use a mobile device that runs on Android? Even Android is a Linux distribution. This Linux book is a practical guide that lets you explore the power of the Linux command-line interface. Starting with the history of Linux, you'll quickly progress to the Linux filesystem hierarchy and learn a variety of basic Linux commands. You'll then understand how to make use of the extensive Linux documentation and help tools. The book shows you how to manage users and groups and takes you through the process of installing and managing software on Linux systems. As you advance, you'll discover how you can interact with Linux processes and troubleshoot network problems before learning the art of writing bash scripts and automating administrative tasks with Cron jobs. In addition to this, you'll get to create your own Linux commands and analyze various disk management techniques. By the end of this book, you'll have gained the Linux skills required to become an efficient Linux system administrator and be able to manage and work productively on Linux systems.
Table of Contents (24 chapters)

Navigating through the directory tree

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.

Figure 2: You are now at /home

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.

WHO IS ROOT?

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
Figure 3: Now you are at /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
Figure 4: Now you are at /etc
Figure 5: You want to go to /home/elliot

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.

THE 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.

ABSOLUTE VERSUS 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.

Figure 6: This Is a Relative Path

The following image shows you the absolute path /home/elliot/Desktop and will always work regardless of your current working directory.

Figure 7: This Is an Absolute Path

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.

Figure 8: Elliot's 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.

Figure 9: The contents of hello.txt