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