xref: /freebsd/bin/ls/ls.1 (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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.\"     @(#)ls.1	8.7 (Berkeley) 7/29/94
33.\"
34.Dd July 18, 2023
35.Dt LS 1
36.Os
37.Sh NAME
38.Nm ls
39.Nd list directory contents
40.Sh SYNOPSIS
41.Nm
42.Op Fl ABCFGHILPRSTUWZabcdfghiklmnopqrstuvwxy1\&,
43.Op Fl -color Ns = Ns Ar when
44.Op Fl D Ar format
45.Op Ar
46.Sh DESCRIPTION
47For each operand that names a
48.Ar file
49of a type other than
50directory,
51.Nm
52displays its name as well as any requested,
53associated information.
54For each operand that names a
55.Ar file
56of type directory,
57.Nm
58displays the names of files contained
59within that directory, as well as any requested, associated
60information.
61.Pp
62If no operands are given, the contents of the current
63directory are displayed.
64If more than one operand is given,
65non-directory operands are displayed first; directory
66and non-directory operands are sorted separately and in
67lexicographical order.
68.Pp
69The following options are available:
70.Bl -tag -width indent
71.It Fl A
72Include directory entries whose names begin with a
73dot
74.Pq Sq Pa \&.
75except for
76.Pa \&.
77and
78.Pa .. .
79Automatically set for the super-user unless
80.Fl I
81is specified.
82.It Fl B
83Force printing of non-printable characters (as defined by
84.Xr ctype 3
85and current locale settings) in file names as
86.Li \e Ns Va xxx ,
87where
88.Va xxx
89is the numeric value of the character in octal.
90This option is not defined in
91.St -p1003.1-2008 .
92.It Fl C
93Force multi-column output; this is the default when output is to a terminal.
94.It Fl D Ar format
95When printing in the long
96.Pq Fl l
97format, use
98.Ar format
99to format the date and time output.
100The argument
101.Ar format
102is a string used by
103.Xr strftime 3 .
104Depending on the choice of format string, this may result in a
105different number of columns in the output.
106This option overrides the
107.Fl T
108option.
109This option is not defined in
110.St -p1003.1-2008 .
111.It Fl F
112Display a slash
113.Pq Ql /
114immediately after each pathname that is a directory,
115an asterisk
116.Pq Ql *
117after each that is executable,
118an at sign
119.Pq Ql @
120after each symbolic link,
121an equals sign
122.Pq Ql =
123after each socket,
124a percent sign
125.Pq Ql %
126after each whiteout,
127and a vertical bar
128.Pq Ql \&|
129after each that is a
130.Tn FIFO .
131.It Fl G
132Enable colorized output.
133This option is equivalent to defining
134.Ev CLICOLOR
135or
136.Ev COLORTERM
137in the environment and setting
138.Fl -color Ns = Ns Ar auto .
139(See below.)
140This functionality can be compiled out by removing the definition of
141.Ev COLORLS .
142This option is not defined in
143.St -p1003.1-2008 .
144.It Fl H
145Symbolic links on the command line are followed.
146This option is assumed if
147none of the
148.Fl F , d ,
149or
150.Fl l
151options are specified.
152.It Fl I
153Prevent
154.Fl A
155from being automatically set for the super-user.
156This option is not defined in
157.St -p1003.1-2008 .
158.It Fl L
159If argument is a symbolic link, list the file or directory the link references
160rather than the link itself.
161This option cancels the
162.Fl P
163option.
164.It Fl P
165If argument is a symbolic link, list the link itself rather than the
166object the link references.
167This option cancels the
168.Fl H
169and
170.Fl L
171options.
172.It Fl R
173Recursively list subdirectories encountered.
174.It Fl S
175Sort by size (largest file first) before sorting the operands in
176lexicographical order.
177.It Fl T
178When printing in the long
179.Pq Fl l
180format, display complete time information for the file, including
181month, day, hour, minute, second, and year.
182The
183.Fl D
184option gives even more control over the output format.
185This option is not defined in
186.St -p1003.1-2008 .
187.It Fl U
188Use time when file was created for sorting or printing.
189This option is not defined in
190.St -p1003.1-2008 .
191.It Fl W
192Display whiteouts when scanning directories.
193This option is not defined in
194.St -p1003.1-2008 .
195.It Fl Z
196Display each file's MAC label; see
197.Xr maclabel 7 .
198This option is not defined in
199.St -p1003.1-2008 .
200.It Fl a
201Include directory entries whose names begin with a
202dot
203.Pq Sq Pa \&. .
204.It Fl b
205As
206.Fl B ,
207but use
208.Tn C
209escape codes whenever possible.
210This option is not defined in
211.St -p1003.1-2008 .
212.It Fl c
213Use time when file status was last changed for sorting or printing.
214.It Fl -color Ns = Ns Ar when
215Output colored escape sequences based on
216.Ar when ,
217which may be set to either
218.Cm always ,
219.Cm auto ,
220or
221.Cm never .
222.Pp
223.Cm always
224will make
225.Nm
226always output color.
227If
228.Ev TERM
229is unset or set to an invalid terminal, then
230.Nm
231will fall back to explicit
232.Tn ANSI
233escape sequences without the help of
234.Xr termcap 5 .
235.Cm always
236is the default if
237.Fl -color
238is specified without an argument.
239.Pp
240.Cm auto
241will make
242.Nm
243output escape sequences based on
244.Xr termcap 5 ,
245but only if
246.Dv stdout
247is a tty and either the
248.Fl G
249flag is specified or the
250.Ev COLORTERM
251environment variable is 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
437option is set, print file sizes grouped and separated by thousands using the
438non-monetary separator returned by
439.Xr localeconv 3 ,
440typically a comma or period.
441If no locale is set, or the locale does not have a non-monetary separator, this
442option has no effect.
443This option is not defined in
444.St -p1003.1-2001 .
445.El
446.Pp
447The
448.Fl 1 , C , x ,
449and
450.Fl l
451options all override each other; the last one specified determines
452the format used.
453.Pp
454The
455.Fl c , u ,
456and
457.Fl U
458options all override each other; the last one specified determines
459the file time used.
460.Pp
461The
462.Fl S
463and
464.Fl t
465options override each other; the last one specified determines
466the sort order used.
467.Pp
468The
469.Fl B , b , w ,
470and
471.Fl q
472options all override each other; the last one specified determines
473the format used for non-printable characters.
474.Pp
475The
476.Fl H , L
477and
478.Fl P
479options all override each other (either partially or fully); they
480are applied in the order specified.
481.Pp
482By default,
483.Nm
484lists one entry per line to standard
485output; the exceptions are to terminals or when the
486.Fl C
487or
488.Fl x
489options are specified.
490.Pp
491File information is displayed with one or more
492.Ao blank Ac Ns s
493separating the information associated with the
494.Fl i , s ,
495and
496.Fl l
497options.
498.Ss The Long Format
499If the
500.Fl l
501option is given, the following information
502is displayed for each file:
503file mode,
504number of links, owner name, group name,
505MAC label,
506number of bytes in the file, abbreviated
507month, day-of-month file was last modified,
508hour file last modified, minute file last
509modified, and the pathname.
510.Pp
511If the modification time of the file is more than 6 months
512in the past or future, and the
513.Fl D
514or
515.Fl T
516are not specified,
517then the year of the last modification
518is displayed in place of the hour and minute fields.
519.Pp
520If the owner or group names are not a known user or group name,
521or the
522.Fl n
523option is given,
524the numeric ID's are displayed.
525.Pp
526If the file is a character special or block special file,
527the device number for the file is displayed in the size field.
528If the file is a symbolic link the pathname of the
529linked-to file is preceded by
530.Dq Li -> .
531.Pp
532The listing of a directory's contents is preceded
533by a labeled total number of blocks used in the file system by the files
534which are listed as the directory's contents
535(which may or may not include
536.Pa \&.
537and
538.Pa ..
539and other files which start with a dot, depending on other options).
540.Pp
541The default block size is 512 bytes.
542The block size may be set with option
543.Fl k
544or environment variable
545.Ev BLOCKSIZE .
546Numbers of blocks in the output will have been rounded up so the
547numbers of bytes is at least as many as used by the corresponding
548file system blocks (which might have a different size).
549.Pp
550The file mode printed under the
551.Fl l
552option consists of the
553entry type and the permissions.
554The entry type character describes the type of file, as
555follows:
556.Pp
557.Bl -tag -width 4n -offset indent -compact
558.It Sy \-
559Regular file.
560.It Sy b
561Block special file.
562.It Sy c
563Character special file.
564.It Sy d
565Directory.
566.It Sy l
567Symbolic link.
568.It Sy p
569.Tn FIFO .
570.It Sy s
571Socket.
572.It Sy w
573Whiteout.
574.El
575.Pp
576The next three fields
577are three characters each:
578owner permissions,
579group permissions, and
580other permissions.
581Each field has three character positions:
582.Bl -enum -offset indent
583.It
584If
585.Sy r ,
586the file is readable; if
587.Sy \- ,
588it is not readable.
589.It
590If
591.Sy w ,
592the file is writable; if
593.Sy \- ,
594it is not writable.
595.It
596The first of the following that applies:
597.Bl -tag -width 4n -offset indent
598.It Sy S
599If in the owner permissions, the file is not executable and
600set-user-ID mode is set.
601If in the group permissions, the file is not executable
602and set-group-ID mode is set.
603.It Sy s
604If in the owner permissions, the file is executable
605and set-user-ID mode is set.
606If in the group permissions, the file is executable
607and setgroup-ID mode is set.
608.It Sy x
609The file is executable or the directory is
610searchable.
611.It Sy \-
612The file is neither readable, writable, executable,
613nor set-user-ID nor set-group-ID mode, nor sticky.
614(See below.)
615.El
616.Pp
617These next two apply only to the third character in the last group
618(other permissions).
619.Bl -tag -width 4n -offset indent
620.It Sy T
621The sticky bit is set
622(mode
623.Li 1000 ) ,
624but not execute or search permission.
625(See
626.Xr chmod 1
627or
628.Xr sticky 7 . )
629.It Sy t
630The sticky bit is set (mode
631.Li 1000 ) ,
632and is searchable or executable.
633(See
634.Xr chmod 1
635or
636.Xr sticky 7 . )
637.El
638.El
639.Pp
640The next field contains a
641plus
642.Pq Ql +
643character if the file has an ACL, or a
644space
645.Pq Ql " "
646if it does not.
647The
648.Nm
649utility does not show the actual ACL;
650use
651.Xr getfacl 1
652to do this.
653.Sh ENVIRONMENT
654The following environment variables affect the execution of
655.Nm :
656.Bl -tag -width ".Ev CLICOLOR_FORCE"
657.It Ev BLOCKSIZE
658If this is set, its value, rounded up to 512 or down to a
659multiple of 512, will be used as the block size in bytes by the
660.Fl l
661and
662.Fl s
663options.
664See
665.Sx The Long Format
666subsection for more information.
667.It Ev CLICOLOR
668Use
669.Tn ANSI
670color sequences to distinguish file types.
671See
672.Ev LSCOLORS
673below.
674In addition to the file types mentioned in the
675.Fl F
676option some extra attributes (setuid bit set, etc.) are also displayed.
677The colorization is dependent on a terminal type with the proper
678.Xr termcap 5
679capabilities.
680The default
681.Dq Li cons25
682console has the proper capabilities,
683but to display the colors in an
684.Xr xterm 1 Pq Pa ports/x11/xterm ,
685for example,
686the
687.Ev TERM
688variable must be set to
689.Dq Li xterm-color .
690Other terminal types may require similar adjustments.
691Colorization
692is silently disabled if the output is not directed to a terminal
693unless the
694.Ev CLICOLOR_FORCE
695variable is defined or
696.Fl -color
697is set to
698.Dq always .
699.It Ev CLICOLOR_FORCE
700Color sequences are normally disabled if the output is not directed to
701a terminal.
702This can be overridden by setting this variable.
703The
704.Ev TERM
705variable still needs to reference a color capable terminal however
706otherwise it is not possible to determine which color sequences to
707use.
708.It Ev COLORTERM
709See description for
710.Ev CLICOLOR
711above.
712.It Ev COLUMNS
713If this variable contains a string representing a
714decimal integer, it is used as the
715column position width for displaying
716multiple-text-column output.
717The
718.Nm
719utility calculates how
720many pathname text columns to display
721based on the width provided.
722(See
723.Fl C
724and
725.Fl x . )
726.It Ev LANG
727The locale to use when determining the order of day and month in the long
728.Fl l
729format output.
730See
731.Xr environ 7
732for more information.
733.It Ev LSCOLORS
734The value of this variable describes what color to use for which
735attribute when colors are enabled with
736.Ev CLICOLOR
737or
738.Ev COLORTERM .
739This string is a concatenation of pairs of the format
740.Ar f Ns Ar b ,
741where
742.Ar f
743is the foreground color and
744.Ar b
745is the background color.
746When the background color is capitalized, the text will underlined.
747.Pp
748The color designators are as follows:
749.Pp
750.Bl -tag -width 4n -offset indent -compact
751.It Sy a
752black
753.It Sy b
754red
755.It Sy c
756green
757.It Sy d
758brown
759.It Sy e
760blue
761.It Sy f
762magenta
763.It Sy g
764cyan
765.It Sy h
766light grey
767.It Sy A
768bold or underlined black, usually shows up as dark grey
769.It Sy B
770bold or underlined red
771.It Sy C
772bold or underlined green
773.It Sy D
774bold or underlined brown, usually shows up as yellow
775.It Sy E
776bold or underlined blue
777.It Sy F
778bold or underlined magenta
779.It Sy G
780bold or underlined cyan
781.It Sy H
782bold or underlined light grey; looks like bright white
783.It Sy x
784default foreground or background
785.It Sy X
786default foreground or background, with an underline or bold
787.El
788.Pp
789Note that the above are standard
790.Tn ANSI
791colors.
792The actual display may differ
793depending on the color capabilities of the terminal in use.
794.Pp
795The order of the attributes are as follows:
796.Pp
797.Bl -enum -offset indent -compact
798.It
799directory
800.It
801symbolic link
802.It
803socket
804.It
805pipe
806.It
807executable
808.It
809block special
810.It
811character special
812.It
813executable with setuid bit set
814.It
815executable with setgid bit set
816.It
817directory writable to others, with sticky bit
818.It
819directory writable to others, without sticky bit
820.El
821.Pp
822The default is
823.Qq "exfxcxdxbxegedabagacad" ,
824i.e., blue foreground and
825default background for regular directories, black foreground and red
826background for setuid executables, etc.
827.It Ev LS_COLWIDTHS
828If this variable is set, it is considered to be a
829colon-delimited list of minimum column widths.
830Unreasonable
831and insufficient widths are ignored (thus zero signifies
832a dynamically sized column).
833Not all columns have changeable widths.
834The fields are,
835in order: inode, block count, number of links, user name,
836group name, flags, file size, file name.
837.It Ev LS_SAMESORT
838If this variable is set, the
839.Fl t
840option sorts the names of files with the same modification timestamp in the same
841sense as the time sort.
842See the description of the
843.Fl t
844option for more details.
845.It Ev TERM
846The
847.Ev CLICOLOR
848and
849.Ev COLORTERM
850functionality depends on a terminal type with color capabilities.
851.It Ev TZ
852The timezone to use when displaying dates.
853See
854.Xr environ 7
855for more information.
856.El
857.Sh EXIT STATUS
858.Ex -std
859.Sh EXAMPLES
860List the contents of the current working directory in long format:
861.Pp
862.Dl $ ls -l
863.Pp
864In addition to listing the contents of the current working directory in
865long format, show inode numbers, file flags (see
866.Xr chflags 1 ) ,
867and suffix each filename with a symbol representing its file type:
868.Pp
869.Dl $ ls -lioF
870.Pp
871List the files in
872.Pa /var/log ,
873sorting the output such that the mostly recently modified entries are
874printed first:
875.Pp
876.Dl $ ls -lt /var/log
877.Sh COMPATIBILITY
878The group field is now automatically included in the long listing for
879files in order to be compatible with the
880.St -p1003.2
881specification.
882.Sh SEE ALSO
883.Xr chflags 1 ,
884.Xr chmod 1 ,
885.Xr getfacl 1 ,
886.Xr sort 1 ,
887.Xr xterm 1 Pq Pa ports/x11/xterm ,
888.Xr localeconv 3 ,
889.Xr strcoll 3 ,
890.Xr strftime 3 ,
891.Xr strmode 3 ,
892.Xr strverscmp 3 ,
893.Xr termcap 5 ,
894.Xr maclabel 7 ,
895.Xr sticky 7 ,
896.Xr symlink 7 ,
897.Xr getfmac 8
898.Sh STANDARDS
899With the exception of options
900.Fl g , n
901and
902.Fl o ,
903the
904.Nm
905utility conforms to
906.St -p1003.1-2001
907and
908.St -p1003.1-2008 .
909The options
910.Fl B , D , G , I , T , U , W , Z , b , h , v , w , y
911and
912.Fl ,
913are non-standard extensions.
914.Pp
915The ACL support is compatible with
916.Tn IEEE
917Std\~1003.2c
918.Pq Dq Tn POSIX Ns .2c
919Draft\~17
920(withdrawn).
921.Sh HISTORY
922An
923.Nm
924command appeared in
925.At v1 .
926.Pp
927The
928.Fl v
929option was added in
930.Fx 14.0 .
931.Sh BUGS
932To maintain backward compatibility, the relationships between the many
933options are quite complex.
934.Pp
935The exception mentioned in the
936.Fl s
937option description might be a feature that was
938based on the fact that single-column output
939usually goes to something other than a terminal.
940It is debatable whether this is a design bug.
941.Pp
942.St -p1003.2
943mandates opposite sort orders for files with the same timestamp when
944sorting with the
945.Fl t
946option.
947