Exploring the System

Chapter 03

Exploring the System Lecture Video

Having familiarized ourselves with file system navigation, it's now the opportune moment to embark on a guided exploration of our Linux system. Prior to commencing, we shall acquire additional knowledge of commands that will prove valuable throughout our journey:

  • ls – List directory contents

  • file – Determine file type

  • less – View file contents

Enhancing the Experience with ls

Recognizing its widespread utility, the ls command holds a prominent position as one of the most frequently employed commands. Its capabilities enable us to observe the contents of directories and ascertain various crucial attributes associated with files and directories. As demonstrated earlier, a straightforward execution of ls provides us with a comprehensive list of files and sub directories housed within the present working directory:

In addition to the current working directory, it is possible to specify a particular directory for listing purposes by utilizing the following syntax:

Furthermore, it is also feasible to designate multiple directories for listing. In the following illustration, we will enumerate both the user's home directory (represented by the "~" symbol) and the /usr directory:

Another option available to us is modifying the output format to unveil more comprehensive details:

By incorporating the -l parameter into the command, we have altered the output to the long format.

Options and Arguments

This leads us to a crucial aspect regarding the functionality of most commands. Typically, commands are accompanied by one or more options that modify their behavior, as well as one or more arguments that represent the entities upon which the command operates. Therefore, the general structure of most commands can be depicted as follows:

The majority of commands utilize options that consist of a single character preceded by a hyphen, such as -l. However, numerous commands, including those developed by the GNU Project, offer support for long options, which consist of a word preceded by two hyphens. Additionally, many commands allow the concatenation of multiple short options. In the given example, the ls command is provided with two options: the l option to generate long format output and the t option to sort the output based on the modification time of the files.

To reverse the order of the sort, we will introduce the long option --reverse into the command.

It's worth noting that command options, similar to filenames in Linux, are case-sensitive.

The ls command offers a wide range of available options, with the most frequently used ones listed in the following table.

Option
Long Option
Description

-a

--all

Display all files, including those whose names commence with a period, which are typically not exhibited (referred to as hidden).

-A

--almost-all

Similar to the aforementioned -a option, this alternative option excludes listing the . (current directory) and .. (parent directory).

-d

--directory

By default, when a directory is specified, ls displays the contents of the directory rather than the directory itself. However, when used in conjunction with the -l option, this particular option allows us to obtain details about the directory itself instead of its contents.

-F

--classify

When enabled, this option appends an indicator character at the conclusion of each listed name, such as a / to signify a directory.

-h

--human-readable

In long format listings, exhibit file sizes in a human-readable format instead of bytes.

-l

Display results in long format.

-r

--reverse

Alter the display order to be in reverse. Typically, ls presents its results in ascending alphabetical order.

-S

Sort results by file size.

-t

Sort by modification time.

Exploring Long Format in Greater Detail

As demonstrated earlier, when utilizing the -l option, ls presents its results in long format, offering a wealth of valuable information. Below is an illustration showcasing the Examples directory from an Ubuntu based system:

Now, let's examine the various fields of one of the files and delve into their respective meanings:

Field
Meaning

-rw-------

The file's access rights are denoted by the first character, indicating the file type. For instance, a leading dash signifies a regular file, whereas a d signifies a directory. The subsequent three characters represent the access rights for the file's owner, followed by three characters for members of the file's group, and the final three characters for everyone else. The comprehensive understanding of this aspect is elaborated upon in Chapter 9 - Permissions.

1

File's number of hard links. See the discussion of links later in this chapter.

me

The username of the file's owner.

me

The name of the group which owns the file.

49

Size of the file in bytes.

Mar 21 18:28

Date and time of the file's last modification.

continue.txt

Name of the file.

Identifying the Type of a File Using the file Command

As we navigate through the system, it becomes advantageous to ascertain the contents of files. To accomplish this, we can utilize the file command to determine the file's type. As previously mentioned, filenames in Linux do not necessarily indicate the actual contents of a file. Although a filename like about.png might conventionally imply a PNG image, Linux does not enforce this requirement. We can invoke the file command in the following manner:

Upon invocation, the file command will display a concise description of the file's contents. For instance:

A multitude of file types exist within Unix-like operating systems like Linux. In fact, a fundamental principle embraced by such systems is the concept that "everything is a file." As we progress through our lessons, we will witness the veracity of this statement and comprehend its significance.

While several files on your system might be familiar, such as MP3 and JPEG files, there exists a variety of file types that may be less evident, as well as a few that are rather peculiar.

Examining File Contents Using less

The less command is designed as a text file viewer program. Within our Linux system, numerous files consist of human-readable text. The less program offers a convenient approach to inspecting such files.

What Is “Text”?

Computers employ various methods to represent information, all of which involve establishing a connection between the information and numerical values used for representation. Ultimately, computers solely comprehend numbers, necessitating the conversion of all data into numeric form.

