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:
Lists files whose complete name is
hellois a directory, displays the contents of the
Lists all files in the directory that begin with the characters
hel(e.g., files named
Lists files that begin with
hellfollowed by one character, such as
*represents any number of unknown characters, while
?represents only one unknown character. You can use
?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 /etc
This 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
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 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
lstands 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
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 named
foo, you would use the following command combination: ls > foo
- If you want to mail a list of the files in your directory to a
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 June 20, 2012.