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