View Directory Contents with ls

Now that you know how to change directories, it is time to take a look at what is in these directories.

Type ls and press [Enter]. You should see the contents of your current directory — or most of it, at least.

The ls command, by itself, will not show you all the files in your directory. To see everything, you must include another option or two.

A multitude of options are available with the ls command.

TipRead the ls Man Page

If you want to see all the options of the ls command, you can read the man page by typing man ls at a shell prompt. If you want to print the man page, type man ls | col -b | lpr at the prompt.

Why are there so many options? Because they can help you sort information according to your needs. For example, you can specify how files are displayed, see their permissions and much more.

Figure 10-6. ls with -a Option

Type ls -a. Now you will see files that begin with dots. These are called hidden files or, appropriately enough, dot files.

Hidden files are mostly configuration files which set preferences in programs, window managers, shells, and more. The reason they are hidden is to help prevent any accidental tampering by the user. Besides, when you are searching for something in a directory, you are not usually looking for these configuration files, so keeping them hidden helps to avoid some screen clutter.

Viewing all the files (ls -a) can give you plenty of detail, but you can view still more information, simply by adding more than one option.

If you want to see the size of a file or directory, when it was created and more, just add the "long" option (-l) to the ls -a command:

ls -al

This command shows the file creation date, its size, ownership, permissions, and more.

You do not have to be in the directory whose contents you want to view to use the ls command.

See what is in the /etc directory by typing:

ls -al /etc

You will get plenty of information about the contents of the /etc directory.

Here is a short list of some popular options with ls. Remember, you can view the full list by reading the ls man page (man ls).

A little later in this chapter, when we introduce you to pipes and I/O redirection, you will discover that there are other ways to view the contents of a directory.