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