xref: /openbsd/usr.bin/stat/stat.1 (revision 4bdff4be)
1.\"	$OpenBSD: stat.1,v 1.24 2019/09/07 10:28:27 schwarze Exp $
2.\"	$NetBSD: stat.1,v 1.11 2003/05/08 13:07:10 wiz Exp $
3.\"
4.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Andrew Brown and Jan Schaumann.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29.\" POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: September 7 2019 $
32.Dt STAT 1
33.Os
34.Sh NAME
35.Nm stat
36.Nd display file status
37.Sh SYNOPSIS
38.Nm
39.Op Fl FLnq
40.Oo
41.Fl f Ar format |
42.Fl l | r | s | x
43.Oc
44.Op Fl t Ar timefmt
45.Op Ar
46.Sh DESCRIPTION
47The
48.Nm
49utility displays information about the file pointed to by
50.Ar file .
51Read, write, or execute permissions of the named file are not required, but
52all directories listed in the pathname leading to the file must be
53searchable.
54If no argument is given,
55.Nm
56displays information about the file descriptor for standard input.
57.Pp
58The information displayed is obtained by calling
59.Xr lstat 2
60with the given argument and evaluating the returned structure.
61The default format displays the
62.Fa st_dev ,
63.Fa st_ino ,
64.Fa st_mode ,
65.Fa st_nlink ,
66.Fa st_uid ,
67.Fa st_gid ,
68.Fa st_rdev ,
69.Fa st_size ,
70.Fa st_atime ,
71.Fa st_mtime ,
72.Fa st_ctime ,
73.Fa st_blksize ,
74.Fa st_blocks ,
75and
76.Fa st_flags
77fields, in that order.
78.Pp
79The options are as follows:
80.Bl -tag -width Ds
81.It Fl F
82As in
83.Xr ls 1 ,
84display a slash (/) immediately after each pathname that is a directory, an
85asterisk (*) after each that is executable, an at sign (@) after each symbolic
86link, an equal sign (=) after each socket, and a vertical bar (|) after each
87that is a FIFO.
88The use of
89.Fl F
90implies
91.Fl l .
92.It Fl f Ar format
93Display information using the specified format.
94See the
95.Sx FORMATS
96section for a description of valid formats.
97.It Fl L
98Use
99.Xr stat 2
100instead of
101.Xr lstat 2 .
102The information reported by
103.Nm
104will refer to the target of
105.Ar file ,
106if file is a symbolic link, and not to
107.Ar file
108itself.
109.It Fl l
110Display output in
111.Ic ls Fl lT
112format.
113.It Fl n
114Do not force a newline to appear at the end of each piece of output.
115.It Fl q
116Suppress failure messages if calls to
117.Xr stat 2
118or
119.Xr lstat 2
120fail.
121.It Fl r
122Display raw information.
123That is, for all the fields in the stat-structure,
124display the raw, numerical value (for example, times in seconds since the
125Epoch, etc.).
126.It Fl s
127Format the output as a line of shell variable assignments.
128.It Fl t Ar timefmt
129Display timestamps using the specified format.
130This format is
131passed directly to
132.Xr strftime 3 .
133.It Fl x
134Display information in a more verbose way.
135.El
136.Ss FORMATS
137Format strings are similar to
138.Xr printf 3
139formats in that they start with
140.Cm % ,
141are then followed by a sequence of formatting characters, and end in
142a character that selects the field of the struct stat which is to be
143formatted.
144If the
145.Cm %
146is immediately followed by one of
147.Cm n ,
148.Cm t ,
149.Cm % ,
150or
151.Cm @ ,
152then a newline character, a tab character, a percent character,
153or the current file number is printed, otherwise the string is
154examined for the following:
155.Pp
156Any of the following optional flags:
157.Bl -tag -width Ds
158.It Cm #
159Selects an alternate output form for octal and hexadecimal output.
160Non-zero octal output will have a leading zero, and non-zero
161hexadecimal output will have
162.Sq 0x
163prepended to it.
164.It Cm +
165Asserts that a sign indicating whether a number is positive or negative
166should always be printed.
167Non-negative numbers are not usually printed
168with a sign.
169.It Cm -
170Aligns string output to the left of the field, instead of to the right.
171.It Cm 0
172Sets the fill character for left padding to the 0 character, instead of
173a space.
174.It space
175Reserves a space at the front of non-negative signed output fields.
176A
177.Sq Cm +
178overrides a space if both are used.
179.El
180.Pp
181Then the following fields:
182.Bl -tag -width Ds
183.It Cm size
184An optional decimal digit string specifying the minimum field width.
185.It Cm prec
186An optional precision composed of a decimal point
187.Sq Cm \&.
188and a decimal digit string that indicates the maximum string length,
189the number of digits to appear after the decimal point in floating point
190output, or the minimum number of digits to appear in numeric output.
191.It Cm fmt
192An optional output format specifier which is one of
193.Cm D ,
194.Cm O ,
195.Cm U ,
196.Cm X ,
197.Cm F ,
198or
199.Cm S .
200These represent signed decimal output, octal output, unsigned decimal
201output, hexadecimal output, floating point output, and string output,
202respectively.
203Some output formats do not apply to all fields.
204Floating point output only applies to timespec fields (the
205.Cm a ,
206.Cm m ,
207and
208.Cm c
209fields).
210.Pp
211The special output specifier
212.Cm S
213may be used to indicate that the output, if
214applicable, should be in string format.
215May be used in combination with
216.Bl -tag -width Ds
217.It Cm amc
218Display date in
219.Xr strftime 3
220format.
221.It Cm dr
222Display actual device name.
223.It Cm gu
224Display group or user name.
225.It Cm p
226Display the mode of
227.Ar file
228as in
229.Ic ls -lTd .
230.It Cm N
231Displays the name of
232.Ar file .
233.It Cm T
234Displays the type of
235.Ar file .
236.It Cm Y
237Insert a
238.Dq "\ ->\ "
239into the output.
240Note that the default output format
241for
242.Cm Y
243is a string, but if specified explicitly, these four characters are
244prepended.
245.El
246.It Cm sub
247An optional sub field specifier (high, middle, low).
248Only applies to
249the
250.Cm p ,
251.Cm d ,
252.Cm r ,
253and
254.Cm T
255output formats.
256It can be one of the following:
257.Bl -tag -width Ds
258.It Cm H
259High \(em specifies the major number for devices from
260.Cm r
261or
262.Cm d ,
263the user bits for permissions from the string form of
264.Cm p ,
265the file type bits from the numeric forms of
266.Cm p ,
267and the long output form of
268.Cm T .
269.It Cm L
270Low \(em specifies the minor number for devices from
271.Cm r
272or
273.Cm d ,
274the other bits for permissions from the string form of
275.Cm p ,
276the user, group, and other bits from the numeric forms of
277.Cm p ,
278and the
279.Ic ls -F
280style output character for file type when used with
281.Cm T
282(the use of
283.Cm L
284for this is optional).
285.It Cm M
286Middle \(em specifies the group bits for permissions from the
287string output form of
288.Cm p ,
289or the
290suid, sgid, and sticky bits for the numeric forms of
291.Cm p .
292.El
293.It Cm datum
294A required field specifier, being one of the following:
295.Bl -tag -width Ds
296.It Cm d
297Device upon which
298.Ar file
299resides
300.Pq Fa st_dev .
301.It Cm i
302.Ar file Ns 's
303inode number
304.Pq Fa st_ino .
305.It Cm p
306File type and permissions
307.Pq Fa st_mode .
308.It Cm l
309Number of hard links to
310.Ar file
311.Pq Fa st_nlink .
312.It Cm u , g
313User-id and group-id of
314.Ar file Ns 's
315owner
316.Pq Fa st_uid , st_gid .
317.It Cm r
318Device number for character and block device special files
319.Pq Fa st_rdev .
320.It Cm a , m , c , B
321The time
322.Ar file
323was last accessed or modified, or when the inode was last changed, or
324the birth time of the inode
325.Pq Fa st_atime , st_mtime , st_ctime , st_birthtime .
326If the file system does not support birth time, the value is undefined.
327.It Cm z
328The size of
329.Ar file
330in bytes
331.Pq Fa st_size .
332.It Cm b
333Number of blocks allocated for
334.Ar file
335.Pq Fa st_blocks .
336.It Cm k
337Optimal file system I/O operation block size
338.Pq Fa st_blksize .
339.It Cm f
340User defined flags for
341.Ar file
342.Pq Fa st_flags .
343.It Cm v
344Inode generation number
345.Pq Fa st_gen .
346.El
347.Pp
348The following four field specifiers are not drawn directly from the
349data in struct stat, but are:
350.Bl -tag -width Ds
351.It Cm N
352The name of the file.
353.It Cm T
354The file type, either as in
355.Ic ls -F
356or in a more descriptive form if the sub field specifier
357.Cm H
358is given.
359.It Cm Y
360The target of a symbolic link.
361.It Cm Z
362Expands to
363.Ar major , Ns Ar minor
364from the rdev field for character or block
365special devices and gives size output for all others.
366.El
367.El
368.Pp
369Only the
370.Cm %
371and the field specifier are required.
372Most field specifiers default to
373.Cm U
374as an output form, with the
375exception of
376.Cm p
377which defaults to
378.Cm O ;
379.Cm a , m ,
380and
381.Cm c
382which default to
383.Cm D ;
384and
385.Cm Y , T ,
386and
387.Cm N ,
388which default to
389.Cm S .
390.Sh EXIT STATUS
391.Ex -std stat
392.Sh EXAMPLES
393Given a symbolic link
394.Pa foo
395that points from
396.Pa /tmp/foo
397to
398.Pa / ,
399you would use
400.Nm
401as follows:
402.Bd -literal -offset indent
403\*(Gt stat -F /tmp/foo
404lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -\*(Gt /
405
406\*(Gt stat -LF /tmp/foo
407drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/
408.Ed
409.Pp
410To initialize some shell-variables, you could use the
411.Fl s
412flag as follows:
413.Bd -literal -offset indent
414\*(Gt csh
415% eval set `stat -s .cshrc`
416% echo $st_size $st_mtime
4171148 1015432481
418
419\*(Gt sh
420$ eval $(stat -s .profile)
421$ echo $st_size $st_mtime
4221148 1015432481
423.Ed
424.Pp
425In order to get a list of the kind of files including files pointed to if the
426file is a symbolic link, you could use the following format:
427.Bd -literal -offset indent
428$ stat -f "%N: %HT%SY" /tmp/*
429/tmp/bar: Symbolic Link -\*(Gt /tmp/foo
430/tmp/output25568: Regular File
431/tmp/blah: Directory
432/tmp/foo: Symbolic Link -\*(Gt /
433.Ed
434.Pp
435In order to get a list of the devices, their types and the major and minor
436device numbers, formatted with tabs and linebreaks, you could use the
437following format:
438.Bd -literal -offset 4n
439stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/*
440[...]
441Name: /dev/xfs0
442        Type: Character Device
443        Major: 51
444        Minor: 0
445
446Name: /dev/zero
447        Type: Character Device
448        Major: 2
449        Minor: 12
450.Ed
451.Pp
452In order to determine the permissions set on a file separately, you could use
453the following format:
454.Bd -literal -offset indent
455\*(Gt stat -f "%Sp -\*(Gt owner=%SHp group=%SMp other=%SLp" .
456drwxr-xr-x -\*(Gt owner=rwx group=r-x other=r-x
457.Ed
458.Pp
459In order to determine the three files that have been modified most recently,
460you could use the following format:
461.Bd -literal -offset indent
462\*(Gt stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2-
463Apr 25 11:47:00 2002 /tmp/blah
464Apr 25 10:36:34 2002 /tmp/bar
465Apr 24 16:47:35 2002 /tmp/foo
466.Ed
467.Sh SEE ALSO
468.Xr file 1 ,
469.Xr ls 1 ,
470.Xr readlink 1 ,
471.Xr lstat 2 ,
472.Xr readlink 2 ,
473.Xr stat 2 ,
474.Xr printf 3 ,
475.Xr strftime 3
476.Sh HISTORY
477A
478.Nm
479utility first appeared in
480.At v1
481but disappeared after
482.At v4 .
483It reappeared in
484.Nx 1.6
485and has been available since
486.Ox 3.8 .
487.Sh AUTHORS
488.An -nosplit
489The
490.Nm
491utility was written by
492.An Andrew Brown Aq Mt atatat@NetBSD.org .
493This man page was written by
494.An Jan Schaumann Aq Mt jschauma@NetBSD.org .
495