1.\" $NetBSD: stat.1,v 1.29 2011/02/16 23:08:29 christos Exp $ 2.\" 3.\" Copyright (c) 2002-2005 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 February 16, 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 octal and hexadecimal output. 206Non-zero octal output will have a leading zero, and non-zero 207hexadecimal output will have 208.Dq 0x 209prepended to it. 210.It Cm + 211Asserts that a sign indicating whether a number is positive or negative 212should always be printed. 213Non-negative numbers are not usually printed with a sign. 214.It Cm - 215Aligns string output to the left of the field, instead of to the right. 216.It Cm 0 217Sets the fill character for left padding to the 0 character, instead of 218a space. 219.It space 220Reserves a space at the front of non-negative signed output fields. 221A 222.Sq Cm + 223overrides a space if both are used. 224.El 225.Pp 226Then the following fields: 227.Bl -tag -width Ds 228.It Cm size 229An optional decimal digit string specifying the minimum field width. 230.It Cm prec 231An optional precision composed of a decimal point 232.Sq Cm \&. 233and a decimal digit string that indicates the maximum string length, 234the number of digits to appear after the decimal point in floating point 235output, or the minimum number of digits to appear in numeric output. 236.It Cm fmt 237An optional output format specifier which is one of 238.Cm D , 239.Cm O , 240.Cm U , 241.Cm X , 242.Cm 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 timespec fields (the 250.Cm a , 251.Cm m , 252and 253.Cm c 254fields). 255.Pp 256The special output specifier 257.Cm S 258may be used to indicate that the output, if 259applicable, should be in string format. 260May be used in combination with 261.Bl -tag -width Ds 262.It Cm amc 263Display date in strftime(3) format. 264.It Cm dr 265Display actual device name. 266.It Cm gu 267Display group or user name. 268.It Cm p 269Display the mode of 270.Ar file 271as in 272.Ic ls -lTd . 273.It Cm N 274Displays the name of 275.Ar file . 276.It Cm T 277Displays the type of 278.Ar file . 279.It Cm Y 280Insert a `` -\*[Gt] '' into the output. 281Note that the default output format for 282.Cm Y 283is a string, but if specified explicitly, these four characters are 284prepended. 285.El 286.It Cm sub 287An optional sub field specifier (high, middle, or low). 288Only applies to the 289.Cm p , 290.Cm d , 291.Cm r , 292.Cm T , 293.Cm N , 294and 295.Cm z 296output formats. 297It can be one of the following: 298.Bl -tag -width Ds 299.It Cm H 300.Dq High 301-- depending on the 302.Cm datum : 303.Bl -tag -compact -width door 304.It Cm d , r 305Major number for devices 306.It Cm p 307.Dq User 308bits from the string form of permissions or the file 309.Dq type 310bits from the numeric forms 311.It Cm T 312The long output form of file type 313.It Cm N 314Directory path of the file, similar to what 315.Xr dirname 1 316would show 317.It Cm z 318File size, rounded to the nearest gigabyte 319.El 320.It Cm M 321.Dq Middle 322-- depending on the 323.Cm datum : 324.Bl -tag -compact -width door 325.It Cm p 326The 327.Dq group 328bits from the string form of permissions or the 329.Dq suid , 330.Dq sgid , 331and 332.Dq sticky 333bits from the numeric forms 334.It Cm z 335File size, rounded to the nearest megabyte 336.El 337.It Cm L 338.Dq Low 339-- depending on the 340.Cm datum : 341.Bl -tag -compact -width door 342.It Cm r , d 343Minor number for devices 344.It Cm p 345The 346.Dq other 347bits from the string form of permissions or the 348.Dq user , 349.Dq group , 350and 351.Dq other 352bits from the numeric forms 353.It Cm T 354The 355.Ic ls -F 356style output character for file type (the use of 357.Cm L 358here is optional) 359.It Cm N 360Base filename of the file, similar to what 361.Xr basename 1 362would show 363.It Cm z 364File size, rounded to the nearest kilobyte 365.El 366.El 367.It Cm datum 368A required field specifier, being one of the following: 369.Bl -tag -width 11n 370.It Cm d 371Device upon which 372.Ar file 373resides 374.Pq Fa st_dev . 375.It Cm i 376.Ar file Ap s 377inode number 378.Pq Fa st_ino . 379.It Cm p 380File type and permissions 381.Pq Fa st_mode . 382.It Cm l 383Number of hard links to 384.Ar file 385.Pq Fa st_nlink . 386.It Cm u , g 387User-id and group-id of 388.Ar file Ap s 389owner 390.Pq Fa st_uid , st_gid . 391.It Cm r 392Device number for character and block device special files 393.Pq Fa st_rdev . 394.It Cm a , m , c , B 395The time 396.Ar file 397was last accessed or modified, or when the inode was last changed, or 398the birth time of the inode 399.Pq Fa st_atime , st_mtime , st_ctime, st_birthtime . 400.It Cm z 401The size of 402.Ar file 403in bytes 404.Pq Fa st_size . 405.It Cm b 406Number of blocks allocated for 407.Ar file 408.Pq Fa st_blocks . 409.It Cm k 410Optimal file system I/O operation block size 411.Pq Fa st_blksize . 412.It Cm f 413User defined flags for 414.Ar file 415.Pq Fa st_flags . 416.It Cm v 417Inode generation number 418.Pq Fa st_gen . 419.El 420.Pp 421The following five field specifiers are not drawn directly from the 422data in struct stat, but are: 423.Bl -tag -width Ds 424.It Cm N 425The name of the file. 426.It Cm R 427The absolute pathname corresponding to the file. 428.It Cm T 429The file type, either as in 430.Ic ls -F 431or in a more descriptive form if the sub field specifier 432.Cm H 433is given. 434.It Cm Y 435The target of a symbolic link. 436.It Cm Z 437Expands to 438.Dq major,minor 439from the rdev field for character or block 440special devices and gives size output for all others. 441.El 442.El 443.Pp 444Only the 445.Cm % 446and the field specifier are required. 447Most field specifiers default to 448.Cm U 449as an output form, with the 450exception of 451.Cm p 452which defaults to 453.Cm O ; 454.Cm a , m , 455and 456.Cm c 457which default to 458.Cm D ; 459and 460.Cm Y , T , 461and 462.Cm N , 463which default to 464.Cm S . 465.Sh EXIT STATUS 466.Nm 467exits 0 on success, and \*[Gt]0 if an error occurred. 468.Sh EXAMPLES 469If no options are specified, the default format is 470"%d %i %Sp %l %Su %Sg %r %z \e"%Sa\e" \e"%Sm\e" \e"%Sc\e" \e"%SB\e" %k %b %#Xf %N". 471.Bd -literal -offset indent 472\*[Gt] stat /tmp/bar 4730 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 474.Ed 475.Pp 476Given a symbolic link 477.Dq foo 478that points from 479.Pa /tmp/foo 480to 481.Pa / , 482you would use 483.Nm 484as follows: 485.Bd -literal -offset indent 486\*[Gt] stat -F /tmp/foo 487lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -\*[Gt] / 488 489\*[Gt] stat -LF /tmp/foo 490drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/ 491.Ed 492.Pp 493To initialize some shell-variables, you could use the 494.Fl s 495flag as follows: 496.Bd -literal -offset indent 497\*[Gt] csh 498% eval set `stat -s .cshrc` 499% echo $st_size $st_mtime 5001148 1015432481 501 502\*[Gt] sh 503$ eval $(stat -s .profile) 504$ echo $st_size $st_mtime 5051148 1015432481 506.Ed 507.Pp 508In order to get a list of the kind of files including files pointed to if the 509file is a symbolic link, you could use the following format: 510.Bd -literal -offset indent 511$ stat -f "%N: %HT%SY" /tmp/* 512/tmp/bar: Symbolic Link -\*[Gt] /tmp/foo 513/tmp/output25568: Regular File 514/tmp/blah: Directory 515/tmp/foo: Symbolic Link -\*[Gt] / 516.Ed 517.Pp 518In order to get a list of the devices, their types and the major and minor 519device numbers, formatted with tabs and linebreaks, you could use the 520following format: 521.Bd -literal -offset indent 522stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/* 523[...] 524Name: /dev/wt8 525 Type: Block Device 526 Major: 3 527 Minor: 8 528 529Name: /dev/zero 530 Type: Character Device 531 Major: 2 532 Minor: 12 533.Ed 534.Pp 535In order to determine the permissions set on a file separately, you could use 536the following format: 537.Bd -literal -offset indent 538\*[Gt] stat -f "%Sp -\*[Gt] owner=%SHp group=%SMp other=%SLp" . 539drwxr-xr-x -\*[Gt] owner=rwx group=r-x other=r-x 540.Ed 541.Pp 542In order to determine the three files that have been modified most recently, 543you could use the following format: 544.Bd -literal -offset indent 545\*[Gt] stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2- 546Apr 25 11:47:00 2002 /tmp/blah 547Apr 25 10:36:34 2002 /tmp/bar 548Apr 24 16:47:35 2002 /tmp/foo 549.Ed 550.Sh SEE ALSO 551.Xr basename 1 , 552.Xr dirname 1 , 553.Xr file 1 , 554.Xr ls 1 , 555.Xr lstat 2 , 556.Xr readlink 2 , 557.Xr stat 2 , 558.Xr printf 3 , 559.Xr strftime 3 560.Sh HISTORY 561The 562.Nm 563utility appeared in 564.Nx 1.6 . 565.Sh AUTHORS 566.An -nosplit 567The 568.Nm 569utility was written by 570.An Andrew Brown 571.Aq atatat@NetBSD.org . 572This man page was written by 573.An Jan Schaumann 574.Aq jschauma@NetBSD.org . 575