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:
ls
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 ishello
; ifhello
is a directory, displays the contents of thehello
directory.ls hel*
Lists all files in the directory that begin with the charactershel
(for example, files namedhel
,hello
, andhello.officer
).ls hell?
Lists files that begin withhell
followed 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
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:-a
Shows all files, including those beginning with.
(a period). The dot is special in the Unix file system.-d
Shows directory names, but not contents-F
Marks special files with symbols to indicate what they are:/
for directories,@
for symbolic links,*
for executable programs-l
Shows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (Thel
stands for "long".)-R
Recursively lists subdirectoriesThe 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 namedfoo
, 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.