-->
Previous | Table of Contents | Next |
Youll use the ls command, short for list, quite oftenprobably every time you use Linux, as a matter of fact. Youve 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 youre a UNIX user, this is probably not the version of ls youre 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 cant 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 well 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. Thats 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, theyd 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, youll discover that there are no apparent files to be found:
gilbert:~$ ls gilbert:~$
However, if you run the command with the -a option, youll see the following:
gilbert:~$ ls -a ./ .bash_history .kermrc .lessrc ../ .emacs .less .term/
The files beginning with the period (.) are called hidden files. Actually, theyre not so hidden as to be mysterious; theyre 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 youre 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 directorys 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 commands important 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 isnt the only tool for viewing files and directories on a Linux system. If youve installed XFree86, theres a file manager that can be used to graphically display the contents of your Linux system (see Chapter 5 for details). And theres 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 cant show you, ls does indeed display different types of files in different colors. While you probably dont 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 youll probably never bother to do, but thats 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, theres enough comments in the default /etc/DIR_COLORS to guide you through any editing session.
Previous | Table of Contents | Next |