The Linux Command Line—Looking Around

The Linux Command Line by William Shotts

Looking Around

Now that you know how to move from working directory to working directory, we’re going to take a tour of your Linux system and, along the way, learn some things about what makes it tick. But before we begin, I have to teach you some tools that will come in handy during our adventure. These are:
  • ls (list files and directories)
  • less (view text files)
  • file (classify a file’s contents)

ls

The ls command is used to list the contents of a directory. It is probably the most commonly used Linux command. It can be used in a number of different ways. Here are some examples:
Examples of the ls command

Command Result
ls List the files in the working directory
ls /bin List the files in the /bin directory (or any other directory you care to specify)
ls -l List the files in the working directory in long format
ls -l /etc /bin List the files in the /bin directory and the /etc directory in long format
ls -la .. List all files (even ones with names beginning with a period character, which are normally hidden) in the parent of the working directory in long format
These examples also point out an important concept about commands. Most commands operate like this:
    command -options arguments
where command is the name of the command, -options is one or more adjustments to the command’s behavior, and arguments is one or more “things” upon which the command operates.
In the case of ls, we see that ls is the name of the command, and that it can have one or more options, such as -a and -l, and it can operate on one or more files or directories.

A Closer Look At Long Format

If you use the -l option with ls, you will get a file listing that contains a wealth of information about the files being listed. Here’s an example:
-rw-------   1 bshotts  bshotts       576 Apr 17  1998 weather.txt
drwxr-xr-x   6 bshotts  bshotts      1024 Oct  9  1999 web_page
-rw-rw-r--   1 bshotts  bshotts    276480 Feb 11 20:41 web_site.tar
-rw-------   1 bshotts  bshotts      5743 Dec 16  1998 xmas_file.txt

----------     -------  -------  -------- ------------ -------------
    |             |        |         |         |             |
    |             |        |         |         |         File Name
    |             |        |         |         |
    |             |        |         |         +---  Modification Time
    |             |        |         |
    |             |        |         +-------------   Size (in bytes)
    |             |        |
    |             |        +-----------------------        Group
    |             |
    |             +--------------------------------        Owner
    |
    +----------------------------------------------   File Permissions

File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification occurred more than six months in the past, the date and year are displayed. Otherwise, the time of day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition to the file’s owner.
Owner
The name of the user who owns the file.
File Permissions
A representation of the file’s access permissions. The first character is the type of file. A “-” indicates a regular (ordinary) file. A “d” indicates a directory. The second set of three characters represent the read, write, and execution rights of the file’s owner. The next three represent the rights of the file’s group, and the final three represent the rights granted to everybody else. I’ll discuss this in more detail in a later lesson.

less

less is a program that lets you view text files. This is very handy since many of the files used to control and configure Linux are human readable.
The less program is invoked by simply typing:
less text_file
This will display the file.

Controlling less

Once started, less will display the text file one page at a time. You may use the Page Up and Page Down keys to move through the text file. To exit less, type “q”. Here are some commands that less will accept:
Keyboard commands for the less program

Command Action
Page Up or b Scroll back one page
Page Down or space Scroll forward one page
G Go to the end of the text file
1G Go to the beginning of the text file
/characters Search forward in the text file for an occurrence of the specified characters
n Repeat the previous search
h Display a complete list less commands and options
q Quit

file

As you wander around your Linux system, it is helpful to determine what kind of data a file contains before you try to view it. This is where the file command comes in. file will examine a file and tell you what kind of file it is.
To use the file program, just type:
file name_of_file

The file program can recognize most types of files, such as:
Various kinds of files

File Type Description Viewable as text?
ASCII text The name says it all yes
Bourne-Again shell script text A bash script yes
ELF 32-bit LSB core file A core dump file (a program will create this when it crashes) no
ELF 32-bit LSB executable An executable binary program no
ELF 32-bit LSB shared object A shared library no
GNU tar archive A tape archive file. A common way of storing groups of files. no, use tar tvf to view listing.
gzip compressed data An archive compressed with gzip no
HTML document text A web page yes
JPEG image data A compressed JPEG image no
PostScript document text A PostScript file yes
RPM A Red Hat Package Manager archive no, use rpm -q to examine contents.
Zip archive data An archive compressed with zip no
While it may seem that most files cannot be viewed as text, you will be surprised how many can. This is especially true of the important configuration files. You will also notice during our adventure that many features of the operating system are controlled by shell scripts. In Linux, there are no secrets!