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