2.3 Lesson 1
Introduction
Certificate: |
Linux Essentials |
---|---|
Version: |
1.6 |
Topic: |
2 Finding Your Way on a Linux System |
Objective: |
2.3 Using Directories and Listing Files |
Lesson: |
1 of 2 |
Files and Directories
The Linux filesystem is similar to other operating system’s filesystems in that it contains files and directories. Files contain data such as human-readable text, executable programs, or binary data that is used by the computer. Directories are used to create organization within the filesystem. Directories can contain files and other directories.
$ tree Documents ├── Mission-Statement.txt └── Reports └── report2018.txt 1 directory, 2 files
In this example, Documents
is a directory that contains one file (Mission-Statement.txt
) and one subdirectory (Reports
). The Reports
directory in turn contains one file called report2018.txt
. The Documents
directory is said to be the parent of the Reports
directory.
Tip
|
If the command |
File and Directory Names
File and directory names in Linux can contain lower case and upper case letters, numbers, spaces and special characters. However, since many special characters have a special meaning in the Linux shell, it is good practice to not use spaces or special characters when naming files or directories. Spaces, for example, need the escape character \
to be entered correctly:
$ cd Mission\ Statements
Also, refer to the filename report2018.txt
. Filenames can contain a suffix which comes after the period (.
). Unlike Windows, this suffix has no special meaning in Linux; it is there for human understanding. In our example .txt
indicates to us that this is a plaintext file, although it could technically contain any kind of data.
Navigating the Filesystem
Getting Current Location
Since Linux shells such as Bash are text-based, it is important to remember your current location when navigating the filesystem. The command prompt provides this information:
user@hostname ~/Documents/Reports $
Note that information such as user
and hostname
will be covered in future sections. From the prompt, we now know that our current location is in the Reports
directory. Similarly, the command pwd
will print working directory:
user@hostname ~/Documents/Reports $ pwd /home/user/Documents/Reports
The relationship of directories is represented with a forward slash (/
). We know that Reports
is a subdirectory of Documents
, which is a subdirectory of user
, which is located in a directory called home
. home
doesn’t seem to have a parent directory, but that is not true at all. The parent of home
is called root, and is represented by the first slash (/
). We will discuss the root directory in a later section.
Notice that the output of the pwd
command differs slightly from the path given on the command prompt. Instead of /home/user
, the command prompt contains a tilde (~
). The tilde is a special character that represents the user’s home directory. This will be covered in more detail in the next lesson.
Listing Directory Contents
The contents of the current directory are listed with the ls
command:
user@hostname ~/Documents/Reports $ ls report2018.txt
Note that ls
provides no information about the parent directory. Similarly, by default ls
does not show any information about contents of subdirectories. ls
can only “see” what is in the current directory.
Changing Current Directory
Navigation in Linux is primarily done with the cd
command. This changes directory. Using the pwd
command from before, we know our current directory is /home/user/Documents/Reports
. We can change our current directory by entering a new path:
user@hostname ~ $ cd /home/user/Documents user@hostname ~/Documents $ pwd /home/user/Documents user@hostname ~/Documents $ ls Mission-Statement.txt Reports
From our new location, we can “see” Mission-Statement.txt
and our subdirectory Reports
, but not the contents of our subdirectory. We can navigate back into Reports
like this:
user@hostname ~/Documents $ cd Reports user@hostname ~/Documents/Reports $ pwd /home/user/Documents/Reports user@hostname ~/Documents/Reports $ ls report2018.txt
We are now back where we started.
Absolute and Relative Paths
The pwd
command always prints an absolute path. This means that the path contains every step of the path, from the top of the filesystem (/
) to the bottom (Reports
). Absolute paths always begin with a /
.
/ └── home └── user └── Documents └── Reports
The absolute path contains all the information required to get to Reports
from anywhere in the filesystem. The drawback is that it is tedious to type.
The second example (cd Reports
) was much easier to type. This is an example of a relative path. Relative paths are shorter but only have meaning in relation to your current location. Consider this analogy: I am visiting you at your house. You tell me that your friend lives next door. I will understand that location because it is relative to my current location. But if you tell me this over the phone, I will not be able to find your friend’s house. You will need to give me the complete street address.
Special Relative Paths
The Linux shell gives us ways to shorten our paths when navigating. To reveal the first special paths, we enter the ls
command with the flag -a
. This flag modifies the ls
command so that all files and directories are listed, including hidden files and directories:
user@hostname ~/Documents/Reports $ ls -a . .. report2018.txt
Note
|
You can refer to the |
This command has revealed two additional results: These are special paths. They do not represent new files or directories, but rather they represent directories that you already know:
.
-
Indicates the current location (in this case,
Reports
). ..
-
Indicates the parent directory (in this case,
Documents
).
It is usually unnecessary to use the special relative path for the current location. It is easier and more understandable to type report2018.txt
than it is to type ./report2018.txt
. But the .
has uses that you will learn in future sections. For now, we will focus on the relative path for the parent directory:
user@hostname ~/Documents/Reports $ cd .. user@hostname ~/Documents $ pwd /home/user/Documents
The example of cd
is much easier when using ..
instead of the absolute path. Additionally, we can combine this pattern to navigate up the file tree very quickly.
user@hostname ~/Documents $ cd ../.. $ pwd /home
Guided Exercises
-
For each of the following paths, identify whether it is absolute or relative:
/home/user/Downloads
../Reports
/var
docs
/
-
Observe the following file structure. Note: Directories end with a slash (
/
) whentree
is invoked with the-F
option. You will need elevated privileges in order to run thetree
command on the root (/
) directory. The following is example output and is not indicative of a full directory structure. Use it to answer the following questions:$ sudo tree -F / / ├── etc/ │ ├── network/ │ │ └── interfaces │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d/ │ └── udev.conf └── home/ ├── lost+found/ └── user/ └── Documents/ 12 directories, 5 files
Use this structure to answer the following questions.
A user enters the following commands:
$ cd /etc/udev $ ls -a
What will be the output of the
ls -a
command? -
Enter the shortest possible command for each of the following:
-
Your current location is root (
/
). Enter the command to navigate tolost+found
within thehome
directory (example):$ cd home/lost+found
-
Your current location is root (
/
). Enter the command to navigate to the directory named/etc/network/
. -
Your current location is
/home/user/Documents/
. Navigate to the directory named/etc/
. -
Your current location is
/etc/systemd/system/
. Navigate to the directory named/home/user/
.
-
-
Consider the following commands:
$ pwd /etc/udev/rules.d $ cd ../../systemd/user $ cd .. $ pwd
What is the output of the final
pwd
command?
Explorational Exercises
-
Suppose a user has entered the following commands:
$ mkdir "this is a test" $ ls this is a test
What
cd
command would allow you to enter this directory? -
Try this again, but after typing in
cd this
, press the TAB key. What is now displayed on the prompt?This is an example of autocompletion, which is an invaluable tool not only for saving time, but for preventing spelling errors.
-
Try to create a directory whose name contains a
\
character. Display the directory’s name withls
and delete the directory.
Summary
In this lesson, you learned:
-
The fundamentals of the Linux filesystem
-
The difference between parent directories and subdirectories
-
The difference between absolute file paths and relative file paths
-
The special relative paths
.
and..
-
Navigate the filesystem using
cd
-
Show your current location using
pwd
-
List all files and directories using
ls -a
The following commands were discussed in this lesson:
cd
-
Change the current directory.
pwd
-
Print the current working directory’s path
ls
-
List the contents of a directory and display properties of files
mkdir
-
Create a new directory
tree
-
Display a hierarchical listing of a directory tree
Answers to Guided Exercises
-
For each of the following paths, identify whether it is absolute or relative:
/home/user/Downloads
absolute
../Reports
relative
/var
absolute
docs
relative
/
absolute
-
Observe the following file structure. Note: Directories end with a slash (
/
) whentree
is invoked with the-F
option. You will need elevated privileges in order to run thetree
command on the root (/
) directory. The following is example output and is not indicative of a full directory structure. Use it to answer the following questions:$ sudo tree -F / / ├── etc/ │ ├── network/ │ │ └── interfaces │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d/ │ └── udev.conf └── home/ ├── lost+found/ └── user/ └── Documents/ 12 directories, 5 files
A user enters the following commands:
$ cd /etc/udev $ ls -a
What will be the output of the
ls -a
command?. .. rules.d udev.conf
-
Enter the shortest possible command for each of the following:
-
Your current location is root (
/
). Enter the command to navigate tolost+found
within the home directory (example):$ cd home/lost+found
-
Your current location is root (
/
). Enter the command to navigate to the directory namednetwork
:$ cd etc/network
-
Your current location is
Documents
. Navigate to the directory namedetc
:$ cd /etc
-
Your current location is
system
. Navigate to the directory nameduser
:$ cd /home/user
-
-
Consider the following commands:
$ pwd /etc/udev/rules.d $ cd ../../systemd/user $ cd .. $ pwd
What is the output of the final
pwd
command?/etc/systemd
Answers to Explorational Exercises
-
Suppose a user has entered the following commands:
$ mkdir "this is a test" $ ls this is a test
What
cd
command would allow you to enter this directory?$ cd this\ is\ a\ test
-
Try this again, but after typing in
cd this
, press the TAB key. What is now displayed on the prompt?$ cd this\ is\ a\ test
This is an example of autocompletion, which is an invaluable tool not only for saving time, but for preventing spelling errors.
-
Try to create a directory whose name contains a
\
character. Display the directory’s name withls
and delete the directory.You can either escape the backslash using another backslash (
\\
) or use single or double quotes around the whole directory name:$ mkdir my\\dir $ ls 'my\dir' $ rmdir 'my\dir'