List the files in a directory in Unix

You can use the ls command to list the files in any directory to which you have access. For a simple directory listing, at the Unix prompt, enter:


This command will list the names of all the files and directories in the current working directory.

  • You can limit the files that are described by using fragments of filenames and wildcards. Examples of this are:
    ls hello
    Lists files whose complete name is hello; if hello is a directory, displays the contents of the hello directory.
    ls hel*
    Lists all files in the directory that begin with the characters hel (for example, files named hel, hello, and hello.officer).
    ls hell?
    Lists files that begin with hell followed by one character, such as helli, hello, and hell1.

    The * represents any number of unknown characters, while ? represents only one unknown character. You can use * and ? anywhere in the filename fragment.

  • If you would like to list files in another directory, use the ls command along with the path to the directory. For example, if you are in your home directory and want to list the contents of the /etc directory, enter:
     ls /etc

    This will list the contents of the /etc directory in columns.

  • Several options control the way in which the information you get is displayed. Options are used in this format:
     ls -option filename

    Neither the options nor the filename are required (you may use ls by itself to see all the files in a directory). You may have multiple options and multiple filenames on a line.

    The options available with ls are far too numerous to list here, but you can see them all in the online manual (man) pages.

    Some of the more helpful options for ls are:

    Shows all files, including those beginning with . (a period). The dot is special in the Unix file system.
    Shows directory names, but not contents
    Marks special files with symbols to indicate what they are: / for directories, @ for symbolic links, * for executable programs
    Shows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (The l stands for "long".)
    Recursively lists subdirectories

    The options can be combined. To list all the files in a directory in the long format, with marks for the types of files, you would enter:

     ls -Flg
  • As with many other Unix commands, you can redirect the output from ls to a file, or pipe it to another command. If you want to save a list of the files in your directory to a file named foo, you would use the following command combination:
     ls > foo

For a more complete discussion of the ls command, see the online manual pages. At the Unix prompt, enter:

 man ls

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document abds in the Knowledge Base.
Last modified on 2023-06-27 11:47:08.