Learn Linux 03: Exploring The System

Published

Contents


Introduction

It’s time to take a tour of the file system on our Linux system. One of the common ideas in Unix-like operating systems such as Linux is that “everything is a file”. As we proceed, we will see just how true that statement is. This chapter will introduce the following commands:

  • ls - List directory contents
  • file - Determine file type
  • less - View file contents
  • reset - Reset the terminal

More ls

The ls command is probably the most used command, and for good reason. With it, besides the current working directory we can also specify a directory to list.

[user@linux ~]$ ls
Desktop Documents Music Pictures Public Templates Videos
[user@linux ~]$ ls /usr
bin games include lib local sbin share src

We can even specify multiple directories. For instance let’s list both the user’s home directory (symbolized by the ~ character) and the /usr directory.

[user@linux ~]$ ls ~ /usr
/home/user:
Desktop Documents Music Pictures Public Templates Videos
/usr:
bin games include lib local sbin share src

We can also change the format of the output to reveal more detail. By adding -l option to the command, we changed the output to the long format.

[user@linux ~]$ ls -l
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Desktop
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Documents
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Music
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Pictures
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Public
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Templates
drwxrwxr-x 2 user user 4096 2017-05-09 12:00 Videos

Options And Arguments

Often Linux commands are followed by one or more options, further, by one or more arguments. Linux command options are used to control the output/behavior of a Linux command.

command -options arguments

Most commands use options, which consist of a single character preceded by a dash, for example, -l. Many commands, however, including those from the GNU Project, also support long options, consisting of a word preceded by two dashes. Also, many commands allow multiple short options to be strung together. In the following example, the ls command is given two options, which are the l option to produce long format output, and the a option to list all files, even those with names that begin with a period, which are normally not listed (that is, hidden).

[user@linux ~]$ ls -la

A Better Look At The Long Format

As we saw earlier, the -l option causes ls to display its results in long format. This format contains a great deal of useful information.

-rw-r--r-- 1 root root 4096 2017-05-09 12:00 debian.jpg
-rw-r--r-- 1 root root 4096 2017-05-09 12:00 hello.png
-rw-r--r-- 1 root root 4096 2017-05-09 12:00 world.png
-rw-r--r-- 1 root root 4096 2017-05-09 12:00 welcome.doc

Let’s look at the different fields from one of the files and their meanings.

  • -rw-r--r-- - Access rights to the file.
  • 1 - File’s number of hard links.
  • root - The username of the file’s owner.
  • root - The name of the group that owns the file.
  • 2017-05-09 12:00 - Date and the time of the file’s last modification.
  • debian.jpg - Name of the file.

Determining A File’s Type With file Command

Filenames in Linux are not required to reflect a file’s contents. While a filename like picture.jpg would normally be expected to contain a JPEG-compressed image, it is not required to in Linux. The file command classifies files based on a series of tests and determines the file type based on the first successful test.

[user@linux ~]$ file /etc/group
/etc/group: ASCII text
[user@linux ~]$ file image.jpg
image.jpg: JPEG image data, JFIF standard 1.01

Viewing File Contents With less Command

The less command is a program to view text files. Throughout our Linux system, there are many files that contain human-readable text. The less program provides a convenient way to examine them. The example below examines the file that defines all the system’s user accounts.

[user@linux ~]$ less /etc/passwd

Once the less program starts, we can view the contents of the file. If the file is longer than one page, we can scroll up and down. To exit less, press q.

Here is a list of less commands:

  • b - scroll back one page
  • space - scroll forward one page
  • up arrow - scroll up one line
  • down arrow - scroll down one line
  • G - move to the end of the text file
  • g - move to the beginning of the text file
  • /text - search forward to the next occurence of text
  • n - Search for the next occurrence of the previous search
  • h - display help screen
  • q - quit

Less Is More

The less program was designed as an improved replacement of an earlier Unix program called more. The name less is a play on the phrase “less is more” - a motto of modernist architects and designers.

less falls into the class of programs called pagers, programs that allow the easy viewing of long text documents in a page-by-page manner. Whereas the more program could only page forward, the less program allows paging both forward and backward and has many other features as well.

Taking A Tour Of The File System

The file system layout on a Linux system is much like that found on other Unix-like systems. The design is actually specified in a published standard called the Linux Filesystem Hierarchy Standard. Not all Linux distributions conform to the standard exactly, but most come pretty close.

Next, we are going to wander around the file system ourselves to see what makes our Linux system tick. This will give us a chance to practice our navigation skills. One of the things we will discover is that many of the interesting files are in plain human-readable text. As we go on our tour, try the following:

  • cd into a given directory.
  • List the directory contents with ls -la.
  • If you see an interesting file, determine its contents with file.
  • If it looks like it might be text, try viewing it with less.

Here are couple of notes before starting:

  • If we accidentally attempt to view a non-text file and it scrambles the terminal window, we can recover by entering the reset command.
  • As we wander around, don’t be afraid to look at stuff. Regular users are largely prohibited from messing things up. Spend some time looking around. The system is ours to explore. Remember, in Linux, there are no secrets!

