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
CommandResult
lsList the files in the working directory
ls /binList the files in the /bin directory (or any other directory you care to specify)
ls -lList the files in the working directory in long format
ls -l /etc /binList 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
CommandAction
Page Up or bScroll back one page
Page Down or spaceScroll forward one page
GGo to the end of the text file
1GGo to the beginning of the text file
/charactersSearch forward in the text file for an occurrence of the specified characters
nRepeat the previous search
hDisplay a complete list less commands and options
qQuit

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 TypeDescriptionViewable as text?
ASCII textThe name says it allyes
Bourne-Again shell script textbash scriptyes
ELF 32-bit LSB core fileA core dump file (a program will create this when it crashes)no
ELF 32-bit LSB executableAn executable binary programno
ELF 32-bit LSB shared objectA shared libraryno
GNU tar archiveA tape archive file. A common way of storing groups of files.no, use tar tvf to view listing.
gzip compressed dataAn archive compressed with gzipno
HTML document textA web pageyes
JPEG image dataA compressed JPEG imageno
PostScript document textA PostScript fileyes
RPMA Red Hat Package Manager archiveno, use rpm -q to examine contents.
Zip archive dataAn archive compressed with zipno
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!