View Directory Contents with ls

Now that you know how to change directories, it is time to learn how to view the contents of these directories. Using the ls command, you can display the contents of your current directory.

Many options are available with the ls command. The ls command, by itself, will not show you all the files in your directory. Some files are hidden files (also called dot files) and can only be seen with an additional option specified to the ls command.


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, at the prompt type man ls | col -b | lpr.

Type the command ls -a. Now you will see files that begin with dots.

Figure 11-3. ls with the -a Option

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. When you are searching for something in a directory, you are not usually looking for these configuration files, so keep them hidden to help avoid some screen clutter.

Viewing all the files using the ls -a command 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. For example, to see what is in the /etc directory from your home directory, type:

ls -al /etc

Figure 11-4. Sample ls Output for the /etc Directory

Here is a short list of some popular options to use 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 (see the Section called Pipes and Pagers) and I/O redirections2-managing-io (see the Section called I/O Redirection and Pipes), you will discover that there are other ways to view the contents of a directory.