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