Directories found on Linux system:

  • / - The root directory, where everything begins.
  • /bin - Contains binaries (programs) that must be present for the system to boot and run.
  • /boot - Contains the Linux kernel, initial RAM disk image (for drivers needed at boot time), and the boot loader. Interesting files include /boot/ grub/grub.conf, or menu.lst, which is used to configure the boot loader, and /boot/vmlinuz (or something similar), the Linux kernel.
  • dev - This is a special directory that contains device nodes. “Everything is a file” also applies to devices. Here is where the kernel maintains a list of all the devices it understands.
  • /etc - This directory contains all the system-wide configuration files. It also contains a collection of shell scripts that start each of the system services at boot time. Everything in this directory should be readable text. While everything in /etc is interesting, here are some all-time favorites: /etc/crontab, a file that defines when automated jobs will run; /etc/fstab, a table of storage devices and their associated mount points; and /etc/passwd, a list of the user accounts.
  • /home - In normal configurations, each user is given a directory in /home. Ordinary users can write files only in their home directories. This limitation protects the system from errant user activity.
  • /lib - Contains shared library files used by the core system programs. These are similar to dynamic link libraries (DLLs) in Windows.
  • /lost+found - Each formatted partition or device using a Linux file system, such as ext3, will have this directory. It is used in the case of a partial recovery from a file system corruption event. Unless something really bad has happened to your system, this directory will remain empty.
  • /media - On modern Linux systems, the /media directory will contain the mount points for removable media such as USB drives, CD-ROMs, and so on, that are mounted automatically at insertion.
  • /mnt - On older Linux systems, the /mnt directory contains mount points for removable devices that have been mounted manually.
  • /opt - This directory is used to install “optional” software. This is mainly used to hold commercial software products that might be installed on the system.
  • /proc - This directory is special. It’s not a real file system in the sense of files stored on your hard drive. Rather, it is a virtual file system maintained by the Linux kernel. The “files” it contains are peepholes into the kernel itself. The files are readable and will give you a picture of how the kernel sees your computer.
  • /root - This is the home directory for the root account.
  • /sbin - This directory contains “system” binaries. These are programs that perform vital system tasks that are generally reserved for the superuser.
  • /tmp - This directory is intended for the storage of temporary, transient files created by various programs. Some configurations cause this directory to be emptied each time the system is rebooted.
  • /usr - This directory tree is likely the largest one on a Linux system. It contains all the programs and support files used by regular users.
  • /usr/bin - Contains the executable programs installed by your Linux distribution. It is not uncommon for this directory to hold thousands of programs.
  • /usr/lib - The shared libraries for the programs in /usr/bin.
  • /usr/local - This directory tree is where programs that are not included with your distribution but are intended for system-wide use are installed. Programs compiled from source code are normally installed in /usr/local/bin. On a newly installed Linux system, this tree exists, but it will be empty until the system administrator puts something in it.
  • /usr/sbin - Contains more system administration programs.
  • /usr/share - Contains all the shared data used by programs in /usr/bin. This includes things such as default configuration files, icons, screen backgrounds, sound files, and so on.
  • /usr/share/doc - Most packages installed on the system will include some kind of documentation. In /usr/share/doc, we will find documentation files organized by package.
  • /var - With the exception of /tmp and /home, the directories we have looked at so far remain relatively static; that is, their contents don’t change. The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, and so forth, are located here.
  • /var/log - Contains log files, records of various system activity. These are important and should be monitored from time to time. The most useful ones are /var/log/messages and /var/log/syslog. Note that for security reasons on some systems, you must be the superuser to view log files.

As we look around, we are likely to see a directory listing (for example, /lib) with an entry like the one below.

lrwxrwxrwx 1 root root   11 2017-05-09 12:00 libdmmp.so -> libdmmp.so.0.2.0

Notice how the first letter of the listing is (l) and the entry seems to have two filenames? This is a special kind of a file called a symbolic link (also known as a soft link or symlink). In most Unix-like systems, it is possible to have a file referenced by multiple names. While the value of this might not be obvious, it is really a useful feature.

Picture this scenario: a program requires the use of a shared resource of some kind contained in a file named “foo”, but “foo” has frequent version changes. It would be good to include the version number in the filename so the administrator or other interested party could see what version of “foo” is installed. This presents a problem. If we change the name of the shared resource, we have to track down every program that might use it and change it to look for a new resource name every time a new version of the resource is installed. That doesn’t sound like fun at all.

Here is where symbolic links save the day. Suppose we install version 2.3 of “foo”, which has the filename “foo-2.3”, and then create a symbolic link simply called “foo” that points to “foo-2.3”. This means that when a program opens the file “foo”, it is actually opening the file “foo-2.3”. Now everybody is happy. The programs that rely on “foo” can find it, and we can still see what actual version is installed. When it is time to upgrade to “foo-2.4”, we just add the file to our system, delete the symbolic link “foo”, and create a new one that points to the new version. Not only does this solve the problem of the version upgrade, it also allows us to keep both versions on our machine. Imagine that “foo-2.4” has a bug (damn those developers!), and we need to revert to the old version. Again, we just delete the symbolic link pointing to the new version and create a new symbolic link pointing to the old version.

The directory listing at the beginning of this section shows a symbolic link called libdmmp.so that points to a shared library file called libdmmp.so.0.2.0. This means that programs looking for libdmmp.so will actually get the file libdmmp.so.0.2.0.

While we are on the subject of links, we need to mention a second type of link called hard links. Hard links also allow files to have multiple names, but they do it in a different way. We’ll talk more about the differences between symbolic and hard links in the next chapter.

Summary

In this chapter we have learned a lot about our system. We’ve seen various files and directories and their contents. The important thing to take away from this is how open the system is. Linux makes everything available for examination and study.