2.3 Lesson 2
Certificate: |
Linux Essentials |
---|---|
Version: |
1.6 |
Topic: |
2 Finding Your Way on a Linux System |
Objective: |
2.3 Using Directories and Listing Files |
Lesson: |
2 of 2 |
Introduction
The Unix operating system was originally designed for mainframe computers in the mid-1960s. These computers were shared among many users, who accessed the system’s resources through terminals. These fundamental ideas carry through to Linux systems today. We still talk about using “terminals” to enter commands in the shell, and every Linux system is organized in such a way that it is easy to create many users on a single system.
Home Directories
This is an example of a normal file system in Linux:
$ tree -L 1 / / ├── bin ├── boot ├── cdrom ├── dev ├── etc ├── home ├── lib ├── mnt ├── opt ├── proc ├── root ├── run ├── sbin ├── srv ├── sys ├── tmp ├── usr └── var
Most of these directories are consistent across all Linux systems. From servers to supercomputers to tiny embedded systems, a seasoned Linux user can be confident that they can find the ls
command inside /bin
, can change the system configuration by modifying files in /etc
, and read system logs in /var
. The standard location of these files and directories is defined by the Filesystem Hierarchy Standard (FHS), which will be discussed in a later lesson. You will learn more about the contents of these directories as you continue learning about Linux, but for the time being, know that:
-
changes that you make in the root filesystem will affect all users, and
-
changing files in the root filesystem will require administrator permissions.
This means that normal users will be prohibited from modifying these files, and may also be prohibited from even reading these files. We will cover the topic of permissions in a later section.
Now, we will focus on the directory /home
, which should be somewhat familiar at this point:
$ tree -L 1 /home /home ├── user ├── michael └── lara
Our example system has three normal users, and each of our users has their own dedicated location, where they can create and modify files and directories without affecting their neighbor. For example, in the previous lesson we were working with the following file structure:
$ tree /home/user user └── Documents ├── Mission-Statement └── Reports └── report2018.txt
In actuality, the real filesystem may look like this:
$ tree /home /home ├── user │ └── Documents │ ├── Mission-Statement │ └── Reports │ └── report2018.txt ├── michael │ ├── Documents │ │ └── presentation-for-clients.odp │ └── Music
…and so on for lara
.
In Linux, /home
is similar to an apartment building. Many users may have their space here, separated into dedicated apartments. The utilities and maintenance of the building itself are the responsibility of the superintendent root user.
The Special Relative Path for Home
When you start a new terminal session in Linux, you see a command prompt similar to this:
user@hostname ~ $
The tilde (~
) here represents our home directory. If you run the ls
command, you will see some familiar output:
$ cd ~ $ ls Documents
Compare that with the file system above to check your understanding.
Consider now what we know about Linux: it is similar to an apartment building, with many users residing in /home
. So user
's home will be different from user michael
's home. To demonstrate this, we will use the su
command to switch user.
user@hostname ~ $ pwd /home/user user@hostname ~ $ su - michael Password: michael@hostname ~ $ pwd /home/michael
The meaning of ~
changes depending of who the user is. For michael
, the
absolute path of ~
is /home/michael
. For lara
, the absolute path of ~
is
/home/lara
, and so on.
Relative-to-Home File Paths
Using ~
for commands is very convenient, provided that you don’t switch users. We will consider the following example for user
, who has begun a new session:
$ ls Documents $ cd Documents $ ls Mission-Statement Reports $ cd Reports $ ls report2018.txt $ cd ~ $ ls Documents
Note that users will always begin a new session in their home directory. In this example, user
has traveled into their Documents/Reports
subdirectory, and with the cd ~
command they have returned to where they started. You can perform the same action by using the cd
command with no arguments:
$ cd Documents/Reports $ pwd /home/user/Documents/Reports $ cd $ pwd /home/user
One last thing to note: we can specify the home directories of other users by specifying the username after the tilde. For example:
$ ls ~michael Documents Music
Note that this will only work if michael
has given us permission to view the contents of his home directory.
Let’s consider a situation where michael
would like to view the file report2018.txt
in user
's home directory. Assuming that michael
has the permission to do so, he can use the less
command.
$ less ~user/Documents/Reports/report2018.txt
Any file path that contains the ~
character is called a relative-to-home path.
Hidden Files and Directories
In the previous lesson, we introduced the option -a
for the ls
command. We used ls -a
to introduce the two special relative paths: .
and ..
. The -a
option will list all files and directories, including hidden files and directories.
$ ls -a ~ . .. .bash_history .bash_logout .bash-profile .bashrc Documents
Hidden files and directories will always begin with a period (.
). By default, a user’s home directory will include many hidden files. These are often used to set user-specific configuration settings, and should only be modified by an experienced user.
The Long List Option
The ls
command has many options to change its behavior. Let’s look at one of the most common options:
$ ls -l -rw-r--r-- 1 user staff 3606 Jan 13 2017 report2018.txt
-l
creates a long list. Files and directories will each occupy one line, but additional information about each file and directory will be displayed.
-rw-r—r--
-
Type of file and permissions of the file. Note that a regular file will begin with dash, and a directory will start with
d
. 1
-
Number of links to the file.
user staff
-
Specifies ownership of the file.
user
is the owner of the file, and the file is also associated with thestaff
group. 3606
-
Size of the file in bytes.
Jan 13 2017
-
Time stamp of the last modification to the file.
report2018.txt
-
Name of the file.
Subjects such as ownership, permissions and links will be covered in future
sections. As you can see, the long list version of the ls
is oftentimes
preferable to the default.
Additional ls Options
Below are some of the ways that we most commonly use the ls
command. As you can see, the user can combine many options together to get the desired output.
ls -lh
-
Combining long list with human readable file sizes will give us useful suffixes such as
M
for megabytes orK
for kilobytes. ls -d */
-
The
-d
option will list directories but not their contents. Combining this with*/
will show only subdirectories and no files. ls -lt
-
Combines long list with the option to sort by modification time. The files with the most recent changes will be at the top, and files with the oldest changes will be at the bottom. But this order can be reversed with:
ls -lrt
-
Combines long list with sort by (modification) time, combined with
-r
which reverses the sort. Now files with the most recent changes are at the bottom of the list. In addition to sorting by modification time, files can also be sorted by access time or by status time. ls -lX
-
Combines long list with the option to sort by file eXtension. This will, for example, group all files ending with
.txt
together, all files ending with.jpg
together, and so on. ls -S
-
The
-S
sorts by file size, much in the same way as-t
and-X
sort by time and extension respectively. The largest files will come first, and smallest last. Note that the contents of subdirectories are not included in the sort. ls -R
-
The
-R
option will modify thels
command to display a recursive list. What does this mean?
Recursion in Bash
Recursion refers to a situation when “something is defined in terms of itself”. Recursion is a very important concept in computer science, but here its meaning is far simpler. Let’s consider our example from before:
$ ls ~ Documents
We know from before that user
has a home directory, and in this directory there is one subdirectory. ls
has up until now only shown us the files and subdirectories of a location, but cannot tell us the contents of these subdirectories. In these lessons, we have been using the tree
command when we wanted to display the contents of many directories. Unfortunately, tree
is not one of the core utilities of Linux and thus is not always available. Compare the output of tree
with the output of ls -R
in the following examples:
$ tree /home/user user └── Documents ├── Mission-Statement └── Reports └── report2018.txt $ ls -R ~ /home/user/: Documents /home/user/Documents: Mission-Statement Reports /home/user/Documents/Reports: report2018.txt
As you can see, with the recursive option, we get a far longer list of files. In fact, it is as if we ran the ls
command in user
's home directory, and encountered one subdirectory. Then, we entered into that subdirectory and ran the ls
command again. We encountered the file Mission-Statement
and another subdirectory called Reports
. And again, we entered into the subdirectory, and ran the ls
command again. Essentially, running ls -R
is like telling Bash: “Run ls
here, and repeat the command in every subdirectory that you find.”
Recursion is particularly important in file modification commands such as copying or removing directories. For example, if you wanted to copy the Documents
subdirectory, you would need to specify a recursive copy in order to extend this command to all subdirectories.
Guided Exercises
-
Use the following file structure to answer the following three questions:
/ ├── etc/ │ ├── network/ │ │ └── interfaces/ │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d │ └── udev.conf └── home/ ├── lost+found/ ├── user/ │ └── Documents/ └── michael/ └── Music/
-
What command will navigate into the
network
directory regardless of your current location? -
What command can
user
enter to navigate into theirDocuments
directory from/etc/udev
? Use the shortest possible path. -
What command can
user
enter to navigate intomichael
'sMusic
directory? Use the shortest possible path.
-
-
Consider the following output of
ls -lh
to answer the next two questions. Note that directories are indicated with ad
at the beginning of the line.drwxrwxrwx 5 eric eric 4.0K Apr 26 2011 China/ -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0066.jpg -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0067.jpg -rwxrwxrwx 1 eric eric 1.6M Jul 18 2011 img_0074.jpg -rwxrwxrwx 1 eric eric 1.8M Jul 18 2011 img_0075.jpg -rwxrwxrwx 1 eric eric 46K Jul 18 2011 scary.jpg -rwxrwxrwx 1 eric eric 469K Jan 29 2018 Screenshot from 2017-08-13 21-22-24.png -rwxrwxrwx 1 eric eric 498K Jan 29 2018 Screenshot from 2017-08-14 21-18-07.png -rwxrwxrwx 1 eric eric 211K Jan 29 2018 Screenshot from 2018-01-06 23-29-30.png -rwxrwxrwx 1 eric eric 150K Jul 18 2011 tobermory.jpg drwxrwxrwx 6 eric eric 4.0K Apr 26 2011 Tokyo/ -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 081.jpg -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 085.jpg -rwxrwxrwx 1 eric eric 944K Jul 18 2011 Toronto 152.jpg -rwxrwxrwx 1 eric eric 728K Jul 18 2011 Toronto 173.jpg drwxrwxrwx 2 eric eric 4.0K Jun 5 2016 Wallpapers/
-
When you run the command
ls -lrS
, what file will be at the beginning? -
Please describe what you expect to see as the output for
ls -ad */
.
-
Explorational Exercises
-
Run the
ls -lh
command in a directory that contains subdirectories. Note the listed size of these directories. Do these file sizes seem correct to you? Do they accurately represent the contents of all files inside that directory? -
Here is a new command to try:
du -h
. Run this command and describe the output that it gives you. -
On many Linux systems, you can type in
ll
and get the same output as you would if you typedls -l
. Please note however thatll
is not a command. For example,man ll
will give you the message that no manual entry exists for it. This is an example of an alias. Why might aliases be useful to a user?
Summary
In this lab, you learned:
-
that each Linux user will have a home directory,
-
the current user’s home directory can be reached by using
~
, -
any file path that uses
~
is called a relative-to-home path.
You also learned about some of the most common ways of modifying the `ls command.
-a
(all)-
prints all files/directories, including hidden
-d
(directories)-
list directories, not their contents
-h
(human readable)-
prints file sizes in human readable format
-l
(long list)-
provides extra details, one file/directory per line
-r
(reverse)-
reverses the order of a sort
-R
(recursive)-
lists every file, including files in each subdirectory
-S
(size)-
sorts by file size
-t
(time)-
sorts by modification time
-X
(eXtension)-
sorts by file extension
Answers to Guided Exercises
-
Use the following file structure to answer the following three questions:
/ ├── etc/ │ ├── network/ │ │ └── interfaces/ │ ├── systemd/ │ │ ├── resolved.conf │ │ ├── system/ │ │ ├── system.conf │ │ ├── user/ │ │ └── user.conf │ └── udev/ │ ├── rules.d │ └── udev.conf └── home/ ├── lost+found/ ├── user/ │ └── Documents/ └── michael/ └── Music/
-
What command will navigate into the
network
directory regardless of your current location?cd /etc/network
-
What command can
user
enter to navigate into theirDocuments
directory from/etc/udev
? Use the shortest possible path.cd ~/Documents
-
What command can
user
enter to navigate intomichael
'sMusic
directory? Use the shortest possible path:cd ~michael/Music
-
-
Consider the following output of
ls -lh
to answer the next two questions. Note that directories are indicated with ad
at the beginning of the line.drwxrwxrwx 5 eric eric 4.0K Apr 26 2011 China/ -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0066.jpg -rwxrwxrwx 1 eric eric 1.5M Jul 18 2011 img_0067.jpg -rwxrwxrwx 1 eric eric 1.6M Jul 18 2011 img_0074.jpg -rwxrwxrwx 1 eric eric 1.8M Jul 18 2011 img_0075.jpg -rwxrwxrwx 1 eric eric 46K Jul 18 2011 scary.jpg -rwxrwxrwx 1 eric eric 469K Jan 29 2018 Screenshot from 2017-08-13 21-22-24.png -rwxrwxrwx 1 eric eric 498K Jan 29 2018 Screenshot from 2017-08-14 21-18-07.png -rwxrwxrwx 1 eric eric 211K Jan 29 2018 Screenshot from 2018-01-06 23-29-30.png -rwxrwxrwx 1 eric eric 150K Jul 18 2011 tobermory.jpg drwxrwxrwx 6 eric eric 4.0K Apr 26 2011 Tokyo/ -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 081.jpg -rwxrwxrwx 1 eric eric 1.4M Jul 18 2011 Toronto 085.jpg -rwxrwxrwx 1 eric eric 944K Jul 18 2011 Toronto 152.jpg -rwxrwxrwx 1 eric eric 728K Jul 18 2011 Toronto 173.jpg drwxrwxrwx 2 eric eric 4.0K Jun 5 2016 Wallpapers/
-
When you run the command
ls -lrS
, what file will be at the beginning?The three folders are all 4.0K, which is the smallest file size.
ls
will then sort the directories alphabetically by default. The correct answer is the filescary.jpg
. -
Please describe what you expect to see as the output for
ls -ad */
.This command will show all subdirectories, including hidden subdirectories.
-
Answers to Explorational Exercises
-
Run the
ls -lh
command in a directory that contains subdirectories. Note the listed size of these directories. Do these file sizes seem correct to you? Do they accurately represent the contents of all files inside that directory?No, they do not. Each directory has a listed file size of 4096 bytes. This is because directories here are an abstraction: they don’t exist as a tree structure on the disk. When you see a directory listed, you are seeing a link to a list of files. The size of these links is 4096 bytes.
-
Here is a new command to try:
du -h
. Run this command and describe the output that it gives you.The
du
command will generate a list of all files and directories, and indicate the size of each. For example,du -s
will display the file size of all files, directories, and subdirectories for a certain location. -
On many Linux systems, you can type in
ll
and get the same output as you would if you typedls -l
. Please note however thatll
is not a command. For example,man ll
will give you the message that no manual entry exists for it. What does this suggest to you about a feature of the command line?ll
is an alias ofls -l
. In Bash, we can use aliases to simplify commonly-used commands.ll
is often defined for you in Linux, but you can create your own as well.