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