ls - list directory contents
For each operand that names a file of a type other than directory, ls writes the name of the file as well as any requested, associated information. For each operand that names a file of type directory, ls writes the names of files contained within that directory, as well as any requested, associated information.
If no operands are specified, the contents of the current directory are written. If more than one operand is specified, non-directory operands are written first; directory and non-directory operands are sorted separately according to the collating sequence in the current locale.
The ls utility supports the XBD specification, Utility Syntax Guidelines .
The following options are supported:
- Write multi-text-column output with entries sorted down the columns, according to the collating sequence. The number of text columns and the column separator characters are unspecified, but should be adapted to the nature of the output device.
- Write a slash (/) immediately after each pathname that is a directory, an asterisk (*) after each that is executable, and a vertical bar (|) after each that is a FIFO. For other file types, other symbols may be written.
- Recursively list subdirectories encountered.
- Write out all directory entries, including those whose names begin with a period (.). Entries beginning with a period will not be written out unless explicitly referenced, the -a option is supplied, or an implementation-dependent condition causes them to be written.
- Use time of last modification of the file status information (see <sys/stat.h> in the XSH specification) instead of last modification of the file itself for sorting (-t) or writing (-l).
- Do not treat directories differently from other types of files. The use of -d with -R produces unspecified results.
- Force each argument to be interpreted as a directory and list the name found in each slot. This option turns off -l, -t, -s and -r, and turns on -a; the order is the order in which entries appear in the directory.
- The same as -l, except that the owner is not written.
- For each file, write the file's file serial number (see stat() in the XSH specification).
- (The letter ell.) Write out in long format (see the STDOUT section). When -l (ell) is specified, -1 (one) is assumed.
- Stream output format; list files across the page, separated by commas.
- The same as -l, except that the owner's UID and GID numbers are written, rather than the associated character strings.
- The same as -l, except that the group is not written.
- Write a slash (/) after each filename if that file is a directory.
- Force each instance of non-printable filename characters and tab characters to be written as the question-mark (?) character. Implementations may provide this option by default if the output is to a terminal device.
- Reverse the order of the sort to get reverse collating sequence or oldest first.
- Indicate the total number of file system blocks consumed by each file displayed. The block size is implementation-dependent.
- Sort by time modified (most recently modified first) before sorting the operands by the collating sequence.
- Use time of last access (see <sys/stat.h> in the XSH specification) instead of last modification of the file for sorting (-t) or writing (-l).
- The same as -C, except that the multi-text-column output is produced with entries sorted across, rather than down, the columns.
- (The numeric digit one.) Force output to be one entry per line.
Specifying more than one of the options in the following mutually exclusive pairs is not considered an error: -C and -l (ell), -m and -l (ell), -x and -l (ell), -C and -1 (one), -c and -u. The last option specified in each pair determines the output format.
The following operand is supported:
- A pathname of a file to be written. If the file specified is not found, a diagnostic message will be output on standard error.
The following environment variables affect the execution of ls:
- Determine the user's preferred column position width for writing multiple text-column output. If this variable contains a string representing a decimal integer, the ls utility calculates how many pathname text columns to write (see -C) based on the width provided. If COLUMNS is not set or invalid, an implementation-dependent number of column positions is assumed, based on the implementation's knowledge of the output device. The column width chosen to write the names of files in any given directory will be constant. Filenames will not be truncated to fit into the multiple text-column output.
- Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
- If set to a non-empty string value, override the values of all the other internationalisation variables.
- Determine the locale for character collation information in determining the pathname collation sequence.
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- versus multi-byte characters in arguments) and which characters are defined as printable (character class print).
- Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
- Determine the format and contents for date and time strings written by ls.
- Determine the location of message catalogues for the processing of LC_MESSAGES .
- Determine the timezone for date and time strings written by ls.
The default format is 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-dependent.
When -m is specified, the format used is:
'-1n' "%s, %s, ...\n", <filename1>, <filename2>where the largest number of filenames is written without exceeding the length of the line.
If the -i option is specified, the file's file serial number (see <sys/stat.h> in the XSH specification) is written in the following format before any other output for the corresponding entry:
"%u ", <file serial number>
If the -l option is specified, the following information will 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>
The -g, -n and -o options use the same format as -l, but with omitted items and their associated blank characters; see the OPTIONS section.
If <owner name> or <group name> cannot be determined, or if -n is given, they are replaced with their associated numeric values using the format %u.
The <date and time>, field will contain the appropriate date and timestamp of when the file was last modified. In the POSIX locale, the field is the equivalent of the output of the following date command:if the file has been modified in the last six months, or:
date "+%b %e %H:%M"(where two space characters 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 character produced by date is not included and the output is 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.
date "+%b %e %Y"
If the file is a character special or block special file, the size of the file may be replaced with implementation-dependent information associated with the device in question.
If the pathname was specified as a file operand, it will be written as specified.
The file mode written under the -l, -g, -n and -o options consists 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> is a single space character if there is no alternate or additional access control method associated with the file; otherwise, a printable character is used.
The <entry type> character describes the type of file, as follows:
- Block special file.
- Character special file.
- Regular file.
Implementations may add other characters to this list to represent other, implementation-dependent, file types.
The next three fields are three characters each:
- <owner permissions>
- Permissions for the file owner class (see file access permissions in the XBD specification, Glossary ).
- <group permissions>
- Permissions for the file group class.
- <other permissions>
- Permissions for the file other class.
Each field has three character positions:
- If r, the file is readable; if "-", it is not readable.
- If w, the file is writable; if "-", it is not writable.
- The first of the following that applies:
- If in <owner permissions>, the file is not executable and set-user-ID mode is set. If in <group permissions>, the file is not executable and set-group-ID mode is set.
- If in <owner permissions>, the file is executable and set-user-ID mode is set. If in <group permissions>, the file is executable and set-group-ID mode is set.
- The file is executable or the directory is searchable.
- None of the attributes of S, s or x applies.
Implementations may add other characters to this list for the third character position. Such additions will, however, be written in lower-case if the file is executable or searchable, and in upper-case if it is not.
If any of the -l, -g, -n, -o or -s options is specified, each list of files within the directory will 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 is:
"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 will be preceded by:
"\n%s:\n", <directory name>
If this string is the first thing to be written, the first newline character is not written. This output precedes 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 character precede the filename; with -g, -l, -n or -o, they precede each line describing a file.
Used only for diagnostic messages.
The following exit values are returned:
- All files were written successfully.
- An error occurred.
Many implementations use the equal sign (=) and the at sign (@) to denote sockets bound to the file system and symbolic links, respectively, for the -F option. Similarly, many historical implementations use the s character and the l character to denote sockets and symbolic links, respectively, 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 will 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
The -s uses implementation-dependent units and cannot be used portably; it may be withdrawn in a future issue.
The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about parts of this interface definition to the IEEE PASC Shell and Utilities Working Group which is identifying the corrections. A future revision of this specification will align with IEEE Std. 1003.2b when finalised.
chmod, find, the XSH specification description of <sys/stat.h>.