xref: /openbsd/usr.bin/stat/stat.1 (revision 91f110e0)
1.\"	$OpenBSD: stat.1,v 1.21 2013/07/16 00:07:52 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: July 16 2013 $
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
127Display information in ``shell output'', suitable for initializing variables.
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 ``0x'' prepended to it.
162.It Cm +
163Asserts that a sign indicating whether a number is positive or negative
164should always be printed.
165Non-negative numbers are not usually printed
166with a sign.
167.It Cm -
168Aligns string output to the left of the field, instead of to the right.
169.It Cm 0
170Sets the fill character for left padding to the 0 character, instead of
171a space.
172.It space
173Reserves a space at the front of non-negative signed output fields.
174A
175.Sq Cm +
176overrides a space if both are used.
177.El
178.Pp
179Then the following fields:
180.Bl -tag -width Ds
181.It Cm size
182An optional decimal digit string specifying the minimum field width.
183.It Cm prec
184An optional precision composed of a decimal point
185.Sq Cm \&.
186and a decimal digit string that indicates the maximum string length,
187the number of digits to appear after the decimal point in floating point
188output, or the minimum number of digits to appear in numeric output.
189.It Cm fmt
190An optional output format specifier which is one of
191.Cm D ,
192.Cm O ,
193.Cm U ,
194.Cm X ,
195.Cm F ,
196or
197.Cm S .
198These represent signed decimal output, octal output, unsigned decimal
199output, hexadecimal output, floating point output, and string output,
200respectively.
201Some output formats do not apply to all fields.
202Floating point output only applies to timespec fields (the
203.Cm a ,
204.Cm m ,
205and
206.Cm c
207fields).
208.Pp
209The special output specifier
210.Cm S
211may be used to indicate that the output, if
212applicable, should be in string format.
213May be used in combination with
214.Bl -tag -width Ds
215.It Cm amc
216Display date in
217.Xr strftime 3
218format.
219.It Cm dr
220Display actual device name.
221.It Cm gu
222Display group or user name.
223.It Cm p
224Display the mode of
225.Ar file
226as in
227.Ic ls -lTd .
228.It Cm N
229Displays the name of
230.Ar file .
231.It Cm T
232Displays the type of
233.Ar file .
234.It Cm Y
235Insert a `` -\*(Gt '' into the output.
236Note that the default output format
237for
238.Cm Y
239is a string, but if specified explicitly, these four characters are
240prepended.
241.El
242.It Cm sub
243An optional sub field specifier (high, middle, low).
244Only applies to
245the
246.Cm p ,
247.Cm d ,
248.Cm r ,
249and
250.Cm T
251output formats.
252It can be one of the following:
253.Bl -tag -width Ds
254.It Cm H
255``High'' -- specifies the major number for devices from
256.Cm r
257or
258.Cm d ,
259the ``user'' bits for permissions from the string form of
260.Cm p ,
261the file ``type'' bits from the numeric forms of
262.Cm p ,
263and the long output form of
264.Cm T .
265.It Cm L
266``Low'' -- specifies the minor number for devices from
267.Cm r
268or
269.Cm d ,
270the ``other'' bits for permissions from the string form of
271.Cm p ,
272the ``user'', ``group'', and ``other'' bits from the numeric forms of
273.Cm p ,
274and the
275.Ic ls -F
276style output character for file type when used with
277.Cm T
278(the use of
279.Cm L
280for this is optional).
281.It Cm M
282``Middle'' -- specifies the ``group'' bits for permissions from the
283string output form of
284.Cm p ,
285or the ``suid'', ``sgid'', and ``sticky'' bits for the numeric forms of
286.Cm p .
287.El
288.It Cm datum
289A required field specifier, being one of the following:
290.Bl -tag -width Ds
291.It Cm d
292Device upon which
293.Ar file
294resides
295.Pq Fa st_dev .
296.It Cm i
297.Ar file Ns 's
298inode number
299.Pq Fa st_ino .
300.It Cm p
301File type and permissions
302.Pq Fa st_mode .
303.It Cm l
304Number of hard links to
305.Ar file
306.Pq Fa st_nlink .
307.It Cm u , g
308User-id and group-id of
309.Ar file Ns 's
310owner
311.Pq Fa st_uid , st_gid .
312.It Cm r
313Device number for character and block device special files
314.Pq Fa st_rdev .
315.It Cm a , m , c , B
316The time
317.Ar file
318was last accessed or modified, or when the inode was last changed, or
319the birth time of the inode
320.Pq Fa st_atime , st_mtime , st_ctime , st_birthtime .
321If the file system does not support birth time, the value is undefined.
322.It Cm z
323The size of
324.Ar file
325in bytes
326.Pq Fa st_size .
327.It Cm b
328Number of blocks allocated for
329.Ar file
330.Pq Fa st_blocks .
331.It Cm k
332Optimal file system I/O operation block size
333.Pq Fa st_blksize .
334.It Cm f
335User defined flags for
336.Ar file
337.Pq Fa st_flags .
338.It Cm v
339Inode generation number
340.Pq Fa st_gen .
341.El
342.Pp
343The following four field specifiers are not drawn directly from the
344data in struct stat, but are:
345.Bl -tag -width Ds
346.It Cm N
347The name of the file.
348.It Cm T
349The file type, either as in
350.Ic ls -F
351or in a more descriptive form if the sub field specifier
352.Cm H
353is given.
354.It Cm Y
355The target of a symbolic link.
356.It Cm Z
357Expands to ``major,minor'' from the rdev field for character or block
358special devices and gives size output for all others.
359.El
360.El
361.Pp
362Only the
363.Cm %
364and the field specifier are required.
365Most field specifiers default to
366.Cm U
367as an output form, with the
368exception of
369.Cm p
370which defaults to
371.Cm O ;
372.Cm a , m ,
373and
374.Cm c
375which default to
376.Cm D ;
377and
378.Cm Y , T ,
379and
380.Cm N ,
381which default to
382.Cm S .
383.Sh EXIT STATUS
384.Ex -std stat
385.Sh EXAMPLES
386Given a symbolic link ``foo'' that points from /tmp/foo to /, you would use
387.Nm
388as follows:
389.Bd -literal -offset indent
390\*(Gt stat -F /tmp/foo
391lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -\*(Gt /
392
393\*(Gt stat -LF /tmp/foo
394drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/
395.Ed
396.Pp
397To initialize some shell-variables, you could use the
398.Fl s
399flag as follows:
400.Bd -literal -offset indent
401\*(Gt csh
402% eval set `stat -s .cshrc`
403% echo $st_size $st_mtime
4041148 1015432481
405
406\*(Gt sh
407$ eval $(stat -s .profile)
408$ echo $st_size $st_mtime
4091148 1015432481
410.Ed
411.Pp
412In order to get a list of the kind of files including files pointed to if the
413file is a symbolic link, you could use the following format:
414.Bd -literal -offset indent
415$ stat -f "%N: %HT%SY" /tmp/*
416/tmp/bar: Symbolic Link -\*(Gt /tmp/foo
417/tmp/output25568: Regular File
418/tmp/blah: Directory
419/tmp/foo: Symbolic Link -\*(Gt /
420.Ed
421.Pp
422In order to get a list of the devices, their types and the major and minor
423device numbers, formatted with tabs and linebreaks, you could use the
424following format:
425.Bd -literal -offset 4n
426stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/*
427[...]
428Name: /dev/xfs0
429        Type: Character Device
430        Major: 51
431        Minor: 0
432
433Name: /dev/zero
434        Type: Character Device
435        Major: 2
436        Minor: 12
437.Ed
438.Pp
439In order to determine the permissions set on a file separately, you could use
440the following format:
441.Bd -literal -offset indent
442\*(Gt stat -f "%Sp -\*(Gt owner=%SHp group=%SMp other=%SLp" .
443drwxr-xr-x -\*(Gt owner=rwx group=r-x other=r-x
444.Ed
445.Pp
446In order to determine the three files that have been modified most recently,
447you could use the following format:
448.Bd -literal -offset indent
449\*(Gt stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2-
450Apr 25 11:47:00 2002 /tmp/blah
451Apr 25 10:36:34 2002 /tmp/bar
452Apr 24 16:47:35 2002 /tmp/foo
453.Ed
454.Sh SEE ALSO
455.Xr file 1 ,
456.Xr ls 1 ,
457.Xr readlink 1 ,
458.Xr lstat 2 ,
459.Xr readlink 2 ,
460.Xr stat 2 ,
461.Xr printf 3 ,
462.Xr strftime 3
463.Sh HISTORY
464The
465.Nm
466utility first appeared in
467.Ox 3.8 .
468.Sh AUTHORS
469.An -nosplit
470The
471.Nm
472utility was written by
473.An Andrew Brown Aq Mt atatat@NetBSD.org .
474This man page was written by
475.An Jan Schaumann Aq Mt jschauma@NetBSD.org .
476