1LS(1P) POSIX Programmer's Manual LS(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 ls — list directory contents
14
16 ls [−ikqrs] [−glno] [−A|−a] [−C|−m|−x|−1] \
17 [−F|−p] [−H|−L] [−R|−d] [−S|−f|−t] [−c|−u] [file...]
18
20 For each operand that names a file of a type other than directory or
21 symbolic link to a directory, ls shall write the name of the file as
22 well as any requested, associated information. For each operand that
23 names a file of type directory, ls shall write the names of files con‐
24 tained within the directory as well as any requested, associated infor‐
25 mation. Filenames beginning with a <period> ('.') and any associated
26 information shall not be written out unless explicitly referenced, the
27 −A or −a option is supplied, or an implementation-defined condition
28 causes them to be written. If one or more of the −d, −F, or −l options
29 are specified, and neither the −H nor the −L option is specified, for
30 each operand that names a file of type symbolic link to a directory, ls
31 shall write the name of the file as well as any requested, associated
32 information. If none of the −d, −F, or −l options are specified, or the
33 −H or −L options are specified, for each operand that names a file of
34 type symbolic link to a directory, ls shall write the names of files
35 contained within the directory as well as any requested, associated
36 information. In each case where the names of files contained within a
37 directory are written, if the directory contains any symbolic links
38 then ls shall evaluate the file information and file type to be those
39 of the symbolic link itself, unless the −L option is specified.
40
41 If no operands are specified, ls shall behave as if a single operand of
42 dot ('.') had been specified. If more than one operand is specified,
43 ls shall write non-directory operands first; it shall sort directory
44 and non-directory operands separately according to the collating
45 sequence in the current locale.
46
47 The ls utility shall detect infinite loops; that is, entering a previ‐
48 ously visited directory that is an ancestor of the last file encoun‐
49 tered. When it detects an infinite loop, ls shall write a diagnostic
50 message to standard error and shall either recover its position in the
51 hierarchy or terminate.
52
54 The ls utility shall conform to the Base Definitions volume of
55 POSIX.1‐2008, Section 12.2, Utility Syntax Guidelines.
56
57 The following options shall be supported:
58
59 −A Write out all directory entries, including those whose names
60 begin with a <period> ('.') but excluding the entries dot
61 and dot-dot (if they exist).
62
63 −C Write multi-text-column output with entries sorted down the
64 columns, according to the collating sequence. The number of
65 text columns and the column separator characters are unspeci‐
66 fied, but should be adapted to the nature of the output
67 device. This option disables long format output.
68
69 −F Do not follow symbolic links named as operands unless the −H
70 or −L options are specified. Write a <slash> ('/') immedi‐
71 ately after each pathname that is a directory, an <asterisk>
72 ('*') after each that is executable, a <vertical-line> ('|')
73 after each that is a FIFO, and an at-sign ('@') after each
74 that is a symbolic link. For other file types, other symbols
75 may be written.
76
77 −H Evaluate the file information and file type for symbolic
78 links specified on the command line to be those of the file
79 referenced by the link, and not the link itself; however, ls
80 shall write the name of the link itself and not the file ref‐
81 erenced by the link.
82
83 −L Evaluate the file information and file type for all symbolic
84 links (whether named on the command line or encountered in a
85 file hierarchy) to be those of the file referenced by the
86 link, and not the link itself; however, ls shall write the
87 name of the link itself and not the file referenced by the
88 link. When −L is used with −l, write the contents of symbolic
89 links in the long format (see the STDOUT section).
90
91 −R Recursively list subdirectories encountered. When a symbolic
92 link to a directory is encountered, the directory shall not
93 be recursively listed unless the −L option is specified. The
94 use of −R with −d or −f produces unspecified results.
95
96 −S Sort with the primary key being file size (in decreasing
97 order) and the secondary key being filename in the collating
98 sequence (in increasing order).
99
100 −a Write out all directory entries, including those whose names
101 begin with a <period> ('.').
102
103 −c Use time of last modification of the file status information
104 (see the Base Definitions volume of POSIX.1‐2008,
105 <sys_stat.h>) instead of last modification of the file itself
106 for sorting (−t) or writing (−l).
107
108 −d Do not follow symbolic links named as operands unless the −H
109 or −L options are specified. Do not treat directories differ‐
110 ently than other types of files. The use of −d with −R or −f
111 produces unspecified results.
112
113 −f List the entries in directory operands in the order they
114 appear in the directory. The behavior for non-directory oper‐
115 ands is unspecified. This option shall turn on −a. When −f
116 is specified, any occurrences of the −r, −S, and −t options
117 shall be ignored and any occurrences of the −A, −g, −l, −n,
118 −o, and −s options may be ignored. The use of −f with −R or
119 −d produces unspecified results.
120
121 −g Turn on the −l (ell) option, but disable writing the file's
122 owner name or number. Disable the −C, −m, and −x options.
123
124 −i For each file, write the file's file serial number (see
125 stat() in the System Interfaces volume of POSIX.1‐2008).
126
127 −k Set the block size for the −s option and the per-directory
128 block count written for the −l, −n, −s, −g, and −o options
129 (see the STDOUT section) to 1024 bytes.
130
131 −l (The letter ell.) Do not follow symbolic links named as oper‐
132 ands unless the −H or −L options are specified. Write out in
133 long format (see the STDOUT section). Disable the −C, −m, and
134 −x options.
135
136 −m Stream output format; list pathnames across the page, sepa‐
137 rated by a <comma> character followed by a <space> character.
138 Use a <newline> character as the list terminator and after
139 the separator sequence when there is not room on a line for
140 the next list entry. This option disables long format output.
141
142 −n Turn on the −l (ell) option, but when writing the file's
143 owner or group, write the file's numeric UID or GID rather
144 than the user or group name, respectively. Disable the −C,
145 −m, and −x options.
146
147 −o Turn on the −l (ell) option, but disable writing the file's
148 group name or number. Disable the −C, −m, and −x options.
149
150 −p Write a <slash> ('/') after each filename if that file is a
151 directory.
152
153 −q Force each instance of non-printable filename characters and
154 <tab> characters to be written as the <question-mark> ('?')
155 character. Implementations may provide this option by default
156 if the output is to a terminal device.
157
158 −r Reverse the order of the sort to get reverse collating
159 sequence oldest first, or smallest file size first depending
160 on the other options given.
161
162 −s Indicate the total number of file system blocks consumed by
163 each file displayed. If the −k option is also specified, the
164 block size shall be 1024 bytes; otherwise, the block size is
165 implementation-defined.
166
167 −t Sort with the primary key being time modified (most recently
168 modified first) and the secondary key being filename in the
169 collating sequence. For a symbolic link, the time used as
170 the sort key is that of the symbolic link itself, unless ls
171 is evaluating its file information to be that of the file
172 referenced by the link (see the −H and −L options).
173
174 −u Use time of last access (see the Base Definitions volume of
175 POSIX.1‐2008, <sys_stat.h>) instead of last modification of
176 the file for sorting (−t) or writing (−l).
177
178 −x The same as −C, except that the multi-text-column output is
179 produced with entries sorted across, rather than down, the
180 columns. This option disables long format output.
181
182 −1 (The numeric digit one.) Force output to be one entry per
183 line. This option does not disable long format output. (Long
184 format output is enabled by −g, −l (ell), −n, and −o; and
185 disabled by −C, −m, and −x.)
186
187 If an option that enables long format output (−g, −l (ell), −n, and −o
188 is given with an option that disables long format output (−C, −m, and
189 −x), this shall not be considered an error. The last of these options
190 specified shall determine whether long format output is written.
191
192 If −R, −d, or −f are specified, the results of specifying these mutu‐
193 ally-exclusive options are specified by the descriptions of these
194 options above. If more than one of any of the other options shown in
195 the SYNOPSIS section in mutually-exclusive sets are given, this shall
196 not be considered an error; the last option specified in each set shall
197 determine the output.
198
199 Note that if −t is specified, −c and −u are not only mutually-exclusive
200 with each other, they are also mutually-exclusive with −S when deter‐
201 mining sort order. But even if −S is specified after all occurrences of
202 −c, −t, and −u, the last use of −c or −u determines the timestamp
203 printed when producing long format output.
204
206 The following operand shall be supported:
207
208 file A pathname of a file to be written. If the file specified is
209 not found, a diagnostic message shall be output on standard
210 error.
211
213 Not used.
214
216 None.
217
219 The following environment variables shall affect the execution of ls:
220
221 COLUMNS Determine the user's preferred column position width for
222 writing multiple text-column output. If this variable con‐
223 tains a string representing a decimal integer, the ls utility
224 shall calculate how many pathname text columns to write (see
225 −C) based on the width provided. If COLUMNS is not set or
226 invalid, an implementation-defined number of column positions
227 shall be assumed, based on the implementation's knowledge of
228 the output device. The column width chosen to write the names
229 of files in any given directory shall be constant. Filenames
230 shall not be truncated to fit into the multiple text-column
231 output.
232
233 LANG Provide a default value for the internationalization vari‐
234 ables that are unset or null. (See the Base Definitions vol‐
235 ume of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
236 ables for the precedence of internationalization variables
237 used to determine the values of locale categories.)
238
239 LC_ALL If set to a non-empty string value, override the values of
240 all the other internationalization variables.
241
242 LC_COLLATE
243 Determine the locale for character collation information in
244 determining the pathname collation sequence.
245
246 LC_CTYPE Determine the locale for the interpretation of sequences of
247 bytes of text data as characters (for example, single-byte as
248 opposed to multi-byte characters in arguments) and which
249 characters are defined as printable (character class print).
250
251 LC_MESSAGES
252 Determine the locale that should be used to affect the format
253 and contents of diagnostic messages written to standard
254 error.
255
256 LC_TIME Determine the format and contents for date and time strings
257 written by ls.
258
259 NLSPATH Determine the location of message catalogs for the processing
260 of LC_MESSAGES.
261
262 TZ Determine the timezone for date and time strings written by
263 ls. If TZ is unset or null, an unspecified default timezone
264 shall be used.
265
267 Default.
268
270 The default format shall be to list one entry per line to standard out‐
271 put; the exceptions are to terminals or when one of the −C, −m, or −x
272 options is specified. If the output is to a terminal, the format is
273 implementation-defined.
274
275 When −m is specified, the format used for the last element of the list
276 shall be:
277
278 "%s\n", <filename>
279
280 The format used for each other element of the list shall be:
281
282 "%s,%s", <filename>, <separator>
283
284 where, if there is not room for the next element of the list to fit
285 within the current line length, <separator> is a string containing an
286 optional <space> character and a mandatory <newline> character; other‐
287 wise it is a single <space> character.
288
289 If the −i option is specified, the file's file serial number (see the
290 Base Definitions volume of POSIX.1‐2008, <sys_stat.h>) shall be written
291 in the following format before any other output for the corresponding
292 entry:
293
294 %u ", <file serial number>
295
296 If the −l option is specified, the following information shall be writ‐
297 ten for files other than character special and block special files:
298
299 "%s %u %s %s %u %s %s\n", <file mode>, <number of links>,
300 <owner name>, <group name>, <size>, <date and time>,
301 <pathname>
302
303 If the −l option is specified, the following information shall be writ‐
304 ten for character special and block special files:
305
306 "%s %u %s %s %s %s %s\n", <file mode>, <number of links>,
307 <owner name>, <group name>, <device info>, <date and time>,
308 <pathname>
309
310 In both cases if the file is a symbolic link and the −L option is also
311 specified, this information shall be for the file resolved from the
312 symbolic link, except that the <pathname> field shall contain the path‐
313 name of the symbolic link itself. If the file is a symbolic link and
314 the −L option is not specified, this information shall be about the
315 link itself and the <pathname> field shall be of the form:
316
317 "%s −> %s", <pathname of link>, <contents of link>
318
319 The −n, −g, and −o options use the same format as −l, but with omitted
320 items and their associated <blank> characters. See the OPTIONS section.
321
322 In both the preceding −l forms, if <owner name> or <group name> cannot
323 be determined, or if −n is given, they shall be replaced with their
324 associated numeric values using the format %u.
325
326 The <size> field shall contain the value that would be returned for the
327 file in the st_size field of struct stat (see the Base Definitions vol‐
328 ume of POSIX.1‐2008, <sys_stat.h>). Note that for some file types this
329 value is unspecified.
330
331 The <device info> field shall contain implementation-defined informa‐
332 tion associated with the device in question.
333
334 The <date and time> field shall contain the appropriate date and time‐
335 stamp of when the file was last modified. In the POSIX locale, the
336 field shall be the equivalent of the output of the following date com‐
337 mand:
338
339 date "+%b %e %H:%M"
340
341 if the file has been modified in the last six months, or:
342
343 date "+%b %e %Y"
344
345 (where two <space> characters are used between %e and %Y) if the file
346 has not been modified in the last six months or if the modification
347 date is in the future, except that, in both cases, the final <newline>
348 produced by date shall not be included and the output shall be as if
349 the date command were executed at the time of the last modification
350 date of the file rather than the current time. When the LC_TIME locale
351 category is not set to the POSIX locale, a different format and order
352 of presentation of this field may be used.
353
354 If the pathname was specified as a file operand, it shall be written as
355 specified.
356
357 The file mode written under the −l, −n, −g, and −o options shall con‐
358 sist of the following format:
359
360 "%c%s%s%s%s", <entry type>, <owner permissions>,
361 <group permissions>, <other permissions>,
362 <optional alternate access method flag>
363
364 The <optional alternate access method flag> shall be the empty string
365 if there is no alternate or additional access control method associated
366 with the file; otherwise, it shall be a string containing a single
367 printable character that is not a <blank>.
368
369 The <entry type> character shall describe the type of file, as follows:
370
371 d Directory.
372
373 b Block special file.
374
375 c Character special file.
376
377 l (ell) Symbolic link.
378
379 p FIFO.
380
381 − Regular file.
382
383 Implementations may add other characters to this list to represent
384 other implementation-defined file types.
385
386 The next three fields shall be three characters each:
387
388 <owner permissions>
389 Permissions for the file owner class (see the Base Definitions
390 volume of POSIX.1‐2008, Section 4.4, File Access Permissions).
391
392 <group permissions>
393 Permissions for the file group class.
394
395 <other permissions>
396 Permissions for the file other class.
397
398 Each field shall have three character positions:
399
400 1. If 'r', the file is readable; if '−', the file is not readable.
401
402 2. If 'w', the file is writable; if '−', the file is not writable.
403
404 3. The first of the following that applies:
405
406 S If in <owner permissions>, the file is not executable and
407 set-user-ID mode is set. If in <group permissions>, the file
408 is not executable and set-group-ID mode is set.
409
410 s If in <owner permissions>, the file is executable and set-
411 user-ID mode is set. If in <group permissions>, the file is
412 executable and set-group-ID mode is set.
413
414 T If in <other permissions> and the file is a directory, search
415 permission is not granted to others, and the restricted dele‐
416 tion flag is set.
417
418 t If in <other permissions> and the file is a directory, search
419 permission is granted to others, and the restricted deletion
420 flag is set.
421
422 x The file is executable or the directory is searchable.
423
424 − None of the attributes of 'S', 's', 'T', 't', or 'x' applies.
425
426 Implementations may add other characters to this list for the third
427 character position. Such additions shall, however, be written in
428 lowercase if the file is executable or searchable, and in uppercase
429 if it is not.
430
431 If any of the −l, −n, −s, −g, or −o options is specified, each list of
432 files within the directory shall be preceded by a status line indicat‐
433 ing the number of file system blocks occupied by files in the directory
434 in 512-byte units if the −k option is not specified, or 1024-byte units
435 if the −k option is specified, rounded up to the next integral number
436 of units, if necessary. In the POSIX locale, the format shall be:
437
438 "total %u\n", <number of units in the directory>
439
440 If more than one directory, or a combination of non-directory files and
441 directories are written, either as a result of specifying multiple op‐
442 erands, or the −R option, each list of files within a directory shall
443 be preceded by:
444
445 "\n%s:\n", <directory name>
446
447 If this string is the first thing to be written, the first <newline>
448 shall not be written. This output shall precede the number of units in
449 the directory.
450
451 If the −s option is given, each file shall be written with the number
452 of blocks used by the file. Along with −C, −1, −m, or −x, the number
453 and a <space> shall precede the filename; with −l, −n, −g, or −o, they
454 shall precede each line describing a file.
455
457 The standard error shall be used only for diagnostic messages.
458
460 None.
461
463 None.
464
466 The following exit values shall be returned:
467
468 0 Successful completion.
469
470 >0 An error occurred.
471
473 Default.
474
475 The following sections are informative.
476
478 Many implementations use the <equals-sign> ('=') to denote sockets
479 bound to the file system for the −F option. Similarly, many historical
480 implementations use the 's' character to denote sockets as the entry
481 type characters for the −l option.
482
483 It is difficult for an application to use every part of the file modes
484 field of ls −l in a portable manner. Certain file types and executable
485 bits are not guaranteed to be exactly as shown, as implementations may
486 have extensions. Applications can use this field to pass directly to a
487 user printout or prompt, but actions based on its contents should gen‐
488 erally be deferred, instead, to the test utility.
489
490 The output of ls (with the −l and related options) contains information
491 that logically could be used by utilities such as chmod and touch to
492 restore files to a known state. However, this information is presented
493 in a format that cannot be used directly by those utilities or be eas‐
494 ily translated into a format that can be used. A character has been
495 added to the end of the permissions string so that applications at
496 least have an indication that they may be working in an area they do
497 not understand instead of assuming that they can translate the permis‐
498 sions string into something that can be used. Future versions or
499 related documents may define one or more specific characters to be used
500 based on different standard additional or alternative access control
501 mechanisms.
502
503 As with many of the utilities that deal with filenames, the output of
504 ls for multiple files or in one of the long listing formats must be
505 used carefully on systems where filenames can contain embedded white
506 space. Systems and system administrators should institute policies and
507 user training to limit the use of such filenames.
508
509 The number of disk blocks occupied by the file that it reports varies
510 depending on underlying file system type, block size units reported,
511 and the method of calculating the number of blocks. On some file system
512 types, the number is the actual number of blocks occupied by the file
513 (counting indirect blocks and ignoring holes in the file); on others it
514 is calculated based on the file size (usually making an allowance for
515 indirect blocks, but ignoring holes).
516
518 An example of a small directory tree being fully listed with ls −laRF a
519 in the POSIX locale:
520
521 total 11
522 drwxr-xr-x 3 fox prog 64 Jul 4 12:07 ./
523 drwxrwxrwx 4 fox prog 3264 Jul 4 12:09 ../
524 drwxr-xr-x 2 fox prog 48 Jul 4 12:07 b/
525 -rwxr--r-- 1 fox prog 572 Jul 4 12:07 foo*
526
527 a/b:
528 total 4
529 drwxr-xr-x 2 fox prog 48 Jul 4 12:07 ./
530 drwxr-xr-x 3 fox prog 64 Jul 4 12:07 ../
531 -rw-r--r-- 1 fox prog 700 Jul 4 12:07 bar
532
534 Some historical implementations of the ls utility show all entries in a
535 directory except dot and dot-dot when a superuser invokes ls without
536 specifying the −a option. When ``normal'' users invoke ls without spec‐
537 ifying −a, they should not see information about any files with names
538 beginning with a <period> unless they were named as file operands.
539
540 Implementations are expected to traverse arbitrary depths when process‐
541 ing the −R option. The only limitation on depth should be based on run‐
542 ning out of physical storage for keeping track of untraversed directo‐
543 ries.
544
545 The −1 (one) option was historically found in BSD and BSD-derived
546 implementations only. It is required in this volume of POSIX.1‐2008 so
547 that conforming applications might ensure that output is one entry per
548 line, even if the output is to a terminal.
549
550 The −S option was added in Issue 7, but had been provided by several
551 implementations for many years. The description given in the standard
552 documents historic practice, but does not match much of the documenta‐
553 tion that described its behavior. Historical documentation typically
554 described it as something like:
555
556 −S Sort by size (largest size first) instead of by name. Special
557 character devices (listed last) are sorted by name.
558
559 even though the file type was never considered when sorting the output.
560 Character special files do typically sort close to the end of the list
561 because their file size on most implementations is zero. But they are
562 sorted alphabetically with any other files that happen to have the same
563 file size (zero), not sorted separately and added to the end.
564
565 This volume of POSIX.1‐2008 is frequently silent about what happens
566 when mutually-exclusive options are specified. Except for −R, −d, and
567 −f, the ls utility is required to accept multiple options from each
568 mutually-exclusive option set without treating them as errors and to
569 use the behavior specified by the last option given in each mutually-
570 exclusive set. Since ls is one of the most aliased commands, it is
571 important that the implementation perform intuitively. For example, if
572 the alias were:
573
574 alias ls="ls −C"
575
576 and the user typed ls −1 (one), single-text-column output should
577 result, not an error.
578
579 The −g, −l (ell), −n, and −o options are not mutually-exclusive
580 options. They all enable long format output. They work together to
581 determine whether the file's owner is written (no if −g is present),
582 file's group is written (no if −o is present), and if the file's group
583 or owner is written whether it is written as the name (default) or a
584 string representation of the UID or GID number (if −n is present). The
585 −C, −m, −x, and −1 (one) are mutually-exclusive options and the first
586 three of these disable long format output. The −1 (one) option does not
587 directly change whether or not long format output is enabled, but by
588 overriding −C, −m, and −x, it can re-enable long format output that had
589 been disabled by one of these options.
590
591 Earlier versions of this standard did not describe the BSD −A option
592 (like −a, but dot and dot-dot are not written out). It has been added
593 due to widespread implementation.
594
595 Implementations may make −q the default for terminals to prevent trojan
596 horse attacks on terminals with special escape sequences. This is not
597 required because:
598
599 * Some control characters may be useful on some terminals; for exam‐
600 ple, a system might write them as "\001" or "^A".
601
602 * Special behavior for terminals is not relevant to applications
603 portability.
604
605 An early proposal specified that the <optional alter‐
606 nate access method flag> had to be '+' if there was an alternate access
607 method used on the file or <space> if there was not. This was changed
608 to be <space> if there is not and a single printable character if there
609 is. This was done for three reasons:
610
611 1. There are historical implementations using characters other than
612 '+'.
613
614 2. There are implementations that vary this character used in that
615 position to distinguish between various alternate access methods in
616 use.
617
618 3. The standard developers did not want to preclude future specifica‐
619 tions that might need a way to specify more than one alternate
620 access method.
621
622 Nonetheless, implementations providing a single alternate access method
623 are encouraged to use '+'.
624
625 Earlier versions of this standard did not have the −k option, which
626 meant that the −s option could not be used portably as its block size
627 was implementation-defined, and the units used to specify the number of
628 blocks occupied by files in a directory in an ls −l listing were fixed
629 as 512-byte units. The −k option has been added to provide a way for
630 the −s option to be used portably, and for consistency it also changes
631 the aforementioned units from 512-byte to 1024-byte.
632
633 The <date and time> field in the −l format is specified only for the
634 POSIX locale. As noted, the format can be different in other locales.
635 No mechanism for defining this is present in this volume of
636 POSIX.1‐2008, as the appropriate vehicle is a messaging system; that
637 is, the format should be specified as a ``message''.
638
640 Allowing −f to ignore the −A, −g, −l, −n, −o, and −s options may be
641 removed in a future version.
642
644 chmod, find
645
646 The Base Definitions volume of POSIX.1‐2008, Section 4.4, File Access
647 Permissions, Chapter 8, Environment Variables, Section 12.2, Utility
648 Syntax Guidelines, <sys_stat.h>
649
650 The System Interfaces volume of POSIX.1‐2008, fstatat()
651
653 Portions of this text are reprinted and reproduced in electronic form
654 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
655 -- Portable Operating System Interface (POSIX), The Open Group Base
656 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
657 cal and Electronics Engineers, Inc and The Open Group. (This is
658 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
659 event of any discrepancy between this version and the original IEEE and
660 The Open Group Standard, the original IEEE and The Open Group Standard
661 is the referee document. The original Standard can be obtained online
662 at http://www.unix.org/online.html .
663
664 Any typographical or formatting errors that appear in this page are
665 most likely to have been introduced during the conversion of the source
666 files to man page format. To report such errors, see https://www.ker‐
667 nel.org/doc/man-pages/reporting_bugs.html .
668
669
670
671IEEE/The Open Group 2013 LS(1P)