Among these representation systems, certain ones can be highly intricate, such as compressed video files, while others are relatively straightforward. One of the earliest and simplest systems is known as ASCII text. ASCII, an acronym for American Standard Code for Information Interchange, denotes a basic encoding scheme initially employed on Teletype machines to establish a mapping between keyboard characters and corresponding numeric values.

Text entails a straightforward mapping of characters to numerical values, resulting in compact data representation. A mere fifty characters of text correspond to fifty bytes of data. It is crucial to comprehend that text solely encompasses a basic mapping of characters to numbers. It should not be confused with word processor documents like those generated by Microsoft Word or OpenOffice.org Writer. Unlike simple ASCII text, such files incorporate numerous non-text elements dedicated to describing their structure and formatting. In contrast, plain ASCII text files solely encompass the characters themselves, along with a few essential control codes like tabs, carriage returns, and line feeds.

Text format files are prevalent throughout a Linux system, and numerous Linux tools are specifically designed to handle such files. The significance of this format is also acknowledged by Windows. A notable example is the widely recognized NOTEPAD.EXE program, which serves as an editor for plain ASCII text files.

The examination of text files holds significance due to the presence of system settings files, also known as configuration files, stored in this format. Being able to read these files provides valuable insights into the system's functioning. Moreover, numerous programs utilized by the system, referred to as scripts, are also stored in text format. In subsequent chapters, we will delve into the process of editing text files to modify system settings and create our own scripts. However, for now, our focus will primarily be on inspecting their contents.

The less command is used as follows:

Once initiated, the less program enables you to navigate through a text file both forwards and backwards. For instance, to inspect the file that specifies all user accounts within the system, input the following command:

Once the less program is launched, we gain access to the file's contents. If the file extends beyond a single page, we can scroll both upwards and downwards. To exit the less program, simply press the q key.

Below is a table presenting the frequently used keyboard commands in less:

Command
Action

Page Up or b

Scroll back one page

Page Down or 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

1G or g

Move to the beginning of the text file

/characters

Search forward to the next occurrence of characters

n

Search for the next occurrence of the previous search

h

Display help screen

q

Quit less

Less Is More

The less utility was developed to enhance and supplant the older Unix tool known as more. Its name, "less," cleverly references the design principle "less is more," often associated with modernist architects and designers.

Belonging to the category of "pagers," less facilitates the convenient perusal of lengthy text documents page by page. Unlike its predecessor, more, which solely enabled forward paging, less permits both forward and backward paging along with a host of additional features.

A Guided Tour

The arrangement of the file system on your Linux system closely resembles that found in other Unix-like systems. This design is defined by the Linux Filesystem Hierarchy Standard, which is a published standard governing the file system structure. While not all Linux distributions adhere to the standard in its entirety, the majority of them closely align with its guidelines.

Now, we will explore the file system firsthand to gain insight into the inner workings of our Linux system. This will provide an opportunity for you to practice your navigation skills. Along the way, we will uncover numerous intriguing files that are presented in easily readable human text format. As we embark on this tour, feel free to try the following actions:

  1. cd into a given directory

  2. List directory content using ls -l

  3. When you encounter a captivating file, utilize the file command to ascertain its contents.

  4. If the file appears to be text-based, consider using the less command to view its contents.

While navigating through the system, don't hesitate to explore and examine various elements. Regular users are generally restricted from making any critical changes as that falls under the responsibility of system administrators. If a command raises an issue or error, simply move on to another task or area. Take your time to observe and explore. Remember, in Linux, there are no hidden secrets, and the system is open for our exploration.

The table below highlights a selection of directories that we can delve into. Don't hesitate to explore additional directories beyond those listed!

Directory
Comments

/

The root directory. Where everything begins.

/bin

This directory houses essential binaries (programs) that are necessary for the system to initialize and operate.

/boot

This directory encompasses the Linux kernel, the initial RAM disk image (which contains drivers required during the boot process), and the boot loader. Interesting files: - /boot/grub/grub.conf or menu.lst, which are used to configure the boot loader. - /boot/vmlinuz, the Linux kernel

/dev

This directory holds special entries known as device nodes, following the principle of "everything is a file" even for devices. It serves as a central location where the kernel maintains a comprehensive list of recognized devices.

/etc

Within the /etc directory, you will find all the system-wide configuration files. Additionally, it houses a compilation of shell scripts responsible for initiating system services during boot time. It is important to note that all contents within this directory should be in readable text format. Interesting Files: Although every file in /etc holds its own intrigue, I would like to share some of my 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. - /etc/passwd, a list of the user accounts.

/home

Under typical configurations, every user is assigned a directory within /home. Regular users possess write privileges solely within their respective home directories. This restriction serves as a safeguard against inadvertent user actions that could potentially impact the system.

/lib

This directory accommodates shared library files that are utilized by core system programs. These files bear resemblance to DLLs (Dynamic Link Libraries) in the Windows operating system.

/lost+found

For every formatted partition or device utilizing a Linux file system like ext3, you will find this directory. It comes into play in the event of a partial recovery from a file system corruption incident. Unless your system has encountered a severe issue, this directory will typically remain empty.

