-->
Previous Table of Contents Next


Listing Files and Directories with Ls and Dir

You’ll use the ls command, short for list, quite often—probably every time you use Linux, as a matter of fact. You’ve already used ls in a discussion of permissions. The following command lists the contents of the current directory or a specified directory:


     gilbert:/$ ls

     bin/         dev/     home/          mnt/          sbin/     var/

     boot/        dos/     lib/           proc/         tmp/      vmlinuz

     cdrom/       etc/     lost+found     root/         usr/

If you’re a UNIX user, this is probably not the version of ls you’re used to, especially if you try this command on your own Linux box. The ls version contained with Linux is actually the GNU version of ls. As such, it makes several improvements to the basic ls command found on other versions of UNIX. One improvement, which we can’t show you in the confines of a black-and-white text, is the addition of color to indicate directories (which will appear on your color monitor as blue) and special types of files. (Later in this section we’ll explain how to change these colors and what the colors mean.) Linux also uses slashes after the name to indicate directories.

In addition, ls (by default) sorts files and directories in ASCII order, in columns. That’s why the first column contains the directories beginning with b and c, followed by the rest of the alphabet. If there were directories that began with any capital letter, they’d be listed first; the directory X/ would appear before bin/, because under ASCII uppercase letters precede lowercase letters.


NOTE:  The Bourne Again SHell, bash, also supports the dir command in a limited sense. The dir command does the same thing as the ls -l command, which will be explained later in this section. DOS users will be relieved to know that their familiar dir command can also be used under Linux.

You can use one of the many command-line options to the ls command. For example, if you use the ls command in your home directory, you’ll discover that there are no apparent files to be found:


     gilbert:~$ ls

     gilbert:~$

However, if you run the command with the -a option, you’ll see the following:


     gilbert:~$ ls -a

     ./      .bash_history     .kermrc        .lessrc

     ../     .emacs            .less          .term/

The files beginning with the period (.) are called hidden files. Actually, they’re not so hidden as to be mysterious; they’re merely hidden when you use the ls command to search for files. The -a option tells the ls command to look for all files.

There are two other listings—. and ..—that may be unfamiliar if you’re not a UNIX user. The single period (.) is merely another way to display the current directory, while the double period (..) is used to display the parent directory.

The -l (ell, not one) option to ls prints a long list of the directory’s contents:


     gilbert:~$ ls -l

The ls command can also be used to determine the existence of a single file in short form:


     gilbert:~$ ls data

     data

or in long form:


     gilbert:~$ ls -l data

     -rwx------  1 kevinr  group1     854 Apr  2 19:12 data

Table 4.5 summarizes the ls command’s important options.

Table 4.5 A Summary of the Ls Command Options
Option Result

-a Lists all files, including hidden files.
-A Lists all files, except for the . and .. listings.
-c Sorts files by the time they were last changed, rather than by the default ASCII order, beginning with the oldest file.
-d Lists only the name of a directory, not its contents.
-l Lists files and directories in long format.
-r Lists the contents in reverse order.
-t Sorts files by the time they were last changed beginning with the newest file.
-x Lists files and sorts them across the page instead of by columns.


NOTE:  The ls command isn’t the only tool for viewing files and directories on a Linux system. If you’ve installed XFree86, there’s a file manager that can be used to graphically display the contents of your Linux system (see Chapter 5 for details). And there’s a command-line tool, the Midnight Commander, that works similarly to the Norton Commander (a once-popular MS-DOS application) (see Chapter 5 for details).

Changing the Ls Colors

Although we can’t show you, ls does indeed display different types of files in different colors. While you probably don’t want to change these colors, Linux gives you the ability to do so. (Indeed, Linux gives you the ability to do a great many things you’ll probably never bother to do, but that’s to the credit of the people who put Linux together.) The settings for these colors are stored in the file /etc/DIR_COLORS, and this file is used by all users. If you want to change these settings, you need to copy this file to your home directory, rename the file .dir_colors (making it a hidden file), and edit the listings in the file. As with many of the configuration files used with Linux, there’s enough comments in the default /etc/DIR_COLORS to guide you through any editing session.


Previous Table of Contents Next