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