/media

In contemporary Linux systems, the /media directory serves as the location for mount points associated with removable media like USB drives, CD-ROMs, and other similar devices. These media are automatically mounted upon insertion.

/mnt

In older Linux systems, the /mnt directory is designated for manually mounted removable devices, serving as the location for their mount points.

/opt

The /opt directory is specifically designated for the installation of "optional" software, primarily intended to accommodate commercial software products that may be installed on your system.

/proc

The /proc directory holds a special status. It does not function as a conventional file system where files are stored on the hard drive. Instead, it operates as a virtual file system meticulously maintained by the Linux kernel. The "files" within /proc act as windows into the inner workings of the kernel itself. These files are readable and offer insights into how the kernel perceives your computer.

/root

This is the home directory for the root account.

/sbin

The /sbin directory houses essential system binaries. These binaries comprise programs that execute critical system tasks typically restricted to the superuser.

/tmp

The /tmp directory serves as a designated location for storing temporary files created by various programs. These files are transient in nature. In certain configurations, the /tmp directory may be emptied upon system reboot.

/usr

The /usr directory tree is typically the largest one within a Linux system. It encompasses all the programs and supporting files utilized by regular users.

/usr/bin

The /usr/bin directory houses the executable programs installed by your Linux distribution. It is not uncommon for this directory to accommodate a vast number of programs, potentially reaching into the thousands.

/usr/lib

The shared libraries for the programs in /usr/bin.

/usr/local

The /usr/local directory tree serves as the destination for programs intended for system-wide use but are not included in your distribution. When you compile programs from source code, they are typically installed in the /usr/local/bin directory. Upon initial installation of a Linux system, this directory tree exists, but it remains empty until the system administrator adds content to it.

/usr/sbin

Contains more system administration programs.

/usr/share

The /usr/share directory houses the shared data utilized by programs in /usr/bin. This encompasses various elements such as default configuration files, icons, screen backgrounds, sound files, and more.

/usr/share/doc

In the /usr/share/doc directory, you will discover documentation files grouped by package for most installed packages on the system.

/var

Except for /tmp and /home, the directories we have explored thus far generally remain unchanged. However, the /var directory tree is designated for storing data that is expected to be modified. It contains various databases, spool files, user mail, and other dynamically changing data.

/var/log

The /var/log directory houses log files that record various system activities. Monitoring these log files periodically is crucial. Among them, the /var/log/messages file is particularly valuable. Keep in mind that on certain systems, access to log files may require superuser privileges due to security considerations.

While exploring, it's common to come across a directory listing that includes an entry similar to this:

Take note of the listing's initial letter as l and the presence of two filenames. This signifies a special file known as a symbolic link (or symlink for short). Symbolic links allow for multiple filenames to refer to the same file, providing a valuable and practical feature in most Unix-like systems.

Consider the following situation: There's a program that relies on a shared resource stored in a file called foo, which undergoes frequent version updates. It would be beneficial to include the version number in the filename so that administrators and other interested parties can easily identify the installed version of foo. However, this poses a challenge. If we modify the name of the shared resource, we would need to locate every program that utilizes it and update their code to reference the new resource name whenever a new version is installed. This process can be cumbersome and time-consuming, certainly not an enjoyable task.

Symbolic links come to the rescue in this scenario. Let's consider the case where we install version 2.6 of foo with the filename foo-2.6 and create a symbolic link named foo that directly points to foo-2.6. This means that when a program accesses the file foo, it is actually accessing the file foo-2.6. This solution brings satisfaction to everyone involved. Programs relying on foo can find it without any issues, and we can still easily identify the actual installed version. When the time comes to upgrade to foo-2.7, we simply add the new file to our system, delete the existing symbolic link foo, and create a new symbolic link that directs to the updated version. This not only resolves the version upgrade problem but also allows us to retain both versions on our machine. Consider a scenario where foo-2.7 introduces a bug (those pesky developers!). In such a case, we can effortlessly revert to the old version by deleting the symbolic link pointing to the new version and creating a new symbolic link that points back to the previous version.

In the directory listing above (taken from the /lib directory of a Fedora system), there is a symbolic link named libc.so.6 that is linked to a shared library file named libc-2.6.so. This clever setup ensures that programs searching for libc.so.6 will effectively access the file libc-2.6.so. Creating symbolic links will be covered in the upcoming chapter, where we'll learn how to implement this useful feature.

While discussing links, it's important to mention another type called hard links. Hard links enable files to have multiple names, but they operate differently compared to symbolic links. In the next chapter, we will delve deeper into the distinctions between symbolic and hard links.

Summary

After concluding our tour, we have gained extensive knowledge about our system. We have explored different files, directories, and their contents. An important aspect to note is the openness of the system. In Linux, numerous crucial files are readily accessible as plain human-readable text. Unlike certain proprietary systems, Linux provides transparency by making everything available for examination and study.

Last updated