View Directory Contents with ls

Now that you know how to change directories, it's time to take a look at what's 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, won't 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'll 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're hidden is to help prevent any accidental tampering by the user. Besides, when you're searching for something in a directory, you're 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 don't have to be in the directory whose contents you want to view to use the ls command.

See what's in the /etc directory by typing:

ls -al /etc

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

Here's 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'll discover that there are other ways to view the contents of a directory.