ls [-CFRacdilqrtu1][-H | -L ][-fgmnopsx][file...]
For each operand that names a file of a type other than directory or symbolic link to a directory, ls shall write the name of the file as well as any requested, associated information. For each operand that names a file of type directory, ls shall write the names of files contained within the directory as well as any requested, associated information. If one of the -d, -F, or -l options are specified, and one of the -H or -L options are not specified, for each operand that names a file of type symbolic link to a directory, ls shall write the name of the file as well as any requested, associated information. If none of the -d, -F, or -l options are specified, or the -H or -L options are specified, for each operand that names a file of type symbolic link to a directory, ls shall write the names of files contained within the directory as well as any requested, associated information.
If no operands are specified, ls shall write the contents of the current directory. If more than one operand is specified, ls shall write non-directory operands first; it shall sort directory and non-directory operands separately according to the collating sequence in the current locale.
The ls utility shall detect infinite loops; that is, entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop, ls shall write a diagnostic message to standard error and shall either recover its position in the hierarchy or terminate.
The ls utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
The following options shall be supported:
Specifying more than one of the options in the following mutually-exclusive pairs shall not be considered an error: -C and -l (ell), -m and -l (ell), -x and -l (ell), -C and -1 (one), -H and -L, -c and -u. The last option specified in each pair shall determine the output format.
The following operand shall be supported:
The following environment variables shall affect the execution of ls:
Determine the locale for character collation information in determining the pathname collation sequence.
The default format shall be to list one entry per line to standard output; the exceptions are to terminals or when one of the -C, -m, or -x options is specified. If the output is to a terminal, the format is implementation-defined.
When -m is specified, the format used shall be:
"%s, %s, ...\n", <filename1>, <filename2>
where the largest number of filenames shall be written without exceeding the length of the line.
If the -i option is specified, the file's file serial number (see <sys/stat.h>) shall be written in the following format before any other output for the corresponding entry:
%u ", <file serial number>
If the -l option is specified without -L, the following information shall be written:
"%s %u %s %s %u %s %s\n", <file mode>, <number of links>, <owner name>, <group name>, <number of bytes in the file>, <date and time>, <pathname>
If the file is a symbolic link, this information shall be about the link itself and the <pathname> field shall be of the form:
"%s -> %s", <pathname of link>, <contents of link>
If both -l and -L are specified, the following information shall be written:
"%s %u %s %s %u %s %s\n", <file mode>, <number of links>, <owner name>, <group name>, <number of bytes in the file>, <date and time>, <pathname of link>
where all fields except <pathname of link> shall be for the file resolved from the symbolic link.
The -g, -n, and -o options use the same format as -l, but with omitted items and their associated <blank>s. See the OPTIONS section.
In both the preceding -l forms, if <owner name> or <group name> cannot be determined, or if -n is given, they shall be replaced with their associated numeric values using the format %u .
The <date and time> field shall contain the appropriate date and timestamp of when the file was last modified. In the POSIX locale, the field shall be the equivalent of the output of the following date command:
date "+%b %e %H:%M"
if the file has been modified in the last six months, or:
date "+%b %e %Y"
(where two <space>s are used between %e and %Y ) if the file has not been modified in the last six months or if the modification date is in the future, except that, in both cases, the final <newline> produced by date shall not be included and the output shall be as if the date command were executed at the time of the last modification date of the file rather than the current time. When the LC_TIME locale category is not set to the POSIX locale, a different format and order of presentation of this field may be used.
If the file is a character special or block special file, the size of the file may be replaced with implementation-defined information associated with the device in question.
If the pathname was specified as a file operand, it shall be written as specified.
The file mode written under the -l, -g, -n, and -o options shall consist of the following format:
"%c%s%s%s%c", <entry type>, <owner permissions>, <group permissions>, <other permissions>, <optional alternate access method flag>
The <optional alternate access method flag> shall be a single <space> if there is no alternate or additional access control method associated with the file; otherwise, a printable character shall be used.
The <entry type> character shall describe the type of file, as follows:
Implementations may add other characters to this list to represent other implementation-defined file types.
The next three fields shall be three characters each:
Permissions for the file owner class (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 4.4, File Access Permissions).
Permissions for the file group class.
Permissions for the file other class.
Each field shall have three character positions:
Implementations may add other characters to this list for the third character position. Such additions shall, however, be written in lowercase if the file is executable or searchable, and in uppercase if it is not.
If any of the -l, -g, -n, -o, or -s options is specified, each list of files within the directory shall be preceded by a status line indicating the number of file system blocks occupied by files in the directory in 512-byte units, rounded up to the next integral number of units, if necessary. In the POSIX locale, the format shall be:
"total %u\n", <number of units in the directory>
If more than one directory, or a combination of non-directory files and directories are written, either as a result of specifying multiple operands, or the -R option, each list of files within a directory shall be preceded by:
"\n%s:\n", <directory name>
If this string is the first thing to be written, the first <newline> shall not be written. This output shall precede the number of units in the directory.
If the -s option is given, each file shall be written with the number of blocks used by the file. Along with -C, -1, -m, or -x, the number and a <space> shall precede the filename; with -g, -l, -n, or -o, they shall precede each line describing a file.
The standard error shall be used only for diagnostic messages.
The following exit values shall be returned:
Default.
The following sections are informative.
Many implementations use the equal sign ( '=' ) to denote sockets bound to the file system for the -F option. Similarly, many historical implementations use the 's' character to denote sockets as the entry type characters for the -l option.
It is difficult for an application to use every part of the file modes field of ls -l in a portable manner. Certain file types and executable bits are not guaranteed to be exactly as shown, as implementations may have extensions. Applications can use this field to pass directly to a user printout or prompt, but actions based on its contents should generally be deferred, instead, to the test utility.
The output of ls (with the -l and related options) contains information that logically could be used by utilities such as chmod and touch to restore files to a known state. However, this information is presented in a format that cannot be used directly by those utilities or be easily translated into a format that can be used. A character has been added to the end of the permissions string so that applications at least have an indication that they may be working in an area they do not understand instead of assuming that they can translate the permissions string into something that can be used. Future issues or related documents may define one or more specific characters to be used based on different standard additional or alternative access control mechanisms.
As with many of the utilities that deal with filenames, the output of ls for multiple files or in one of the long listing formats must be used carefully on systems where filenames can contain embedded white space. Systems and system administrators should institute policies and user training to limit the use of such filenames.
The number of disk blocks occupied by the file that it reports varies depending on underlying file system type, block size units reported, and the method of calculating the number of blocks. On some file system types, the number is the actual number of blocks occupied by the file (counting indirect blocks and ignoring holes in the file); on others it is calculated based on the file size (usually making an allowance for indirect blocks, but ignoring holes).
An example of a small directory tree being fully listed with ls -laRF a in the POSIX locale:
total 11 drwxr-xr-x 3 hlj prog 64 Jul 4 12:07 ./ drwxrwxrwx 4 hlj prog 3264 Jul 4 12:09 ../ drwxr-xr-x 2 hlj prog 48 Jul 4 12:07 b/ -rwxr--r-- 1 hlj prog 572 Jul 4 12:07 foo* a/b: total 4 drwxr-xr-x 2 hlj prog 48 Jul 4 12:07 ./ drwxr-xr-x 3 hlj prog 64 Jul 4 12:07 ../ -rw-r--r-- 1 hlj prog 700 Jul 4 12:07 bar
Some historical implementations of the ls utility show all entries in a directory except dot and dot-dot when a superuser invokes ls without specifying the -a option. When "normal" users invoke ls without specifying -a, they should not see information about any files with names beginning with a period unless they were named as file operands.
Implementations are expected to traverse arbitrary depths when processing the -R option. The only limitation on depth should be based on running out of physical storage for keeping track of untraversed directories.
The -1 (one) option was historically found in BSD and BSD-derived implementations only. It is required in this volume of IEEE Std 1003.1-2001 so that conforming applications might ensure that output is one entry per line, even if the output is to a terminal.
Generally, this volume of IEEE Std 1003.1-2001 is silent about what happens when options are given multiple times. In the cases of -C, -l, and -1, however, it does specify the results of these overlapping options. Since ls is one of the most aliased commands, it is important that the implementation perform intuitively. For example, if the alias were:
alias ls="ls -C"
and the user typed ls -1, single-text-column output should result, not an error.
The BSD ls provides a -A option (like -a, but dot and dot-dot are not written out). The small difference from -a did not seem important enough to require both.
Implementations may make -q the default for terminals to prevent trojan horse attacks on terminals with special escape sequences. This is not required because:
An early proposal specified that the optional alternate access method flag had to be '+' if there was an alternate access method used on the file or <space> if there was not. This was changed to be <space> if there is not and a single printable character if there is. This was done for three reasons:
Nonetheless, implementations providing a single alternate access method are encouraged to use '+' .
In an early proposal, the units used to specify the number of blocks occupied by files in a directory in an ls -l listing were implementation-defined. This was because BSD systems have historically used 1024-byte units and System V systems have historically used 512-byte units. It was pointed out by BSD developers that their system has used 512-byte units in some places and 1024-byte units in other places. (System V has consistently used 512.) Therefore, this volume of IEEE Std 1003.1-2001 usually specifies 512. Future releases of BSD are expected to consistently provide 512 bytes as a default with a way of specifying 1024-byte units where appropriate.
The <date and time> field in the -l format is specified only for the POSIX locale. As noted, the format can be different in other locales. No mechanism for defining this is present in this volume of IEEE Std 1003.1-2001, as the appropriate vehicle is a messaging system; that is, the format should be specified as a "message".
The -s uses implementation-defined units and cannot be used portably; it may be withdrawn in a future version.
chmod() , find , the System Interfaces volume of IEEE Std 1003.1-2001, stat(), the Base Definitions volume of IEEE Std 1003.1-2001, <sys/stat.h>