UNIX - The ls Command

Using ls for directory listings For those who are familiar with DOS commands, the ls command is the Unix equivalent of DIR. This will show you the contents of the directory you are in. There are switches that can be used to display the information you need. By typing ls from the command prompt, you will recieve the following information (or something similar):

www: {1} % ls
public_html      wwwlogs
www: {2} %

-l switch: More information can be displayed about the contents of the directory by using the -l switch. This will display the permissions, number of items of a directory, owner, group, size, date, time, and name of each item in the directory (in that order). Here is an example of what you might see:

www: {2} % ls -l
total 2
drwxr-xr-x  14 sexsite user   1024 Jun 26 19:23 public_html
drwxrwxr-x   2 root    user    512 Sep 26 20:52 wwwlogs
www: {3} %

-a switch: The -a switch is used to display hidden files. Hidden files are files that do not have a prefix to it (example: .htaccess or .htpasswd are both hidden files). This command switch can be used in conjunction with the -l switch as indicated in this next example:

www: {4} % ls -al
total 8
drwxr-xr-x 7 sexsite user 1024 Jul 21 09:42 .
drwxr-xr-x 9 sexsite user  512 Sep 14 14:05 ..
-rw-r--r-- 1 sexsite user  831 Jul 27 12:47 .htaccess
-rw-r--r-- 1 sexsite user 5398 Jul 27 12:47 .htpasswd
-rw-r--r-- 1 sexsite user  330 Jul 16 15:33 background.gif
-rw-r--r-- 1 sexsite user  551 Jul 16 18:52 index.html
drwxr-xr-x 4 sexsite user  512 Sep 15 18:22 movies
drwxr-xr-x 9 sexsite user 1024 Sep 15 18:21 pics
www: {5} %

If you were to use the -a switch by itself, you would receive output similar to the first example with the exception that all files are shown (including the hidden files).

-r switch: It is unfortunate that the telnet client that comes with windows does not allow you to scroll what you have already done in the session. The window cannot be expanded, and often, the contents of a directory scroll past the top of the telnet window (there are some good telnet clients that will allow you to do so. A good one is CRT). The -r switch will allow you to reverse the order of the directory listing. This is helpful if the file you are looking for scrolls past the top of the screen. This switch can also be used in conjunction with other switches. The following example shows the use of all three switches at the same time:

www: {5} % ls -alr
total 8
drwxr-xr-x 9 sexsite user 1024 Sep 15 18:21 pics
drwxr-xr-x 4 sexsite user  512 Sep 15 18:22 movies
-rw-r--r-- 1 sexsite user  551 Jul 16 18:52 index.html
-rw-r--r-- 1 sexsite user  330 Jul 16 15:33 background.gif
-rw-r--r-- 1 sexsite user 5398 Jul 27 12:47 .htpasswd
-rw-r--r-- 1 sexsite user  831 Jul 27 12:47 .htaccess
drwxr-xr-x 9 sexsite user  512 Sep 14 14:05 ..
drwxr-xr-x 7 sexsite user 1024 Jul 21 09:42 .
www: {6} %

This concludes the lesson on using the ls command to display contents of directories.