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 because their filenames start with a dot) and can only be seen by using the -a option with the ls command.


To see all the options of the ls command, you can read the man page for the ls command 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.

click to expand
Figure 8-3. ls with the -a Option


Hidden files are mostly configuration files that 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 when viewing directories at the shell prompt.

Viewing all the files using the ls -a command can give you plenty of detail, but you can view still more information by adding the -l option.

Adding the -i option to the ls command shows you information such as the size of a file or directory, the file creation date, 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 8-4. Sample ls Output for the /etc Directory

Table 8-2 shows some options commonly used with ls. Remember, you can view the full list by reading the ls man page (man ls).

Table 8-2: Options for ls


What It Does


Lists all the files in the directory, including the hidden files (.filename). The .. and . at the top of your list refer to the parent directory and the current directory, respectively.


Lists details about contents, including permissions (modes), owner, group, size, creation date, whether the file is a link to somewhere else on the system, and where its link points.


Adds a symbol to the end of each listing. These symbols include / to indicate a directory; @ to indicate a symbolic link to another file; and * to indicate an executable file.


Lists the contents of the directory from back to front.


This option lists the contents of all directories below the current directory recursively.


Sorts files by their size.

The Red Hat Documentation Team - Official Red Hat Linux User's Guide
The Red Hat Documentation Team - Official Red Hat Linux User's Guide
Year: 2002
Pages: 223 © 2008-2017.
If you may any questions please contact us: