2 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
3 .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
4 .\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" 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 2019-03-06 "Linux" "Linux Programmer's Manual"
42 stat, fstat, lstat, fstatat \- get file status
45 .B #include <sys/types.h>
46 .B #include <sys/stat.h>
47 .B #include <unistd.h>
49 .BI "int stat(const char *" pathname ", struct stat *" statbuf );
50 .BI "int fstat(int " fd ", struct stat *" statbuf );
51 .BI "int lstat(const char *" pathname ", struct stat *" statbuf );
53 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
54 .B #include <sys/stat.h>
56 .BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \
62 Feature Test Macro Requirements for glibc (see
63 .BR feature_test_macros (7)):
69 /* glibc 2.19 and earlier */ _BSD_SOURCE
71 || /* Since glibc 2.20 */ _DEFAULT_SOURCE
73 || _XOPEN_SOURCE\ >=\ 500
74 .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
76 || /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L
85 _POSIX_C_SOURCE\ >=\ 200809L
94 These functions return information about a file, in the buffer pointed to by
96 No permissions are required on the file itself, but\(emin the case of
100 .BR lstat ()\(emexecute
101 (search) permission is required on all of the directories in
103 that lead to the file.
108 retrieve information about the file pointed to by
119 is a symbolic link, then it returns information about the link itself,
120 not the file that it refers to.
125 except that the file about which information is to be retrieved
126 is specified by the file descriptor
129 .SS The stat structure
130 All of these system calls return a
132 structure, which contains the following fields:
137 dev_t st_dev; /* ID of device containing file */
138 ino_t st_ino; /* Inode number */
139 mode_t st_mode; /* File type and mode */
140 nlink_t st_nlink; /* Number of hard links */
141 uid_t st_uid; /* User ID of owner */
142 gid_t st_gid; /* Group ID of owner */
143 dev_t st_rdev; /* Device ID (if special file) */
144 off_t st_size; /* Total size, in bytes */
145 blksize_t st_blksize; /* Block size for filesystem I/O */
146 blkcnt_t st_blocks; /* Number of 512B blocks allocated */
148 /* Since Linux 2.6, the kernel supports nanosecond
149 precision for the following timestamp fields.
150 For the details before Linux 2.6, see NOTES. */
152 struct timespec st_atim; /* Time of last access */
153 struct timespec st_mtim; /* Time of last modification */
154 struct timespec st_ctim; /* Time of last status change */
156 #define st_atime st_atim.tv_sec /* Backward compatibility */
157 #define st_mtime st_mtim.tv_sec
158 #define st_ctime st_ctim.tv_sec
164 the order of fields in the
166 structure varies somewhat
167 across architectures.
169 the definition above does not show the padding bytes
170 that may be present between some fields on various architectures.
171 Consult the glibc and kernel source code
172 if you need to know the details.
174 .\" Background: inode attributes are modified with i_mutex held, but
175 .\" read by stat() without taking the mutex.
177 for performance and simplicity reasons, different fields in the
179 structure may contain state information from different moments
180 during the execution of the system call.
185 is changed by another process by calling
192 together with the new
196 together with the new
201 structure are as follows:
204 This field describes the device on which this file resides.
209 macros may be useful to decompose the device ID in this field.)
212 This field contains the file's inode number.
215 This field contains the file type and mode.
218 for further information.
221 This field contains the number of hard links to the file.
224 This field contains the user ID of the owner of the file.
227 This field contains the ID of the group owner of the file.
230 This field describes the device that this file (inode) represents.
233 This field gives the size of the file (if it is a regular
234 file or a symbolic link) in bytes.
235 The size of a symbolic link is the length of the pathname
236 it contains, without a terminating null byte.
239 This field gives the "preferred" block size for efficient filesystem I/O.
242 This field indicates the number of blocks allocated to the file,
244 (This may be smaller than
246 when the file has holes.)
249 This is the file's last access timestamp.
252 This is the file's last modification timestamp.
255 This is the file's last status change timestamp.
257 For further information on the above fields, see
263 system call is a more general interface for accessing file information
264 which can still provide exactly the behavior of each of
270 If the pathname given in
272 is relative, then it is interpreted relative to the directory
273 referred to by the file descriptor
275 (rather than relative to the current working directory of
276 the calling process, as is done by
280 for a relative pathname).
290 is interpreted relative to the current working
291 directory of the calling process (like
303 can either be 0, or include one or more of the following flags ORed:
305 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
306 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
309 is an empty string, operate on the file referred to by
311 (which may have been obtained using the
317 can refer to any type of file, not just a directory, and
320 is similar to that of
326 the call operates on the current working directory.
327 This flag is Linux-specific; define
329 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
330 to obtain its definition.
332 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
333 Don't automount the terminal ("basename") component of
335 if it is a directory that is an automount point.
336 This allows the caller to gather attributes of an automount point
337 (rather than the location it would mount).
339 .\" commit 42f46148217865a545e129612075f3d828a2c4e4
340 also don't instantiate a nonexistent name in an
341 on-demand directory such as used for automounter indirect maps.
342 This flag can be used in tools that scan directories
343 to prevent mass-automounting of a directory of automount points.
346 flag has no effect if the mount point has already been mounted over.
347 This flag is Linux-specific; define
349 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
350 to obtain its definition.
359 .B AT_SYMLINK_NOFOLLOW
362 is a symbolic link, do not dereference it:
363 instead return information about the link itself, like
367 dereferences symbolic links, like
372 for an explanation of the need for
375 On success, zero is returned.
376 On error, \-1 is returned, and
378 is set appropriately.
382 Search permission is denied for one of the directories
383 in the path prefix of
386 .BR path_resolution (7).)
390 is not a valid open file descriptor.
396 Too many symbolic links encountered while traversing the path.
405 does not exist or is a dangling symbolic link.
409 is an empty string and
415 Out of memory (i.e., kernel memory).
418 A component of the path prefix of
426 refers to a file whose size, inode number,
427 or number of blocks cannot be represented in, respectively, the types
432 This error can occur when, for example,
433 an application compiled on a 32-bit platform without
434 .I -D_FILE_OFFSET_BITS=64
437 on a file whose size exceeds
441 The following additional errors can occur for
446 is not a valid file descriptor.
449 Invalid flag specified in
456 is a file descriptor referring to a file other than a directory.
459 was added to Linux in kernel 2.6.16;
460 library support was added to glibc in version 2.4.
465 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
466 .\" SVr4 documents additional
468 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
469 .\" documents additional
473 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
478 According to POSIX.1-2001,
480 on a symbolic link need return valid information only in the
482 field and the file type of the
487 POSIX.1-2008 tightens the specification, requiring
489 to return valid information in all fields except the mode bits in
496 fields may be less portable.
497 (They were introduced in BSD.
498 The interpretation differs between systems,
499 and possibly on a single system when NFS mounts are involved.)
502 Older kernels and older standards did not support nanosecond timestamp
504 Instead, there were three timestamp
505 .RI fields\(em st_atime ,
508 .IR st_ctime \(emtyped
511 that recorded timestamps with one-second precision.
513 Since kernel 2.5.48, the
515 structure supports nanosecond resolution for the three file timestamp fields.
516 The nanosecond components of each timestamp are available
517 via names of the form
518 .IR st_atim.tv_nsec ,
519 if suitable feature test macros are defined.
520 Nanosecond timestamps were standardized in POSIX.1-2008,
521 and, starting with version 2.12,
522 glibc exposes the nanosecond component names if
524 is defined with the value 200809L or greater, or
526 is defined with the value 700 or greater.
527 Up to and including glibc 2.19,
528 the definitions of the nanoseconds components are also defined if
533 If none of the aforementioned macros are defined,
534 then the nanosecond values are exposed with names of the form
537 .SS C library/kernel differences
538 Over time, increases in the size of the
540 structure have led to three successive versions of
552 on 32-bit platforms such as i386.
553 The first two versions were already present in Linux 1.0
554 (albeit with different names);
555 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
556 .\" various versions of the structure definitions
557 the last was added in Linux 2.4.
558 Similar remarks apply for
563 The kernel-internal versions of the
565 structure dealt with by the different versions are, respectively:
567 .IR __old_kernel_stat
568 The original structure, with rather narrow fields, and no padding.
573 field and padding added to various parts of the structure to
574 allow for future expansion.
584 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
585 and various other enlarged fields and further padding in the structure.
586 (Various padding bytes were eventually consumed in Linux 2.6,
587 with the advent of 32-bit device IDs and nanosecond components
588 for the timestamp fields.)
592 wrapper function hides these details from applications,
593 invoking the most recent version of the system call provided by the kernel,
594 and repacking the returned information if required for old binaries.
596 .\" A note from Andries Brouwer, July 2007
598 .\" > Is the story not rather more complicated for some calls like
601 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
603 .\" The idea is here not so much that syscalls change, but that
604 .\" the definitions of struct stat and of the types dev_t and mode_t change.
605 .\" This means that libc (even if it does not call the kernel
606 .\" but only calls some internal function) must know what the
607 .\" format of dev_t or of struct stat is.
608 .\" The communication between the application and libc goes via
609 .\" the include file <sys/stat.h> that defines a _STAT_VER and
610 .\" _MKNOD_VER describing the layout of the data that user space
611 .\" uses. Each (almost each) occurrence of stat() is replaced by
612 .\" an occurrence of xstat() where the first parameter of xstat()
613 .\" is this version number _STAT_VER.
615 .\" Now, also the definitions used by the kernel change.
616 .\" But glibc copes with this in the standard way, and the
617 .\" struct stat as returned by the kernel is repacked into
618 .\" the struct stat as expected by the application.
619 .\" Thus, _STAT_VER and this setup cater for the application-libc
620 .\" interface, rather than the libc-kernel interface.
622 .\" (Note that the details depend on gcc being used as c compiler.)
624 On modern 64-bit systems, life is simpler: there is a single
626 system call and the kernel deals with a
628 structure that contains fields of a sufficient size.
630 The underlying system call employed by the glibc
632 wrapper function is actually called
634 or, on some architectures,
635 .\" strace(1) shows the name "newfstatat" on x86-64
638 The following program calls
640 and displays selected fields in the returned
645 #include <sys/types.h>
646 #include <sys/stat.h>
650 #include <sys/sysmacros.h>
653 main(int argc, char *argv[])
658 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
662 if (lstat(argv[1], &sb) == \-1) {
667 printf("ID of containing device: [%lx,%lx]\en",
668 (long) major(sb.st_dev), (long) minor(sb.st_dev));
670 printf("File type: ");
672 switch (sb.st_mode & S_IFMT) {
673 case S_IFBLK: printf("block device\en"); break;
674 case S_IFCHR: printf("character device\en"); break;
675 case S_IFDIR: printf("directory\en"); break;
676 case S_IFIFO: printf("FIFO/pipe\en"); break;
677 case S_IFLNK: printf("symlink\en"); break;
678 case S_IFREG: printf("regular file\en"); break;
679 case S_IFSOCK: printf("socket\en"); break;
680 default: printf("unknown?\en"); break;
683 printf("I\-node number: %ld\en", (long) sb.st_ino);
685 printf("Mode: %lo (octal)\en",
686 (unsigned long) sb.st_mode);
688 printf("Link count: %ld\en", (long) sb.st_nlink);
689 printf("Ownership: UID=%ld GID=%ld\en",
690 (long) sb.st_uid, (long) sb.st_gid);
692 printf("Preferred I/O block size: %ld bytes\en",
693 (long) sb.st_blksize);
694 printf("File size: %lld bytes\en",
695 (long long) sb.st_size);
696 printf("Blocks allocated: %lld\en",
697 (long long) sb.st_blocks);
699 printf("Last status change: %s", ctime(&sb.st_ctime));
700 printf("Last file access: %s", ctime(&sb.st_atime));
701 printf("Last file modification: %s", ctime(&sb.st_mtime));
715 .BR capabilities (7),