Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Linux Quickly

You're reading from   Learn Linux Quickly A beginner-friendly guide to getting up and running with the world's most powerful operating system

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781800566002
Length 338 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Ahmed AlKabary Ahmed AlKabary
Author Profile Icon Ahmed AlKabary
Ahmed AlKabary
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Your First Keystrokes 2. Climbing the Tree FREE CHAPTER 3. Meet the Editors 4. Copying, Moving, and Deleting Files 5. Read Your Manuals! 6. Hard versus Soft Links 7. Who Is Root? 8. Controlling the Population 9. Piping and I/O Redirection 10. Analyzing and Manipulating Files 11. Let's Play Find and Seek 12. You Got a Package 13. Kill the Process 14. The Power of Sudo 15. What's Wrong with the Network? 16. Bash Scripting Is Fun 17. You Need a Cron Job 18. Archiving and Compressing Files 19. Create Your Own Commands 20. Everyone Needs Disk Space 21. echo "Goodbye My Friend" 22. Assessments 23. Other Books You May Enjoy

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
You have been reading a chapter from
Learn Linux Quickly
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781800566002
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image