xref: /netbsd-src/usr.bin/stat/stat.1 (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1.\"	$NetBSD: stat.1,v 1.46 2022/06/24 13:11:44 kre Exp $
2.\"
3.\" Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Andrew Brown and Jan Schaumann.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd June 22, 2022
31.Dt STAT 1
32.Os
33.Sh NAME
34.Nm stat
35.Nd display file status
36.Sh SYNOPSIS
37.Nm
38.Op Fl FLnq
39.Oo
40.Fl f Ar format |
41.Fl l |
42.Fl r |
43.Fl s |
44.Fl x
45.Oc
46.Op Fl t Ar timefmt
47.Op Ar
48.Sh DESCRIPTION
49The
50.Nm
51utility displays information about each file given by
52.Ar file .
53Read, write, or execute permissions for the named file are not required, but
54all directories listed in the pathname leading to the file must be
55searchable.
56.Pp
57If no
58.Ar file
59argument is given,
60.Nm
61displays information about the file descriptor for standard input.
62In this case the
63.Fl L
64option is ignored, and
65.Nm
66uses
67.Xr fstat 2
68rather than
69.Xr lstat 2
70or
71.Xr stat 2
72to obtain information.
73The
74.Sq file name
75(and also the
76.Sq path name )
77in this case is
78.Ql \&(stdin) .
79The file number
80.Pq Ql %@
81will be zero.
82.Pp
83Otherwise the information displayed is obtained by calling
84.Xr lstat 2
85(or
86.Xr stat 2
87with
88.Fl L )
89with each given argument in turn and evaluating the returned structure.
90.Pp
91The default format displays the
92.Fa st_dev ,
93.Fa st_ino ,
94.Fa st_mode ,
95.Fa st_nlink ,
96.Fa st_uid ,
97.Fa st_gid ,
98.Fa st_rdev ,
99.Fa st_size ,
100.Fa st_atime ,
101.Fa st_mtime ,
102.Fa st_ctime ,
103.Fa st_birthtime ,
104.Fa st_blksize ,
105.Fa st_blocks ,
106and
107.Fa st_flags
108fields, in that order.
109.Pp
110The options are as follows:
111.Bl -tag -width Fl
112.It Fl F
113As in
114.Xr ls 1 ,
115display a slash
116.Pq Ql /
117immediately after each pathname that is a directory, an
118asterisk
119.Pq Ql *
120after each that is executable, an at sign
121.Pq Ql @
122after each symbolic link, a percent sign
123.Pq Ql %
124after each whiteout, an equal sign
125.Pq Ql =
126after each socket, and a vertical bar
127.Pq Ql \&|
128after each that is a FIFO.
129The use of
130.Fl F
131implies
132.Fl l .
133.It Fl f Ar format
134Display information using the specified format.
135See the
136.Sx FORMATS
137section for a description of valid formats.
138.It Fl L
139Use
140.Xr stat 2
141instead of
142.Xr lstat 2 .
143The information reported by
144.Nm
145will refer to the target of
146.Ar file ,
147if
148.Ar file
149is a symbolic link, rather than to
150.Ar file
151itself.
152.It Fl l
153Display output in
154.Ic ls Fl lT
155format.
156.It Fl n
157Do not force a newline to appear at the end of each piece of output.
158.It Fl q
159Suppress failure messages if calls to
160.Xr fstat 2 ,
161.Xr lstat 2 ,
162.Xr readlink 2 ,
163.Xr realpath 3 ,
164or
165.Xr stat 2
166fail.
167.It Fl r
168Display raw information.
169That is, for all the fields in the stat-structure,
170display the raw, numerical value (for example, times in seconds since the
171epoch, etc.)
172.It Fl s
173Display information in
174.Dq shell command
175output format,
176suitable for initializing variables.
177This is equivalent to specifying
178.Bd -literal
179FMT="st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l"
180FMT="$FMT st_uid=%u st_gid=%g st_rdev=%r st_size=%z"
181FMT="$FMT st_atime=%Sa st_mtime=%Sm st_ctime=%Sc"
182FMT="$FMT st_birthtime=%SB st_blksize=%k st_blocks=%b"
183FMT="$FMT st_flags=%f"
184stat -t %s -f "$FMT" .
185.Ed
186.Pp
187The
188.Ar timefmt
189may be altered from the default for
190.Fl s
191.Pq Ql \&%s
192by also using the
193.Fl t
194option.
195Note that if you use a
196.Ar timefmt
197that contains embedded whitespace or shell
198meta-characters,
199you will need to
200include appropriate quoting in the
201.Fl t
202format, or supply an explicit format
203.Pq Fl f ,
204rather than
205.Fl s ,
206with the format containing appropriate quoting so the output remains valid.
207.It Fl t Ar timefmt
208Display timestamps, when to be output in string format,
209using the specified format.
210This format is
211passed directly to
212.Xr strftime 3
213with the extension that
214.Ql %f
215prints nanoseconds if available.
216.It Fl x
217Display information in a more verbose way as seen from some Linux
218distributions.
219.El
220.Ss FORMATS
221Format strings are similar to
222.Xr printf 3
223formats in that they contain character data,
224which is simply output,
225interspersed with data conversions which start with
226.Cm % ,
227are then followed by a sequence of formatting characters, and end in
228a character that selects the datum, the field of the struct stat,
229or other data,
230which is to be formatted.
231If the
232.Cm %
233is immediately followed by one of
234.Cm n ,
235.Cm t ,
236.Cm % ,
237or
238.Cm @ ,
239then a newline character, a tab character, a percent character,
240or the current file number in the argument list is printed.
241Otherwise the string is examined for the following:
242.Pp
243Any of the following optional flags in any order:
244.Bl -tag -width Cm
245.It Cm #
246Selects an alternate output form for string, octal and hexadecimal output.
247String output will be encoded in
248.Xr vis 3
249style.
250Octal output will have a leading zero.
251Non-zero hexadecimal output will have
252.Ql 0x
253prepended to it.
254.It Cm +
255Asserts that a sign indicating whether a number is positive or negative
256should always be printed.
257Non-negative numbers are not usually printed with a sign.
258.It Cm -
259Aligns string output to the left of the field, instead of to the right.
260.It Cm 0
261Sets the fill character for left padding to the 0 character, instead of
262a space.
263.It space
264Reserves a space at the front of non-negative signed output fields.
265A
266.Sq Cm +
267overrides a space if both are used.
268.El
269.Pp
270Then followed by the following fields in the following order:
271.Bl -tag -width Ar
272.It Ar size
273An optional decimal digit string specifying the minimum field width.
274Note that a leading zero is treated as the
275.Sq Cm 0
276flag (above), subsequent embedded zeroes are part of the
277.Ar size .
278.It Ar prec
279An optional precision composed of a decimal point
280.Sq Cm \&.
281and a decimal digit string that indicates the maximum string length,
282the number of digits to appear after the decimal point in floating point
283output, or the minimum number of digits to appear in other numeric output.
284.It Ar fmt
285An optional output format specifier which is one of
286.Cm D ,
287.Cm O ,
288.Cm U ,
289.Cm X ,
290.Cm F ,
291or
292.Cm S .
293These represent signed decimal output, octal output, unsigned decimal
294output, hexadecimal output, floating point output, and string output,
295respectively.
296Some output formats do not apply to all fields.
297Floating point output only applies to timespec fields (the
298.Cm a ,
299.Cm m ,
300and
301.Cm c
302fields).
303.Pp
304The special output format specifier
305.Cm S
306may be used to indicate that the output, if
307applicable, should be in string format.
308May be used in combination with the following field specifiers:
309.Bl -tag -width Cm
310.It Cm a , m , c
311Display date in
312.Xr strftime 3
313format with the extension that
314.Ql %f
315prints nanoseconds if available.
316.It Cm d , r
317Display actual device name.
318.It Cm g , u
319Display group or user name.
320.It Cm p
321Display the mode of
322.Ar file
323symbolically, as in
324.Ic ls -lTd .
325.It Cm N
326Displays the name of
327.Ar file .
328.It Cm T
329Displays the type of
330.Ar file .
331.It Cm R , Y
332Insert a
333.Sq Li \~->\~ \" .Ql, but force quotes even in PS output
334into the output.
335Note that the default output formats for
336.Cm Y
337and
338.Cm R
339are strings, if
340.Cm S
341is specified explicitly, these four characters are prepended.
342.El
343.It Ar sub
344An optional sub field specifier: high, middle, or low.
345Only applies to the
346.Cm d ,
347.Cm N ,
348.Cm p ,
349.Cm r ,
350.Cm T ,
351and
352.Cm z
353output field specifiers.
354It can be one of the following:
355.Bl -tag -width Cm
356.It Cm H
357.Dq High
358subfield of
359.Ar datum\^ :
360.Bl -tag -compact -width Cm
361.It Cm d , r
362Major number for devices
363.It Cm N
364Directory path of the file, similar to what
365.Xr dirname 1
366would show
367.It Cm p
368.Dq User
369bits from the string form of permissions,
370or the file
371.Dq type
372bits from the numeric forms
373.It Cm T
374The long output form of file type
375.It Cm z
376File size, rounded to the nearest gigabyte
377.El
378.It Cm M
379.Dq Middle
380subfield of
381.Ar datum\^ :
382.Bl -tag -compact -width Cm
383.It Cm p
384The
385.Dq group
386bits from the string form of permissions,
387or the
388.Dq suid ,
389.Dq sgid ,
390and
391.Dq sticky
392bits from the numeric forms
393.It Cm z
394File size, rounded to the nearest megabyte
395.El
396.It Cm L
397.Dq Low
398subfield of
399.Ar datum\^ :
400.Bl -tag -compact -width Cm
401.It Cm d , r
402Minor number for devices
403.It Cm N
404Base filename of the file, similar to what
405.Xr basename 1
406would show
407.It Cm p
408The
409.Dq other
410bits from the string form of permissions,
411or the
412.Dq user ,
413.Dq group ,
414and
415.Dq other
416bits from the numeric forms
417.It Cm T
418The
419.Ic ls -F
420style output character for file type (the use of
421.Cm L
422here is optional)
423.It Cm z
424File size, rounded to the nearest kilobyte
425.El
426.El
427.It Ar datum
428A required field specifier, ending the conversion specification,
429being one of the following:
430.Bl -tag -width Cm
431.It Cm d
432Device upon which
433.Ar file
434resides
435.Pq Fa st_dev .
436.It Cm i
437.Ar file Ap s
438inode number
439.Pq Fa st_ino .
440.It Cm p
441File type and permissions
442.Pq Fa st_mode .
443.It Cm l
444Number of hard links to
445.Ar file
446.Pq Fa st_nlink .
447.It Cm u , g
448User-id and group-id of
449.Ar file Ap s
450owner
451.Pq Fa st_uid , st_gid .
452.It Cm r
453Device number for character and block device special files
454.Pq Fa st_rdev .
455.It Cm a , m , c , B
456The time
457.Ar file
458was last accessed or modified, or when its inode was last changed, or
459the birth time of the inode
460.Pq Fa st_atime , st_mtime , st_ctime , st_birthtime .
461.It Cm z
462The size of
463.Ar file
464in bytes
465.Pq Fa st_size .
466.It Cm b
467Number of blocks allocated for
468.Ar file
469.Pq Fa st_blocks .
470.It Cm k
471Optimal file system I/O operation block size
472.Pq Fa st_blksize .
473.It Cm f
474User defined flags for
475.Ar file
476.Pq Fa st_flags .
477.It Cm v
478Inode generation number
479.Pq Fa st_gen .
480.El
481.Pp
482The following five field specifiers are not drawn directly from the
483data in
484.Vt struct stat ,
485but are:
486.Bl -tag -width Cm
487.It Cm N
488The name of the file.
489.It Cm R
490The absolute pathname corresponding to the file.
491.It Cm T
492The file type, either as in
493.Ic ls -F
494or in a more descriptive form if the sub field specifier
495.Cm H
496is given.
497.It Cm Y
498The target of a symbolic link.
499.It Cm Z
500Expands to
501.Dq Ar major Ns Li \&, Ns Ar minor\^
502.Po
503that is,
504.Sq Li %Hr,%-Lr
505.Pc
506for character or block special devices,
507and gives size output
508.Pq Sq Li %z
509for all other file types.
510A specified field width applies to the overall result
511(approximately half each for the two device file sub-fields),
512but precision, output format, and flags are used separately for
513each conversion made (but note the
514.Sq Cm -
515in the
516.Sq Li %-Lr
517conversion.)
518.El
519.El
520.Pp
521Only the
522.Sq Cm %
523and the
524.Ar datum
525(field specifier) are required.
526Most field specifiers default to
527.Cm U
528as an output format, with the
529exception of
530.Cm p
531which defaults to
532.Cm O ;
533.Cm a , m ,
534and
535.Cm c
536which default to
537.Cm D ;
538and
539.Cm Y , T , R ,
540and
541.Cm N ,
542which default to
543.Cm S .
544.Sh EXIT STATUS
545.Ex -std
546.Sh EXAMPLES
547If no options are specified, the default format is:
548.Bd -literal -offset indent
549%d %i %Sp %l %Su %Sg %r %z "%Sa" "%Sm" "%Sc" "%SB" %k %b %#Xf %N
550.Ed
551.Pp
552Thus:
553.Bd -literal -offset indent -compact
554> stat /tmp/bar
5550 78852 -rw-r--r-- 1 root wheel \(mi1 0 "Jul  8 10:26:03 2004" "Jul  8 10:26:03 2004" "Jul  8 10:28:13 2004" "Jan  1 09:00:00 1970" 16384 0 0 /tmp/bar
556.Ed
557.Pp
558This next example produces output very similar to that from
559.Ic find Ar \&.\|.\|. Ic -ls ,
560except that
561.Xr find 1
562displays the time in a different format, and
563.Xr find 1
564sometimes adds one or more spaces after the comma in
565.Dq Ar major Ns Li \&, Ns Ar minor\^
566for device nodes:
567.Bd -literal -offset indent
568> stat -f "%7i %6b %-11Sp %3l %-17Su %-17Sg %9Z %Sm %N%SY" /tmp/bar
569  78852      0 -rw-r--r--    1 root              wheel                     0 Jul  8 10:26:03 2004 /tmp/bar
570
571> find /tmp/bar -ls -exit
572  78852      0 -rw-r--r--    1 root              wheel                     0 Jul  8  2004 /tmp/bar
573.Ed
574.Pp
575This example produces output very similar to that from
576.Ic ls -lTd ,
577except that
578.Xr ls 1
579adjusts the column spacing differently when listing multiple files,
580and adds at least one space after the comma in
581.Dq Ar major Ns Li \&, Ns Ar minor\^
582for device nodes:
583.Bd -literal -offset indent
584> stat -f "%-11Sp %l %Su  %Sg  %Z %Sm %N%SY" /tmp/bar
585-rw-r--r--  1 root  wheel  0 Jul  8 10:26:03 2004 /tmp/bar
586
587> ls -lTd /tmp/bar
588-rw-r--r--  1 root  wheel  0 Jul  8 10:26:03 2004 /tmp/bar
589.Ed
590.Pp
591Given a symbolic link
592.Pa /tmp/foo
593that points to
594.Pa / ,
595you would use
596.Nm
597as follows:
598.Bd -literal -offset indent
599> stat -F /tmp/foo
600lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -> /
601
602> stat -LF /tmp/foo
603drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/
604.Ed
605.Pp
606To initialize some shell variables, you could use the
607.Fl s
608flag as follows:
609.Bd -literal -offset indent
610> csh
611% eval set `stat -s .cshrc`
612% echo $st_size $st_mtime
6131148 1015432481
614
615> sh
616$ eval $(stat -s .profile)
617$ echo $st_size $st_mtime
6181148 1015432481
619.Ed
620.Pp
621In order to get a list of the kind of files including files pointed to if the
622file is a symbolic link, you could use the following format:
623.Bd -literal -offset indent
624$ stat -f "%N: %HT%SY" /tmp/*
625/tmp/bar: Symbolic Link -> /tmp/foo
626/tmp/output25568: Regular File
627/tmp/blah: Directory
628/tmp/foo: Symbolic Link -> /
629.Ed
630.Pp
631In order to get a list of the devices, their types and the major and minor
632device numbers, formatted with tabs and line breaks, you could use the
633following format:
634.Bd -literal -offset indent
635stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/*
636[...]
637Name: /dev/wt8
638        Type: Block Device
639        Major: 3
640        Minor: 8
641
642Name: /dev/zero
643        Type: Character Device
644        Major: 2
645        Minor: 12
646.Ed
647.Pp
648In order to determine the permissions set on a file separately, you could use
649the following format:
650.Bd -literal -offset indent
651> stat -f "%Sp -> owner=%SHp group=%SMp other=%SLp" .
652drwxr-xr-x -> owner=rwx group=r-x other=r-x
653.Ed
654.Pp
655In order to determine the three files that have been modified most recently,
656you could use the following format:
657.Bd -literal -offset indent
658> stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2-
659Apr 25 11:47:00 2002 /tmp/blah
660Apr 25 10:36:34 2002 /tmp/bar
661Apr 24 16:47:35 2002 /tmp/foo
662.Ed
663.Pp
664User names, group names, and file names that contain spaces
665or other special characters may be encoded in
666.Xr vis 3
667style, using the
668.Sq Cm \&#
669flag:
670.Bd -literal -offset indent
671> ln -s 'target with spaces' 'link with spaces'
672> stat -f "%#N%#SY" 'link with spaces'
673link\eswith\esspaces -> target\eswith\esspaces
674.Ed
675.Sh SEE ALSO
676.Xr basename 1 ,
677.Xr dirname 1 ,
678.Xr find 1 ,
679.Xr ls 1 ,
680.Xr readlink 1 ,
681.Xr fstat 2 ,
682.Xr lstat 2 ,
683.Xr readlink 2 ,
684.Xr stat 2 ,
685.Xr printf 3 ,
686.Xr realpath 3 ,
687.Xr strftime 3
688.Sh HISTORY
689The
690.Nm
691utility appeared in
692.Nx 1.6 .
693.Sh AUTHORS
694.An -nosplit
695The
696.Nm
697utility was written by
698.An Andrew Brown
699.Aq atatat@NetBSD.org .
700This man page was written by
701.An Jan Schaumann
702.Aq jschauma@NetBSD.org .
703