In Unix, how do I list the files in a directory?
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 helloLists files whose complete name is hello; ifhellois a directory, displays the contents of thehellodirectory.ls hel*Lists all files in the directory that begin with the characters hel(e.g., files namedhel,hello, andhello.officer).ls hell?Lists files that begin with hellfollowed by one character, such ashelli,hello, andhell1.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
lscommand along with the path to the directory. For example, if you are in your home directory and want to list the contents of the/etcdirectory, enter: ls /etcThis will list the contents of the
/etcdirectory 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
lsby itself to see all the files in a directory). You may have multiple options and multiple filenames on a line.The options available with
lsare 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
lsare:-aShows all files, including those beginning with .(a period). The dot is special in the Unix file system.-dShows directory names, but not contents -FMarks special files with symbols to indicate what they are: /for directories,@for symbolic links,*for executable programs-lShows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (The lstands for "long".)-RRecursively 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
lsto a file, or pipe it to another command. If you want to save a list of the files in your directory to a file namedfoo, you would use the following command combination: ls > foo - If you want to mail a list of the files in your directory to a
user named
tom, you would use the following combination: ls | Mail tom
For a more complete discussion of the ls command, see the
online manual pages. At the Unix prompt, enter:
At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?
Last modified on February 16, 2009.







