2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
5 .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
6 .\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
8 .\" Permission is granted to make and distribute verbatim copies of this
9 .\" manual provided the copyright notice and this permission notice are
10 .\" preserved on all copies.
12 .\" Permission is granted to copy and distribute modified versions of this
13 .\" manual under the conditions for verbatim copying, provided that the
14 .\" entire resulting derived work is distributed under the terms of a
15 .\" permission notice identical to this one.
17 .\" Since the Linux kernel and libraries are constantly changing, this
18 .\" manual page may be incorrect or out-of-date. The author(s) assume no
19 .\" responsibility for errors or omissions, or for damages resulting from
20 .\" the use of the information contained herein. The author(s) may not
21 .\" have taken the same level of care in the production of this manual,
22 .\" which is licensed free of charge, as they might when working
25 .\" Formatted or processed versions of this manual, if unaccompanied by
26 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Modified by Michael Haardt <michael@moria.de>
29 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30 .\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
31 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
33 .\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
34 .\" Modified 1999-07-06 by aeb & Albert Cahalan
35 .\" Modified 2000-01-07 by aeb
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2007-06-08 mtk: Added example program
38 .\" 2007-07-05 mtk: Added details on underlying system call interfaces
40 .TH STAT 2 2009-09-30 "Linux" "Linux Programmer's Manual"
42 stat, fstat, lstat \- get file status
44 .B #include <sys/types.h>
46 .B #include <sys/stat.h>
48 .B #include <unistd.h>
50 .BI "int stat(const char *" path ", struct stat *" buf );
52 .BI "int fstat(int " fd ", struct stat *" buf );
54 .BI "int lstat(const char *" path ", struct stat *" buf );
57 Feature Test Macro Requirements for glibc (see
58 .BR feature_test_macros (7)):
62 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
65 These functions return information about a file.
66 No permissions are required on the file itself, but \(em in the case of
71 execute (search) permission is required on all of the directories in
73 that lead to the file.
76 stats the file pointed to by
86 is a symbolic link, then the link itself is stat-ed,
87 not the file that it refers to.
92 except that the file to be stat-ed is specified by the file descriptor
95 All of these system calls return a
97 structure, which contains the following fields:
102 dev_t st_dev; /* ID of device containing file */
103 ino_t st_ino; /* inode number */
104 mode_t st_mode; /* protection */
105 nlink_t st_nlink; /* number of hard links */
106 uid_t st_uid; /* user ID of owner */
107 gid_t st_gid; /* group ID of owner */
108 dev_t st_rdev; /* device ID (if special file) */
109 off_t st_size; /* total size, in bytes */
110 blksize_t st_blksize; /* blocksize for file system I/O */
111 blkcnt_t st_blocks; /* number of 512B blocks allocated */
112 time_t st_atime; /* time of last access */
113 time_t st_mtime; /* time of last modification */
114 time_t st_ctime; /* time of last status change */
121 field describes the device on which this file resides.
126 macros may be useful to decompose the device ID in this field.)
130 field describes the device that this file (inode) represents.
134 field gives the size of the file (if it is a regular
135 file or a symbolic link) in bytes.
136 The size of a symlink is the length of the pathname
137 it contains, without a trailing null byte.
141 field indicates the number of blocks allocated to the file, 512-byte units.
142 (This may be smaller than
144 when the file has holes.)
148 field gives the "preferred" blocksize for efficient file system I/O.
149 (Writing to a file in smaller chunks may cause
150 an inefficient read-modify-rewrite.)
152 Not all of the Linux file systems implement all of the time fields.
153 Some file system types allow mounting in such a way that file
154 and/or directory accesses do not cause an update of the
164 and related information in
168 is not updated if a file is opened with the
175 is changed by file accesses, for example, by
182 (of more than zero bytes).
185 may or may not update
190 is changed by file modifications, for example, by
196 (of more than zero bytes).
199 of a directory is changed by the creation or deletion of files
205 changed for changes in owner, group, hard link count, or mode.
209 is changed by writing or by setting inode information
210 (i.e., owner, group, link count, mode, etc.).
212 The following POSIX macros are defined to check the file type using the
218 is it a regular file?
233 symbolic link? (Not in POSIX.1-1996.)
236 socket? (Not in POSIX.1-1996.)
239 The following flags are defined for the
245 S_IFMT 0170000 bit mask for the file type bit fields
246 S_IFSOCK 0140000 socket
247 S_IFLNK 0120000 symbolic link
248 S_IFREG 0100000 regular file
249 S_IFBLK 0060000 block device
250 S_IFDIR 0040000 directory
251 S_IFCHR 0020000 character device
253 S_ISUID 0004000 set UID bit
254 S_ISGID 0002000 set-group-ID bit (see below)
255 S_ISVTX 0001000 sticky bit (see below)
256 S_IRWXU 00700 mask for file owner permissions
257 S_IRUSR 00400 owner has read permission
258 S_IWUSR 00200 owner has write permission
259 S_IXUSR 00100 owner has execute permission
260 S_IRWXG 00070 mask for group permissions
261 S_IRGRP 00040 group has read permission
262 S_IWGRP 00020 group has write permission
263 S_IXGRP 00010 group has execute permission
264 S_IRWXO 00007 mask for permissions for others (not in group)
265 S_IROTH 00004 others have read permission
266 S_IWOTH 00002 others have write permission
267 S_IXOTH 00001 others have execute permission
273 has several special uses.
274 For a directory it indicates that BSD semantics is to be used
275 for that directory: files created there inherit their group ID from
276 the directory, not from the effective group ID of the creating process,
277 and directories created there will also get the
280 For a file that does not have the group execution bit
283 the set-group-ID bit indicates mandatory file/record locking.
287 on a directory means that a file
288 in that directory can be renamed or deleted only by the owner
289 of the file, by the owner of the directory, and by a privileged
292 On success, zero is returned.
293 On error, \-1 is returned, and
295 is set appropriately.
299 Search permission is denied for one of the directories
300 in the path prefix of
303 .BR path_resolution (7).)
313 Too many symbolic links encountered while traversing the path.
326 Out of memory (i.e., kernel memory).
329 A component of the path prefix of
336 refers to a file whose size cannot be represented in the type
338 This can occur when an application compiled on a 32-bit platform without
339 .I -D_FILE_OFFSET_BITS=64
342 on a file whose size exceeds
346 These system calls conform to SVr4, 4.3BSD, POSIX.1-2001.
347 .\" SVr4 documents additional
349 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
350 .\" documents additional
354 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
360 fields may be less portable.
361 (They were introduced in BSD.
362 The interpretation differs between systems,
363 and possibly on a single system when NFS mounts are involved.)
365 POSIX does not describe the
375 bits, but instead demands the use of
384 POSIX.1-1996, but both are present in POSIX.1-2001;
385 the former is from SVID 4, the latter from SUSv2.
387 Unix V7 (and later systems) had
392 prescribes the synonyms
397 Values that have been (or are) in use on various systems:
400 hex name ls octal description
401 f000 S_IFMT 170000 mask for file type
402 0000 000000 SCO out-of-service inode; BSD unknown
403 type; SVID-v2 and XPG2 have both
404 0 and 0100000 for ordinary file
405 1000 S_IFIFO p| 010000 FIFO (named pipe)
406 2000 S_IFCHR c 020000 character special (V7)
407 3000 S_IFMPC 030000 multiplexed character special (V7)
408 4000 S_IFDIR d/ 040000 directory (V7)
409 5000 S_IFNAM 050000 XENIX named special file
410 with two subtypes, distinguished by
411 \fIst_rdev\fP values 1, 2
412 0001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
413 0002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
414 6000 S_IFBLK b 060000 block special (V7)
415 7000 S_IFMPB 070000 multiplexed block special (V7)
416 8000 S_IFREG - 100000 regular (V7)
417 9000 S_IFCMP 110000 VxFS compressed
418 9000 S_IFNWK n 110000 network special (HP-UX)
419 a000 S_IFLNK l@ 120000 symbolic link (BSD)
420 b000 S_IFSHAD 130000 Solaris shadow inode for ACL
421 (not seen by userspace)
422 c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
423 d000 S_IFDOOR D> 150000 Solaris door
424 e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
425 0200 S_ISVTX 001000 sticky bit: save swapped text even
428 On nondirectories: don't cache this
430 On directories: restricted deletion
432 0400 S_ISGID 002000 set-group-ID on execution (V7)
433 for directories: use BSD semantics for
435 0400 S_ENFMT 002000 System V file locking enforcement (shared
437 0800 S_ISUID 004000 set-user-ID on execution (V7)
438 0800 S_CDF 004000 directory is a context dependent
442 A sticky command appeared in Version 32V AT&T UNIX.
444 Since kernel 2.5.48, the
446 structure supports nanosecond resolution for the three
447 file timestamp fields.
448 Glibc exposes the nanosecond component of each field using names either
450 .IR st_atim.tv_nsec ,
455 feature test macro is defined,
458 if neither of these macros is defined.
459 On file systems that do not support subsecond timestamps,
460 these nanosecond fields are returned with the value 0.
461 .\" As at kernel 2.6.25, XFS and JFS support nanosecond timestamps,
462 .\" but ext2, ext3, and Reiserfs do not.
463 .\" FIXME . SUSv4 specifies nanosecond timestamps.
467 will generally not trigger automounter action, whereas
471 For most files under the
475 does not return the file size in the
477 field; instead the field is returned with the value 0.
478 .SS Underlying kernel interface
479 Over time, increases in the size of the
481 structure have led to three successive versions of
491 (new in kernel 2.4; slot
495 wrapper function hides these details from applications,
496 invoking the most recent version of the system call provided by the kernel,
497 and repacking the returned information if required for old binaries.
498 Similar remarks apply for
503 .\" A note from Andries Brouwer, July 2007
505 .\" > Is the story not rather more complicated for some calls like
508 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
510 .\" The idea is here not so much that syscalls change, but that
511 .\" the definitions of struct stat and of the types dev_t and mode_t change.
512 .\" This means that libc (even if it does not call the kernel
513 .\" but only calls some internal function) must know what the
514 .\" format of dev_t or of struct stat is.
515 .\" The communication between the application and libc goes via
516 .\" the include file <sys/stat.h> that defines a _STAT_VER and
517 .\" _MKNOD_VER describing the layout of the data that user space
518 .\" uses. Each (almost each) occurrence of stat() is replaced by
519 .\" an occurrence of xstat() where the first parameter of xstat()
520 .\" is this version number _STAT_VER.
522 .\" Now, also the definitions used by the kernel change.
523 .\" But glibc copes with this in the standard way, and the
524 .\" struct stat as returned by the kernel is repacked into
525 .\" the struct stat as expected by the application.
526 .\" Thus, _STAT_VER and this setup cater for the application-libc
527 .\" interface, rather than the libc-kernel interface.
529 .\" (Note that the details depend on gcc being used as c compiler.)
531 The following program calls
533 and displays selected fields in the returned
538 #include <sys/types.h>
539 #include <sys/stat.h>
545 main(int argc, char *argv[])
550 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
554 if (stat(argv[1], &sb) == \-1) {
559 printf("File type: ");
561 switch (sb.st_mode & S_IFMT) {
562 case S_IFBLK: printf("block device\\n"); break;
563 case S_IFCHR: printf("character device\\n"); break;
564 case S_IFDIR: printf("directory\\n"); break;
565 case S_IFIFO: printf("FIFO/pipe\\n"); break;
566 case S_IFLNK: printf("symlink\\n"); break;
567 case S_IFREG: printf("regular file\\n"); break;
568 case S_IFSOCK: printf("socket\\n"); break;
569 default: printf("unknown?\\n"); break;
572 printf("I\-node number: %ld\\n", (long) sb.st_ino);
574 printf("Mode: %lo (octal)\\n",
575 (unsigned long) sb.st_mode);
577 printf("Link count: %ld\\n", (long) sb.st_nlink);
578 printf("Ownership: UID=%ld GID=%ld\\n",
579 (long) sb.st_uid, (long) sb.st_gid);
581 printf("Preferred I/O block size: %ld bytes\\n",
582 (long) sb.st_blksize);
583 printf("File size: %lld bytes\\n",
584 (long long) sb.st_size);
585 printf("Blocks allocated: %lld\\n",
586 (long long) sb.st_blocks);
588 printf("Last status change: %s", ctime(&sb.st_ctime));
589 printf("Last file access: %s", ctime(&sb.st_atime));
590 printf("Last file modification: %s", ctime(&sb.st_mtime));
602 .BR capabilities (7),