1.\" $NetBSD: stat.1,v 1.40 2017/09/20 08:57:02 wiz 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 19, 2017 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. 171This is equivalent to specifying 172.Bd -literal 173FMT="st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l st_uid=%u st_gid=%g" 174FMT="$FMT st_rdev=%r st_size=%z st_atime=%Sa st_mtime=%Sm st_ctime=%Sc" 175FMT="$FMT st_birthtime=%SB st_blksize=%k st_blocks=%b st_flags=%f" 176stat -t %s -f "$FMT" . 177.Ed 178Note that if you use a timeformat that contains embedded whitespace or shell 179meta-characters you will need to include appropriate quoting so the 180.Fl s 181output remains valid. 182.It Fl t Ar timefmt 183Display timestamps using the specified format. 184This format is 185passed directly to 186.Xr strftime 3 187with the extension that %f prints nanoseconds if available. 188.It Fl v 189Turn off quiet mode. 190.It Fl x 191Display information in a more verbose way as known from some Linux 192distributions. 193.El 194.Ss FORMATS 195Format strings are similar to 196.Xr printf 3 197formats in that they start with 198.Cm % , 199are then followed by a sequence of formatting characters, and end in 200a character that selects the field of the struct stat which is to be 201formatted. 202If the 203.Cm % 204is immediately followed by one of 205.Cm n , 206.Cm t , 207.Cm % , 208or 209.Cm @ , 210then a newline character, a tab character, a percent character, 211or the current file number is printed, otherwise the string is 212examined for the following: 213.Pp 214Any of the following optional flags: 215.Bl -tag -width Ds 216.It Cm # 217Selects an alternate output form for string, octal and hexadecimal output. 218String output will be encoded in 219.Xr vis 3 220style. 221Non-zero octal output will have a leading zero. 222Non-zero hexadecimal output will have 223.Dq 0x 224prepended to it. 225.It Cm + 226Asserts that a sign indicating whether a number is positive or negative 227should always be printed. 228Non-negative numbers are not usually printed with a sign. 229.It Cm - 230Aligns string output to the left of the field, instead of to the right. 231.It Cm 0 232Sets the fill character for left padding to the 0 character, instead of 233a space. 234.It space 235Reserves a space at the front of non-negative signed output fields. 236A 237.Sq Cm + 238overrides a space if both are used. 239.El 240.Pp 241Then the following fields: 242.Bl -tag -width Ds 243.It Cm size 244An optional decimal digit string specifying the minimum field width. 245.It Cm prec 246An optional precision composed of a decimal point 247.Sq Cm \&. 248and a decimal digit string that indicates the maximum string length, 249the number of digits to appear after the decimal point in floating point 250output, or the minimum number of digits to appear in numeric output. 251.It Cm fmt 252An optional output format specifier which is one of 253.Cm D , 254.Cm O , 255.Cm U , 256.Cm X , 257.Cm F , 258or 259.Cm S . 260These represent signed decimal output, octal output, unsigned decimal 261output, hexadecimal output, floating point output, and string output, 262respectively. 263Some output formats do not apply to all fields. 264Floating point output only applies to timespec fields (the 265.Cm a , 266.Cm m , 267and 268.Cm c 269fields). 270.Pp 271The special output specifier 272.Cm S 273may be used to indicate that the output, if 274applicable, should be in string format. 275May be used in combination with 276.Bl -tag -width Ds 277.It Cm amc 278Display date in 279.Xr strftime 3 280format with the extension that %f prints nanoseconds if available. 281.It Cm dr 282Display actual device name. 283.It Cm gu 284Display group or user name. 285.It Cm p 286Display the mode of 287.Ar file 288as in 289.Ic ls -lTd . 290.It Cm N 291Displays the name of 292.Ar file . 293.It Cm T 294Displays the type of 295.Ar file . 296.It Cm Y 297Insert a `` -> '' into the output. 298Note that the default output format for 299.Cm Y 300is a string, but if specified explicitly, these four characters are 301prepended. 302.El 303.It Cm sub 304An optional sub field specifier (high, middle, or low). 305Only applies to the 306.Cm p , 307.Cm d , 308.Cm r , 309.Cm T , 310.Cm N , 311and 312.Cm z 313output formats. 314It can be one of the following: 315.Bl -tag -width Ds 316.It Cm H 317.Dq High 318-- depending on the 319.Cm datum : 320.Bl -tag -compact -width door 321.It Cm d , r 322Major number for devices 323.It Cm p 324.Dq User 325bits from the string form of permissions or the file 326.Dq type 327bits from the numeric forms 328.It Cm T 329The long output form of file type 330.It Cm N 331Directory path of the file, similar to what 332.Xr dirname 1 333would show 334.It Cm z 335File size, rounded to the nearest gigabyte 336.El 337.It Cm M 338.Dq Middle 339-- depending on the 340.Cm datum : 341.Bl -tag -compact -width door 342.It Cm p 343The 344.Dq group 345bits from the string form of permissions or the 346.Dq suid , 347.Dq sgid , 348and 349.Dq sticky 350bits from the numeric forms 351.It Cm z 352File size, rounded to the nearest megabyte 353.El 354.It Cm L 355.Dq Low 356-- depending on the 357.Cm datum : 358.Bl -tag -compact -width door 359.It Cm r , d 360Minor number for devices 361.It Cm p 362The 363.Dq other 364bits from the string form of permissions or the 365.Dq user , 366.Dq group , 367and 368.Dq other 369bits from the numeric forms 370.It Cm T 371The 372.Ic ls -F 373style output character for file type (the use of 374.Cm L 375here is optional) 376.It Cm N 377Base filename of the file, similar to what 378.Xr basename 1 379would show 380.It Cm z 381File size, rounded to the nearest kilobyte 382.El 383.El 384.It Cm datum 385A required field specifier, being one of the following: 386.Bl -tag -width 11n 387.It Cm d 388Device upon which 389.Ar file 390resides 391.Pq Fa st_dev . 392.It Cm i 393.Ar file Ap s 394inode number 395.Pq Fa st_ino . 396.It Cm p 397File type and permissions 398.Pq Fa st_mode . 399.It Cm l 400Number of hard links to 401.Ar file 402.Pq Fa st_nlink . 403.It Cm u , g 404User-id and group-id of 405.Ar file Ap s 406owner 407.Pq Fa st_uid , st_gid . 408.It Cm r 409Device number for character and block device special files 410.Pq Fa st_rdev . 411.It Cm a , m , c , B 412The time 413.Ar file 414was last accessed or modified, or when the inode was last changed, or 415the birth time of the inode 416.Pq Fa st_atime , st_mtime , st_ctime , st_birthtime . 417.It Cm z 418The size of 419.Ar file 420in bytes 421.Pq Fa st_size . 422.It Cm b 423Number of blocks allocated for 424.Ar file 425.Pq Fa st_blocks . 426.It Cm k 427Optimal file system I/O operation block size 428.Pq Fa st_blksize . 429.It Cm f 430User defined flags for 431.Ar file 432.Pq Fa st_flags . 433.It Cm v 434Inode generation number 435.Pq Fa st_gen . 436.El 437.Pp 438The following five field specifiers are not drawn directly from the 439data in struct stat, but are: 440.Bl -tag -width Ds 441.It Cm N 442The name of the file. 443.It Cm R 444The absolute pathname corresponding to the file. 445.It Cm T 446The file type, either as in 447.Ic ls -F 448or in a more descriptive form if the sub field specifier 449.Cm H 450is given. 451.It Cm Y 452The target of a symbolic link. 453.It Cm Z 454Expands to 455.Dq Ar major , Ns Ar minor 456from the rdev field for character or block 457special devices and gives size output for all others. 458.El 459.El 460.Pp 461Only the 462.Cm % 463and the field specifier are required. 464Most field specifiers default to 465.Cm U 466as an output form, with the 467exception of 468.Cm p 469which defaults to 470.Cm O ; 471.Cm a , m , 472and 473.Cm c 474which default to 475.Cm D ; 476and 477.Cm Y , T , 478and 479.Cm N , 480which default to 481.Cm S . 482.Sh EXIT STATUS 483.Ex -std 484.Sh EXAMPLES 485If no options are specified, the default format is 486"%d %i %Sp %l %Su %Sg %r %z \e"%Sa\e" \e"%Sm\e" \e"%Sc\e" \e"%SB\e" %k %b %#Xf %N". 487.Bd -literal -offset indent 488> stat /tmp/bar 4890 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 490.Ed 491.Pp 492This example produces output very similar to that from 493.Ic find ... -ls 494(except that 495.Xr find 1 496displays the time in a different format, and 497.Xr find 1 498sometimes adds one or more spaces after the comma in 499.Dq Ar major , Ns Ar minor 500for device nodes): 501.Bd -literal -offset indent 502> stat -f "%7i %6b %-11Sp %3l %-17Su %-17Sg %9Z %Sm %N%SY" /tmp/bar 503 78852 0 -rw-r--r-- 1 root wheel 0 Jul 8 10:26:03 2004 /tmp/bar 504 505> find /tmp/bar -ls -exit 506 78852 0 -rw-r--r-- 1 root wheel 0 Jul 8 2004 /tmp/bar 507.Ed 508.Pp 509This example produces output very similar to that from 510.Ic ls -lTd 511(except that 512.Xr ls 1 513adjusts the column spacing differently when listing multiple files, 514and 515.Xr ls 1 516adds at least one space after the comma in 517.Dq Ar major , Ns Ar minor 518for device nodes): 519.Bd -literal -offset indent 520> stat -f "%-11Sp %l %Su %Sg %Z %Sm %N%SY" /tmp/bar 521-rw-r--r-- 1 root wheel 0 Jul 8 10:26:03 2004 /tmp/bar 522 523> ls -lTd /tmp/bar 524-rw-r--r-- 1 root wheel 0 Jul 8 10:26:03 2004 /tmp/bar 525.Ed 526.Pp 527Given a symbolic link 528.Dq foo 529that points from 530.Pa /tmp/foo 531to 532.Pa / , 533you would use 534.Nm 535as follows: 536.Bd -literal -offset indent 537> stat -F /tmp/foo 538lrwxrwxrwx 1 jschauma cs 1 Apr 24 16:37:28 2002 /tmp/foo@ -> / 539 540> stat -LF /tmp/foo 541drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/ 542.Ed 543.Pp 544To initialize some shell-variables, you could use the 545.Fl s 546flag as follows: 547.Bd -literal -offset indent 548> csh 549% eval set `stat -s .cshrc` 550% echo $st_size $st_mtime 5511148 1015432481 552 553> sh 554$ eval $(stat -s .profile) 555$ echo $st_size $st_mtime 5561148 1015432481 557.Ed 558.Pp 559In order to get a list of the kind of files including files pointed to if the 560file is a symbolic link, you could use the following format: 561.Bd -literal -offset indent 562$ stat -f "%N: %HT%SY" /tmp/* 563/tmp/bar: Symbolic Link -> /tmp/foo 564/tmp/output25568: Regular File 565/tmp/blah: Directory 566/tmp/foo: Symbolic Link -> / 567.Ed 568.Pp 569In order to get a list of the devices, their types and the major and minor 570device numbers, formatted with tabs and linebreaks, you could use the 571following format: 572.Bd -literal -offset indent 573stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/* 574[...] 575Name: /dev/wt8 576 Type: Block Device 577 Major: 3 578 Minor: 8 579 580Name: /dev/zero 581 Type: Character Device 582 Major: 2 583 Minor: 12 584.Ed 585.Pp 586In order to determine the permissions set on a file separately, you could use 587the following format: 588.Bd -literal -offset indent 589> stat -f "%Sp -> owner=%SHp group=%SMp other=%SLp" . 590drwxr-xr-x -> owner=rwx group=r-x other=r-x 591.Ed 592.Pp 593In order to determine the three files that have been modified most recently, 594you could use the following format: 595.Bd -literal -offset indent 596> stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2- 597Apr 25 11:47:00 2002 /tmp/blah 598Apr 25 10:36:34 2002 /tmp/bar 599Apr 24 16:47:35 2002 /tmp/foo 600.Ed 601.Pp 602User names, group names, and file names that contain spaces 603or other special characters may be encoded in 604.Xr vis 3 605style, using the 606.Cm \&# 607modifier: 608.Bd -literal -offset indent 609> ln -s 'target with spaces' 'link with spaces' 610> stat -f "%#N%#SY" 'link with spaces' 611link\eswith\esspaces -> target\eswith\esspaces 612.Ed 613.Sh SEE ALSO 614.Xr basename 1 , 615.Xr dirname 1 , 616.Xr file 1 , 617.Xr ls 1 , 618.Xr lstat 2 , 619.Xr readlink 2 , 620.Xr stat 2 , 621.Xr printf 3 , 622.Xr strftime 3 623.Sh HISTORY 624The 625.Nm 626utility appeared in 627.Nx 1.6 . 628.Sh AUTHORS 629.An -nosplit 630The 631.Nm 632utility was written by 633.An Andrew Brown 634.Aq atatat@NetBSD.org . 635This man page was written by 636.An Jan Schaumann 637.Aq jschauma@NetBSD.org . 638