xref: /netbsd-src/usr.bin/stat/stat.1 (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1.\"	$NetBSD: stat.1,v 1.15 2005/01/08 03:46:05 yamt 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.Dd December 31, 2004
38.Dt STAT 1
39.Os
40.Sh NAME
41.Nm stat ,
42.Nm readlink
43.Nd display file status
44.Sh SYNOPSIS
45.Nm
46.Op Fl FLnq
47.Oo
48.Fl f Ar format |
49.Fl l |
50.Fl r |
51.Fl s |
52.Fl x
53.Oc
54.Op Fl t Ar timefmt
55.Op Ar
56.Nm readlink
57.Op Fl n
58.Op Ar
59.Sh DESCRIPTION
60The
61.Nm
62utility displays information about the file pointed to by
63.Ar file .
64Read, write, or execute permissions of the named file are not required, but
65all directories listed in the path name leading to the file must be
66searchable.
67If no argument is given,
68.Nm
69displays information about the file descriptor for standard input.
70.Pp
71When invoked as
72.Nm readlink ,
73only the target of the symbolic link is printed.
74If the given argument is not a symbolic link,
75.Nm readlink
76will print nothing and exit with an error.
77.Pp
78The information displayed is obtained by calling
79.Xr lstat 2
80with the given argument and evaluating the returned structure.
81.Pp
82The options are as follows:
83.Bl -tag -width XFXformatXXX
84.It Fl F
85As in
86.Ic ls ,
87display a slash
88.Pq Sq /
89immediately after each pathname that is a directory, an
90asterisk
91.Pq Sq *
92after each that is executable, an at sign
93.Pq Sq @
94after each symbolic link, a percent sign
95.Pq Sq %
96after each whiteout, an equal sign
97.Pq Sq =
98after each socket, and a vertical bar
99.Pq Sq \&|
100after each that is a FIFO.
101The use of
102.Fl F
103implies
104.Fl l .
105.It Fl f Ar format
106Display information using the specified format.
107See the
108.Sx FORMATS
109section for a description of valid formats.
110.It Fl L
111Use
112.Xr stat 2
113instead of
114.Xr lstat 2 .
115The information reported by
116.Nm
117will refer to the target of
118.Ar file ,
119if file is a symbolic link, and not to
120.Ar file
121itself.
122.It Fl l
123Display output in
124.Ic ls Fl lT
125format.
126.It Fl n
127Do not force a newline to appear at the end of each piece of output.
128.It Fl q
129Suppress failure messages if calls to
130.Xr stat 2
131or
132.Xr lstat 2
133fail.
134When run as
135.Nm readlink ,
136error messages are automatically suppressed.
137.It Fl r
138Display raw information.
139That is, for all the fields in the stat-structure,
140display the raw, numerical value (for example, times in seconds since the
141epoch, etc.)
142.It Fl s
143Display information in
144.Dq shell output ,
145suitable for initializing variables.
146.It Fl t Ar timefmt
147Display timestamps using the specified format.
148This format is
149passed directly to
150.Xr strftime 3 .
151.It Fl x
152Display information in a more verbose way as known from some Linux
153distributions.
154.El
155.Ss FORMATS
156Format strings are similar to
157.Xr printf 3
158formats in that they start with
159.Cm % ,
160are then followed by a sequence of formatting characters, and end in
161a character that selects the field of the struct stat which is to be
162formatted.
163If the
164.Cm %
165is immediately followed by one of
166.Cm n ,
167.Cm t ,
168.Cm % ,
169or
170.Cm @ ,
171then a newline character, a tab character, a percent character,
172or the current file number is printed, otherwise the string is
173examined for the following:
174.Pp
175Any of the following optional flags:
176.Bl -tag -width Ds
177.It Cm #
178Selects an alternate output form for octal and hexadecimal output.
179Non-zero octal output will have a leading zero, and non-zero
180hexadecimal output will have
181.Dq 0x
182prepended to it.
183.It Cm +
184Asserts that a sign indicating whether a number is positive or negative
185should always be printed.
186Non-negative numbers are not usually printed with a sign.
187.It Cm -
188Aligns string output to the left of the field, instead of to the right.
189.It Cm 0
190Sets the fill character for left padding to the 0 character, instead of
191a space.
192.It space
193Reserves a space at the front of non-negative signed output fields.
194A
195.Sq Cm +
196overrides a space if both are used.
197.El
198.Pp
199Then the following fields:
200.Bl -tag -width Ds
201.It Cm size
202An optional decimal digit string specifying the minimum field width.
203.It Cm prec
204An optional precision composed of a decimal point
205.Sq Cm \&.
206and a decimal digit string that indicates the maximum string length,
207the number of digits to appear after the decimal point in floating point
208output, or the minimum number of digits to appear in numeric output.
209.It Cm fmt
210An optional output format specifier which is one of
211.Cm D ,
212.Cm O ,
213.Cm U ,
214.Cm X ,
215.Cm F ,
216or
217.Cm S .
218These represent signed decimal output, octal output, unsigned decimal
219output, hexadecimal output, floating point output, and string output,
220respectively.
221Some output formats do not apply to all fields.
222Floating point output only applies to timespec fields (the
223.Cm a ,
224.Cm m ,
225and
226.Cm c
227fields).
228.Pp
229The special output specifier
230.Cm S
231may be used to indicate that the output, if
232applicable, should be in string format.
233May be used in combination with
234.Bl -tag -width Ds
235.It Cm amc
236Display date in strftime(3) format.
237.It Cm dr
238Display actual device name.
239.It Cm gu
240Display group or user name.
241.It Cm p
242Display the mode of
243.Ar file
244as in
245.Ic ls -lTd .
246.It Cm N
247Displays the name of
248.Ar file .
249.It Cm T
250Displays the type of
251.Ar file .
252.It Cm Y
253Insert a `` -\*[Gt] '' into the output.
254Note that the default output format for
255.Cm Y
256is a string, but if specified explicitly, these four characters are
257prepended.
258.El
259.It Cm sub
260An optional sub field specifier (high, middle, low).
261Only applies to the
262.Cm p ,
263.Cm d ,
264.Cm r ,
265and
266.Cm T
267output formats.
268It can be one of the following:
269.Bl -tag -width Ds
270.It Cm H
271.Dq High
272-- specifies the major number for devices from
273.Cm r
274or
275.Cm d ,
276the
277.Dq user
278bits for permissions from the string form of
279.Cm p ,
280the file
281.Dq type
282bits from the numeric forms of
283.Cm p ,
284the long output form of
285.Cm T ,
286and the directory path from the
287.Cm N
288output similar to what
289.Xr dirname 1
290would show.
291.It Cm L
292.Dq Low
293-- specifies the minor number for devices from
294.Cm r
295or
296.Cm d ,
297the
298.Dq other
299bits for permissions from the string form of
300.Cm p ,
301the
302.Dq user ,
303.Dq group ,
304and
305.Dq other
306bits from the numeric forms of
307.Cm p ,
308the
309.Ic ls -F
310style output character for file type when used with
311.Cm T
312(the use of
313.Cm L
314for this is optional), and the filename component of the
315.Cm N
316output form similar to what
317.Xr basename 1
318would display.
319.It Cm M
320.Dq Middle
321-- specifies the
322.Dq group
323bits for permissions from the string output form of
324.Cm p ,
325or the
326.Dq suid ,
327.Dq sgid ,
328and
329.Dq sticky
330bits for the numeric forms of
331.Cm p .
332.El
333.It Cm datum
334A required field specifier, being one of the following:
335.Bl -tag -width 11n
336.It Cm d
337Device upon which
338.Ar file
339resides.
340.It Cm i
341.Ar file Ap s
342inode number.
343.It Cm p
344File type and permissions.
345.It Cm l
346Number of hard links to
347.Ar file .
348.It Cm u , g
349User-id and group-id of
350.Ar file Ap s
351owner.
352.It Cm r
353Device number for character and block device special files.
354.It Cm a , m , c , B
355The time
356.Ar file
357was last accessed or modified, of when the inode was last changed, or
358the birth time of the inode.
359.It Cm z
360The size of
361.Ar file
362in bytes.
363.It Cm b
364Number of blocks allocated for
365.Ar file .
366.It Cm k
367Optimal file system I/O operation block size.
368.It Cm f
369User defined flags for
370.Ar file .
371.It Cm v
372Inode generation number.
373.El
374.Pp
375The following four field specifiers are not drawn directly from the
376data in struct stat, but are
377.Bl -tag -width Ds
378.It Cm N
379The name of the file.
380.It Cm T
381The file type, either as in
382.Ic ls -F
383or in a more descriptive form if the sub field specifier
384.Cm H
385is given.
386.It Cm Y
387The target of a symbolic link.
388.It Cm Z
389Expands to
390.Dq major,minor
391from the rdev field for character or block
392special devices and gives size output for all others.
393.El
394.El
395.Pp
396Only the
397.Cm %
398and the field specifier are required.
399Most field specifiers default to
400.Cm U
401as an output form, with the
402exception of
403.Cm p
404which defaults to
405.Cm O ,
406.Cm a , m ,
407and
408.Cm c
409which default to
410.Cm D ,
411and
412.Cm Y , T ,
413and
414.Cm N ,
415which default to
416.Cm S .
417.Sh EXIT STATUS
418.Nm
419exits 0 on success, and \*[Gt]0 if an error occurred.
420.Sh EXAMPLES
421If no options are specified, the default format is
422"%d %i %Sp %l %Su %Sg %r %z \\"%Sa\\" \\"%Sm\\" \\"%Sc\\" \\"%SB\\" %k %b %#Xf %N".
423.Bd -literal -offset indent
424\*[Gt] stat /tmp/bar
4250 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
426.Ed
427.Pp
428Given a symbolic link
429.Dq foo
430that points from
431.Pa /tmp/foo
432to
433.Pa / ,
434you would use
435.Nm
436as follows:
437.Bd -literal -offset indent
438\*[Gt] stat -F /tmp/foo
439lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -\*[Gt] /
440
441\*[Gt] stat -LF /tmp/foo
442drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/
443.Ed
444.Pp
445To initialize some shell-variables, you could use the
446.Fl s
447flag as follows:
448.Bd -literal -offset indent
449\*[Gt] csh
450% eval set `stat -s .cshrc`
451% echo $st_size $st_mtime
4521148 1015432481
453
454\*[Gt] sh
455$ eval $(stat -s .profile)
456$ echo $st_size $st_mtime
4571148 1015432481
458.Ed
459.Pp
460In order to get a list of the kind of files including files pointed to if the
461file is a symbolic link, you could use the following format:
462.Bd -literal -offset indent
463$ stat -f "%N: %HT%SY" /tmp/*
464/tmp/bar: Symbolic Link -\*[Gt] /tmp/foo
465/tmp/output25568: Regular File
466/tmp/blah: Directory
467/tmp/foo: Symbolic Link -\*[Gt] /
468.Ed
469.Pp
470In order to get a list of the devices, their types and the major and minor
471device numbers, formatted with tabs and linebreaks, you could use the
472following format:
473.Bd -literal -offset indent
474stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/*
475[...]
476Name: /dev/wt8
477        Type: Block Device
478        Major: 3
479        Minor: 8
480
481Name: /dev/zero
482        Type: Character Device
483        Major: 2
484        Minor: 12
485.Ed
486.Pp
487In order to determine the permissions set on a file separately, you could use
488the following format:
489.Bd -literal -offset indent
490\*[Gt] stat -f "%Sp -\*[Gt] owner=%SHp group=%SMp other=%SLp" .
491drwxr-xr-x -\*[Gt] owner=rwx group=r-x other=r-x
492.Ed
493.Pp
494In order to determine the three files that have been modified most recently,
495you could use the following format:
496.Bd -literal -offset indent
497\*[Gt] stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2-
498Apr 25 11:47:00 2002 /tmp/blah
499Apr 25 10:36:34 2002 /tmp/bar
500Apr 24 16:47:35 2002 /tmp/foo
501.Ed
502.Sh SEE ALSO
503.Xr file 1 ,
504.Xr basename 1 ,
505.Xr dirname 1 ,
506.Xr ls 1 ,
507.Xr lstat 2 ,
508.Xr readlink 2 ,
509.Xr stat 2 ,
510.Xr printf 3 ,
511.Xr strftime 3
512.Sh HISTORY
513The
514.Nm
515utility appeared in
516.Nx 1.6 .
517.Sh AUTHORS
518.An -nosplit
519The
520.Nm
521utility was written by
522.An Andrew Brown
523.Aq atatat@NetBSD.org .
524This man page was written by
525.An Jan Schaumann
526.Aq jschauma@NetBSD.org .
527