xref: /freebsd/bin/ls/ls.1 (revision 56b17de1e8360fe131d425de20b5e75ff3ea897c)
1.\"-
2.\" Copyright (c) 1980, 1990, 1991, 1993, 1994
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" the Institute of Electrical and Electronics Engineers, Inc.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd July 22, 2024
33.Dt LS 1
34.Os
35.Sh NAME
36.Nm ls
37.Nd list directory contents
38.Sh SYNOPSIS
39.Nm
40.Op Fl ABCFGHILPRSTUWZabcdfghiklmnopqrstuvwxy1\&,
41.Op Fl -color Ns = Ns Ar when
42.Op Fl D Ar format
43.Op Ar
44.Sh DESCRIPTION
45For each operand that names a
46.Ar file
47of a type other than
48directory,
49.Nm
50displays its name as well as any requested,
51associated information.
52For each operand that names a
53.Ar file
54of type directory,
55.Nm
56displays the names of files contained
57within that directory, as well as any requested, associated
58information.
59.Pp
60If no operands are given, the contents of the current
61directory are displayed.
62If more than one operand is given,
63non-directory operands are displayed first; directory
64and non-directory operands are sorted separately and in
65lexicographical order.
66.Pp
67The following options are available:
68.Bl -tag -width indent
69.It Fl A
70Include directory entries whose names begin with a
71dot
72.Pq Sq Pa \&.
73except for
74.Pa \&.
75and
76.Pa .. .
77Automatically set for the super-user unless
78.Fl I
79is specified.
80.It Fl B
81Force printing of non-printable characters (as defined by
82.Xr ctype 3
83and current locale settings) in file names as
84.Li \e Ns Va xxx ,
85where
86.Va xxx
87is the numeric value of the character in octal.
88This option is not defined in
89.St -p1003.1-2008 .
90.It Fl C
91Force multi-column output; this is the default when output is to a terminal.
92.It Fl D Ar format
93When printing in the long
94.Pq Fl l
95format, use
96.Ar format
97to format the date and time output.
98The argument
99.Ar format
100is a string used by
101.Xr strftime 3 .
102Depending on the choice of format string, this may result in a
103different number of columns in the output.
104This option overrides the
105.Fl T
106option.
107This option is not defined in
108.St -p1003.1-2008 .
109.It Fl F
110Display a slash
111.Pq Ql /
112immediately after each pathname that is a directory,
113an asterisk
114.Pq Ql *
115after each that is executable,
116an at sign
117.Pq Ql @
118after each symbolic link,
119an equals sign
120.Pq Ql =
121after each socket,
122a percent sign
123.Pq Ql %
124after each whiteout,
125and a vertical bar
126.Pq Ql \&|
127after each that is a
128.Tn FIFO .
129.It Fl G
130Enable colorized output.
131This option is equivalent to defining
132.Ev CLICOLOR
133or
134.Ev COLORTERM
135in the environment and setting
136.Fl -color Ns = Ns Ar auto .
137(See below.)
138This functionality can be compiled out by removing the definition of
139.Ev COLORLS .
140This option is not defined in
141.St -p1003.1-2008 .
142.It Fl H
143Symbolic links on the command line are followed.
144This option is assumed if
145none of the
146.Fl F , d ,
147or
148.Fl l
149options are specified.
150.It Fl I
151Prevent
152.Fl A
153from being automatically set for the super-user.
154This option is not defined in
155.St -p1003.1-2008 .
156.It Fl L
157If argument is a symbolic link, list the file or directory the link references
158rather than the link itself.
159This option cancels the
160.Fl P
161option.
162.It Fl P
163If argument is a symbolic link, list the link itself rather than the
164object the link references.
165This option cancels the
166.Fl H
167and
168.Fl L
169options.
170.It Fl R
171Recursively list subdirectories encountered.
172.It Fl S
173Sort by size (largest file first) before sorting the operands in
174lexicographical order.
175.It Fl T
176When printing in the long
177.Pq Fl l
178format, display complete time information for the file, including
179month, day, hour, minute, second, and year.
180The
181.Fl D
182option gives even more control over the output format.
183This option is not defined in
184.St -p1003.1-2008 .
185.It Fl U
186Use time when file was created for sorting or printing.
187This option is not defined in
188.St -p1003.1-2008 .
189.It Fl W
190Display whiteouts when scanning directories.
191This option is not defined in
192.St -p1003.1-2008 .
193.It Fl Z
194Display each file's MAC label; see
195.Xr maclabel 7 .
196This option is not defined in
197.St -p1003.1-2008 .
198.It Fl a
199Include directory entries whose names begin with a
200dot
201.Pq Sq Pa \&. .
202.It Fl b
203As
204.Fl B ,
205but use
206.Tn C
207escape codes whenever possible.
208This option is not defined in
209.St -p1003.1-2008 .
210.It Fl c
211Use time when file status was last changed for sorting or printing.
212.It Fl -color Ns = Ns Ar when
213Output colored escape sequences based on
214.Ar when ,
215which may be set to either
216.Cm always ,
217.Cm auto ,
218or
219.Cm never .
220.Pp
221.Cm always
222will make
223.Nm
224always output color.
225If
226.Ev TERM
227is unset or set to an invalid terminal, then
228.Nm
229will fall back to explicit
230.Tn ANSI
231escape sequences without the help of
232.Xr termcap 5 .
233.Cm always
234is the default if
235.Fl -color
236is specified without an argument.
237.Pp
238.Cm auto
239will make
240.Nm
241output escape sequences based on
242.Xr termcap 5 ,
243but only if
244.Dv stdout
245is a tty and either the
246.Fl G
247flag is specified or one of the environment variables
248.Ev COLORTERM
249or
250.Ev CLICOLOR
251is set and not empty.
252.Pp
253.Cm never
254will disable color regardless of environment variables.
255.Cm never
256is the default when neither
257.Fl -color
258nor
259.Fl G
260is specified.
261.Pp
262For compatibility with GNU coreutils,
263.Nm
264supports
265.Cm yes
266or
267.Cm force
268as equivalent to
269.Cm always ,
270.Cm no
271or
272.Cm none
273as equivalent to
274.Cm never ,
275and
276.Cm tty
277or
278.Cm if-tty
279as equivalent to
280.Cm auto .
281.It Fl d
282Directories are listed as plain files (not searched recursively).
283.It Fl f
284Output is not sorted.
285This option turns on
286.Fl a .
287It also negates the effect of the
288.Fl r ,
289.Fl S
290and
291.Fl t
292options.
293As allowed by
294.St -p1003.1-2008 ,
295this option has no effect on the
296.Fl d ,
297.Fl l ,
298.Fl R
299and
300.Fl s
301options.
302.It Fl g
303Display the long
304.Pq Fl l
305format output without the file owner's name or number.
306.It Fl h
307When used with the
308.Fl l
309option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte
310and Petabyte in order to reduce the number of digits to four or fewer
311using base 2 for sizes.
312This option is not defined in
313.St -p1003.1-2008 .
314.It Fl i
315For each file, print the file's file serial number (inode number).
316.It Fl k
317This has the same effect as setting environment variable
318.Ev BLOCKSIZE
319to 1024, except that it also nullifies any
320.Fl h
321options to its left.
322.It Fl l
323(The lowercase letter
324.Dq ell . )
325List files in the long format, as described in the
326.Sx The Long Format
327subsection below.
328.It Fl m
329Stream output format; list files across the page, separated by commas.
330.It Fl n
331Display user and group IDs numerically rather than converting to a user
332or group name in a long
333.Pq Fl l
334output.
335.It Fl o
336Include the file flags in a long
337.Pq Fl l
338output.
339This option is incompatible with
340.St -p1003.1-2008 .
341See
342.Xr chflags 1
343for a list of file flags and their meanings.
344.It Fl p
345Write a slash
346.Pq Ql /
347after each filename if that file is a directory.
348.It Fl q
349Force printing of non-graphic characters in file names as
350the character
351.Ql \&? ;
352this is the default when output is to a terminal.
353.It Fl r
354Reverse the order of the sort.
355.It Fl s
356Display the number of blocks used in the file system by each file.
357Block sizes and directory totals are handled as described in
358.Sx The Long Format
359subsection below, except (if the long format is not also requested)
360the directory totals are not output when the output is in a
361single column, even if multi-column output is requested.
362.It Fl t
363Sort by descending time modified (most recently modified first).
364If two files have the same modification timestamp, sort their names
365in ascending lexicographical order.
366The
367.Fl r
368option reverses both of these sort orders.
369.Pp
370Note that these sort orders are contradictory: the time sequence is in
371descending order, the lexicographical sort is in ascending order.
372This behavior is mandated by
373.St -p1003.2 .
374This feature can cause problems listing files stored with sequential names on
375FAT file systems, such as from digital cameras, where it is possible to have
376more than one image with the same timestamp.
377In such a case, the photos cannot be listed in the sequence in which
378they were taken.
379To ensure the same sort order for time and for lexicographical sorting, set the
380environment variable
381.Ev LS_SAMESORT
382or use the
383.Fl y
384option.
385This causes
386.Nm
387to reverse the lexicographical sort order when sorting files with the
388same modification timestamp.
389.It Fl u
390Use time of last access,
391instead of time of last modification
392of the file for sorting
393.Pq Fl t
394or printing
395.Pq Fl l .
396.It Fl v
397Sort following a natural ordering, using
398.Xr strverscmp 3
399instead of
400.Xr strcoll 3
401as the comparison function.
402E.g., files lexicographically ordered
403"bloem1", "bloem10", and "bloem9" would instead be ordered
404"bloem1", "bloem9", and "bloem10", as one would perhaps expect.
405.It Fl w
406Force raw printing of non-printable characters.
407This is the default
408when output is not to a terminal.
409This option is not defined in
410.St -p1003.1-2001 .
411.It Fl x
412The same as
413.Fl C ,
414except that the multi-column output is produced with entries sorted
415across, rather than down, the columns.
416.It Fl y
417When the
418.Fl t
419option is set, sort the alphabetical output in the same order as the time output.
420This has the same effect as setting
421.Ev LS_SAMESORT .
422See the description of the
423.Fl t
424option for more details.
425This option is not defined in
426.St -p1003.1-2001 .
427.It Fl 1
428(The numeric digit
429.Dq one . )
430Force output to be
431one entry per line.
432This is the default when
433output is not to a terminal.
434.It Fl ,
435(Comma) When the
436.Fl l
437or
438.Fl s
439option is set, print file sizes grouped and separated by thousands using the
440non-monetary separator returned by
441.Xr localeconv 3 ,
442typically a comma or period.
443If no locale is set, or the locale does not have a non-monetary separator, this
444option has no effect.
445This option is not defined in
446.St -p1003.1-2001 .
447.El
448.Pp
449The
450.Fl 1 , C , x ,
451and
452.Fl l
453options all override each other; the last one specified determines
454the format used.
455.Pp
456The
457.Fl c , u ,
458and
459.Fl U
460options all override each other; the last one specified determines
461the file time used.
462.Pp
463The
464.Fl S , t
465and
466.Fl v
467options override each other; the last one specified determines
468the sort order used.
469.Pp
470The
471.Fl B , b , w ,
472and
473.Fl q
474options all override each other; the last one specified determines
475the format used for non-printable characters.
476.Pp
477The
478.Fl H , L
479and
480.Fl P
481options all override each other (either partially or fully); they
482are applied in the order specified.
483.Pp
484By default,
485.Nm
486lists one entry per line to standard
487output; the exceptions are to terminals or when the
488.Fl C
489or
490.Fl x
491options are specified.
492.Pp
493File information is displayed with one or more
494.Ao blank Ac Ns s
495separating the information associated with the
496.Fl i , s ,
497and
498.Fl l
499options.
500.Ss The Long Format
501If the
502.Fl l
503option is given, the following information
504is displayed for each file:
505file mode,
506number of links, owner name, group name,
507MAC label,
508number of bytes in the file, abbreviated
509month, day-of-month file was last modified,
510hour file last modified, minute file last
511modified, and the pathname.
512.Pp
513If the modification time of the file is more than 6 months
514in the past or future, and the
515.Fl D
516or
517.Fl T
518are not specified,
519then the year of the last modification
520is displayed in place of the hour and minute fields.
521.Pp
522If the owner or group names are not a known user or group name,
523or the
524.Fl n
525option is given,
526the numeric ID's are displayed.
527.Pp
528If the file is a character special or block special file,
529the device number for the file is displayed in the size field.
530If the file is a symbolic link the pathname of the
531linked-to file is preceded by
532.Dq Li -> .
533.Pp
534The listing of a directory's contents is preceded
535by a labeled total number of blocks used in the file system by the files
536which are listed as the directory's contents
537(which may or may not include
538.Pa \&.
539and
540.Pa ..
541and other files which start with a dot, depending on other options).
542.Pp
543The default block size is 512 bytes.
544The block size may be set with option
545.Fl k
546or environment variable
547.Ev BLOCKSIZE .
548Numbers of blocks in the output will have been rounded up so the
549numbers of bytes is at least as many as used by the corresponding
550file system blocks (which might have a different size).
551.Pp
552The file mode printed under the
553.Fl l
554option consists of the
555entry type and the permissions.
556The entry type character describes the type of file, as
557follows:
558.Pp
559.Bl -tag -width 4n -offset indent -compact
560.It Sy \-
561Regular file.
562.It Sy b
563Block special file.
564.It Sy c
565Character special file.
566.It Sy d
567Directory.
568.It Sy l
569Symbolic link.
570.It Sy p
571.Tn FIFO .
572.It Sy s
573Socket.
574.It Sy w
575Whiteout.
576.El
577.Pp
578The next three fields
579are three characters each:
580owner permissions,
581group permissions, and
582other permissions.
583Each field has three character positions:
584.Bl -enum -offset indent
585.It
586If
587.Sy r ,
588the file is readable; if
589.Sy \- ,
590it is not readable.
591.It
592If
593.Sy w ,
594the file is writable; if
595.Sy \- ,
596it is not writable.
597.It
598The first of the following that applies:
599.Bl -tag -width 4n -offset indent
600.It Sy S
601If in the owner permissions, the file is not executable and
602set-user-ID mode is set.
603If in the group permissions, the file is not executable
604and set-group-ID mode is set.
605.It Sy s
606If in the owner permissions, the file is executable
607and set-user-ID mode is set.
608If in the group permissions, the file is executable
609and setgroup-ID mode is set.
610.It Sy x
611The file is executable or the directory is
612searchable.
613.It Sy \-
614The file is neither readable, writable, executable,
615nor set-user-ID nor set-group-ID mode, nor sticky.
616(See below.)
617.El
618.Pp
619These next two apply only to the third character in the last group
620(other permissions).
621.Bl -tag -width 4n -offset indent
622.It Sy T
623The sticky bit is set
624(mode
625.Li 1000 ) ,
626but not execute or search permission.
627(See
628.Xr chmod 1
629or
630.Xr sticky 7 . )
631.It Sy t
632The sticky bit is set (mode
633.Li 1000 ) ,
634and is searchable or executable.
635(See
636.Xr chmod 1
637or
638.Xr sticky 7 . )
639.El
640.El
641.Pp
642The next field contains a
643plus
644.Pq Ql +
645character if the file has an ACL, or a
646space
647.Pq Ql " "
648if it does not.
649The
650.Nm
651utility does not show the actual ACL;
652use
653.Xr getfacl 1
654to do this.
655.Sh ENVIRONMENT
656The following environment variables affect the execution of
657.Nm :
658.Bl -tag -width ".Ev CLICOLOR_FORCE"
659.It Ev BLOCKSIZE
660If this is set, its value, rounded up to 512 or down to a
661multiple of 512, will be used as the block size in bytes by the
662.Fl l
663and
664.Fl s
665options.
666See
667.Sx The Long Format
668subsection for more information.
669.It Ev CLICOLOR
670Use
671.Tn ANSI
672color sequences to distinguish file types.
673See
674.Ev LSCOLORS
675below.
676In addition to the file types mentioned in the
677.Fl F
678option some extra attributes (setuid bit set, etc.) are also displayed.
679The colorization is dependent on a terminal type with the proper
680.Xr termcap 5
681capabilities.
682The default
683.Dq Li cons25
684console has the proper capabilities,
685but to display the colors in an
686.Xr xterm 1 Pq Pa ports/x11/xterm ,
687for example,
688the
689.Ev TERM
690variable must be set to
691.Dq Li xterm-color .
692Other terminal types may require similar adjustments.
693Colorization
694is silently disabled if the output is not directed to a terminal
695unless the
696.Ev CLICOLOR_FORCE
697variable is defined or
698.Fl -color
699is set to
700.Dq always .
701.It Ev CLICOLOR_FORCE
702Color sequences are normally disabled if the output is not directed to
703a terminal.
704This can be overridden by setting this variable.
705The
706.Ev TERM
707variable still needs to reference a color capable terminal however
708otherwise it is not possible to determine which color sequences to
709use.
710.It Ev COLORTERM
711See description for
712.Ev CLICOLOR
713above.
714.It Ev COLUMNS
715If this variable contains a string representing a
716decimal integer, it is used as the
717column position width for displaying
718multiple-text-column output.
719The
720.Nm
721utility calculates how
722many pathname text columns to display
723based on the width provided.
724(See
725.Fl C
726and
727.Fl x . )
728.It Ev LANG
729The locale to use when determining the order of day and month in the long
730.Fl l
731format output.
732See
733.Xr environ 7
734for more information.
735.It Ev LSCOLORS
736The value of this variable describes what color to use for which
737attribute when colors are enabled with
738.Ev CLICOLOR
739or
740.Ev COLORTERM .
741This string is a concatenation of pairs of the format
742.Ar f Ns Ar b ,
743where
744.Ar f
745is the foreground color and
746.Ar b
747is the background color.
748When the background color is capitalized, the text is underlined.
749.Pp
750The color designators are as follows:
751.Pp
752.Bl -tag -width 4n -offset indent -compact
753.It Sy a
754black
755.It Sy b
756red
757.It Sy c
758green
759.It Sy d
760brown
761.It Sy e
762blue
763.It Sy f
764magenta
765.It Sy g
766cyan
767.It Sy h
768light grey
769.It Sy A
770bold or underlined black, usually shows up as dark grey
771.It Sy B
772bold or underlined red
773.It Sy C
774bold or underlined green
775.It Sy D
776bold or underlined brown, usually shows up as yellow
777.It Sy E
778bold or underlined blue
779.It Sy F
780bold or underlined magenta
781.It Sy G
782bold or underlined cyan
783.It Sy H
784bold or underlined light grey; looks like bright white
785.It Sy x
786default foreground or background
787.It Sy X
788default foreground or background, with an underline or bold
789.El
790.Pp
791Note that the above are standard
792.Tn ANSI
793colors.
794The actual display may differ
795depending on the color capabilities of the terminal in use.
796.Pp
797The order of the attributes are as follows:
798.Pp
799.Bl -enum -offset indent -compact
800.It
801directory
802.It
803symbolic link
804.It
805socket
806.It
807pipe
808.It
809executable
810.It
811block special
812.It
813character special
814.It
815executable with setuid bit set
816.It
817executable with setgid bit set
818.It
819directory writable to others, with sticky bit
820.It
821directory writable to others, without sticky bit
822.El
823.Pp
824The default is
825.Qq "exfxcxdxbxegedabagacad" ,
826i.e., blue foreground and
827default background for regular directories, black foreground and red
828background for setuid executables, etc.
829.It Ev LS_COLWIDTHS
830If this variable is set, it is considered to be a
831colon-delimited list of minimum column widths.
832Unreasonable
833and insufficient widths are ignored (thus zero signifies
834a dynamically sized column).
835Not all columns have changeable widths.
836The fields are,
837in order: inode, block count, number of links, user name,
838group name, flags, file size, file name.
839.It Ev LS_SAMESORT
840If this variable is set, the
841.Fl t
842option sorts the names of files with the same modification timestamp in the same
843sense as the time sort.
844See the description of the
845.Fl t
846option for more details.
847.It Ev TERM
848The
849.Ev CLICOLOR
850and
851.Ev COLORTERM
852functionality depends on a terminal type with color capabilities.
853.It Ev TZ
854The timezone to use when displaying dates.
855See
856.Xr environ 7
857for more information.
858.El
859.Sh EXIT STATUS
860.Ex -std
861.Sh EXAMPLES
862List the contents of the current working directory in long format:
863.Pp
864.Dl $ ls -l
865.Pp
866In addition to listing the contents of the current working directory in
867long format, show inode numbers, file flags (see
868.Xr chflags 1 ) ,
869and suffix each filename with a symbol representing its file type:
870.Pp
871.Dl $ ls -lioF
872.Pp
873List the files in
874.Pa /var/log ,
875sorting the output such that the mostly recently modified entries are
876printed first:
877.Pp
878.Dl $ ls -lt /var/log
879.Sh COMPATIBILITY
880The group field is now automatically included in the long listing for
881files in order to be compatible with the
882.St -p1003.2
883specification.
884.Sh SEE ALSO
885.Xr chflags 1 ,
886.Xr chmod 1 ,
887.Xr getfacl 1 ,
888.Xr sort 1 ,
889.Xr xterm 1 Pq Pa ports/x11/xterm ,
890.Xr localeconv 3 ,
891.Xr strcoll 3 ,
892.Xr strftime 3 ,
893.Xr strmode 3 ,
894.Xr strverscmp 3 ,
895.Xr termcap 5 ,
896.Xr maclabel 7 ,
897.Xr sticky 7 ,
898.Xr symlink 7 ,
899.Xr getfmac 8
900.Sh STANDARDS
901With the exception of options
902.Fl g , n
903and
904.Fl o ,
905the
906.Nm
907utility conforms to
908.St -p1003.1-2001
909and
910.St -p1003.1-2008 .
911The options
912.Fl B , D , G , I , T , U , W , Z , b , h , v , w , y
913and
914.Fl ,
915are non-standard extensions.
916.Pp
917The ACL support is compatible with
918.Tn IEEE
919Std\~1003.2c
920.Pq Dq Tn POSIX Ns .2c
921Draft\~17
922(withdrawn).
923.Sh HISTORY
924An
925.Nm
926command appeared in
927.At v1 .
928.Pp
929The
930.Fl v
931option was added in
932.Fx 13.2 .
933.Sh BUGS
934To maintain backward compatibility, the relationships between the many
935options are quite complex.
936.Pp
937The exception mentioned in the
938.Fl s
939option description might be a feature that was
940based on the fact that single-column output
941usually goes to something other than a terminal.
942It is debatable whether this is a design bug.
943.Pp
944.St -p1003.2
945mandates opposite sort orders for files with the same timestamp when
946sorting with the
947.Fl t
948option.
949