Listing the files in a directory: ls

You can use the ls command to list the files in a directory:

   ls [option] directory_name

By combining different command options you can display as little or as much information about each file as you need.



Long listing

To get more information about each file and directory, use the command

ls -l [directory_name]

which displays a one-line summary for each file within the location specified by directory_name. If no directory name is given, the contents of the current directory are displayed. For example:

-rw-------  1 abc          4181 Jul 24  1992 mbox
drwxr-xr-x  2 abc           512 Oct 12 14:28 pub

The first item in the listing is called the file mode which shows the type of file and access permissions. The following items represent the number of links to it; the username of the person owning it; its size, when it was last changed, and finally, its name.


Hidden files

The command

ls -a

lists all the "hidden" files that begin with a '.' (dot). All other files and directories are also listed.

Hidden files are often used to hold commands that customise how a particular program is to work. These files are usually held together in your home directory.

Every directory has two dot files, '.' and '..' which can be used in a shorthand way to refer to the current directory '.'(dot) and the parent directory of the current directory '..' (dot dot).


ls command options

Option      Action
  -a        list hidden files
  -d        list the name of the current directory
  -F        show directories with a trailing '/'
            executable files with a trailing '*'
  -g        show group ownership of file in long listing
  -i        print the inode number of each file
  -l        long listing giving details about files
            and directories
  -R        list all subdirectories encountered
  -t        sort by time modified instead of name


ls examples

To list the contents of the current directory:

   ls
   README        bago.hlp      helpex.hlp   hyperhelp
   app-defaults  helpex        hoh.hlp

File and directory names are listed in columns in alphabetical order.

To list each file's type and inode number:

   ls -Fi
     6752 Mail/         102574 SGML/         147215 UNIX_tips/
   172802 Xapps/         51200 bin/           44801 dead.letter
    44802 mbox           44809 test*         102733 tmp/

This lists the contents of the current directory. Directories are shown with a trailing '/'; files which have access permissions that allow the file to be executed are shown with a trailing '*'. (The inode number is used to look up information such as type, size, location, owner etc, in the system inode table).

To list the contents of a directory using wildcard characters:

   ls *.txt
   udi1.txt           wais_x500_www.txt

This lists each file in the current directory which has the filename extension *.txt.

To list files in another directory:

   ls ../Mail

This lists the contents of the directory Mail which is at the same level in the directory hierarchy as the current directory.

To list each subdirectory and its contents, repeating this recursion:

   ls -R uunet
   MetaCard    README      README.ftp

   uunet/MetaCard:
   BLURB.txt  BUGS.txt   FAQ.txt    README.mc  metacard

   uunet/MetaCard/metacard:
   Makefile      XCmdGlue.c    XTtemplate.c  XtTemplate.c  mc       mchelp
   README.ext    XCmdGlue.h    XtGlue.c      external.c    mcXThelp mchome
   XCmdFuncs.c   XCmdNames.h   XtGlue.h      external.h    mcdemo   mctools

This will recursively list the contents of each subdirectory there is below the directory uunet.