DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Working with files and directories

Listing the contents of a directory

The names and other information about the files and subdirectories contained within a directory can be displayed using the ls(C) family of commands. In its simplest form, ls gives a list of the filenames found in the current working directory, as follows:

   $ ls
   cs-save
   gav_make
   glossary.s
   graphics
   intro.err
   nohup.out
   procs.txt
To see a list of the filenames in a multi-column format, use the lc variant, as follows:
   $ lc
   cs-save         intro.err
   gav_make        nohup.out
   glossary.s      procs.txt
   graphics
For a full listing, giving file size, permissions, owner and other items of information, use the ls -l option, as follows:
   $ ls -l
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
For a complete breakdown of this information, see ``File and directory attributes''. In fact, this version of the command is used so commonly, that it can be entered in shorthand, as l(C).

As we saw in ``Filenaming conventions'', filenames may begin with a dot, in which case, the files are hidden from normal directory listings. The ls -a (all) option displays hidden files as well as normal files, as follows:

   $ ls -a
   .
   ..
   .history
   .kshrc
   .mailbox
   .profile
   cs-save
   gav_make
   glossary.s
   graphics
   intro.err
   nohup.out
   procs.txt
To list the contents of another directory, without first moving to that directory, use the ls command, specifying the directory to look at as an argument, as follows:
   $ ls /u/workfiles/projects
This command line lists the contents of a directory called /u/workfiles/projects.

You need permission to read a directory before you can view its contents. See ``Access control for files and directories'' for an explanation of permissions.

The tilde-plus sequence (~+) is expanded by the shell to point to the current working directory (actually, the value of the PWD environment variable). A more useful variant of this notation is the tilde-minus notation (~-), which expands to the value of OLDPWD, that is, the previous working directory. This allows you to refer back to your earlier work without having to type in the relevant pathname, as follows:

   $ ls -l
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
   $ cd ../project2
   $ ls -l
   -rw-r--r--   1 chris   techpubs    3137 Oct 24 17:49 agenda
   drwxr-xr-x   2 chris   techpubs      96 Aug 31 13:08 bin
   $ ls -l ~-
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
If you list the contents of a directory that contains more files and subdirectories than can be displayed on one screen, the list scrolls continually until all the files have been displayed. This makes it very difficult to read them. To view the list one screen at a time, type the following:
   $ ls | more
The output from ls is piped to the more(C) command which then displays it; more prints its input one screen at a time. (See ``Running commands in a pipeline'' for more information about pipes.) Press <Enter> to scroll down by one line, or the <Space> bar to scroll down by one screen. Otherwise, you can pipe the output from ls into the pg(C) command, which performs a similar operation. The main difference between the two is that pg allows you to step backward through a file (by pressing the minus key (-)), as well as forward (by pressing <Enter> or the plus key (+)).

Another way to pause the scrolling is to use the <Ctrl>S and <Ctrl>Q keystrokes. Press <Ctrl>S to temporarily stop the scrolling, and <Ctrl>Q to continue. If you want to stop the listing completely, press <Del>. These keystrokes depend on your terminal setup; if they do not seem to work, ask your system administrator to help you.


Next topic: Renaming a directory
Previous topic: Creating a directory

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003