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