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