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