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 2020-04-11 "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 the link 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 time of the last access of file data.
252 This is the time of last modification of file data.
255 This is the file's last status change timestamp
256 (time of last change to the inode).
258 For further information on the above fields, see
264 system call is a more general interface for accessing file information
265 which can still provide exactly the behavior of each of
271 If the pathname given in
273 is relative, then it is interpreted relative to the directory
274 referred to by the file descriptor
276 (rather than relative to the current working directory of
277 the calling process, as is done by
281 for a relative pathname).
291 is interpreted relative to the current working
292 directory of the calling process (like
304 can either be 0, or include one or more of the following flags ORed:
306 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
307 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
310 is an empty string, operate on the file referred to by
312 (which may have been obtained using the
318 can refer to any type of file, not just a directory, and
321 is similar to that of
327 the call operates on the current working directory.
328 This flag is Linux-specific; define
330 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
331 to obtain its definition.
333 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
334 Don't automount the terminal ("basename") component of
336 if it is a directory that is an automount point.
337 This allows the caller to gather attributes of an automount point
338 (rather than the location it would mount).
340 .\" commit 42f46148217865a545e129612075f3d828a2c4e4
341 also don't instantiate a nonexistent name in an
342 on-demand directory such as used for automounter indirect maps.
344 flag has no effect if the mount point has already been mounted over.
356 can be used in tools that scan directories
357 to prevent mass-automounting of a directory of automount points.
360 This flag is Linux-specific; define
362 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
363 to obtain its definition.
365 .B AT_SYMLINK_NOFOLLOW
368 is a symbolic link, do not dereference it:
369 instead return information about the link itself, like
373 dereferences symbolic links, like
378 for an explanation of the need for
381 On success, zero is returned.
382 On error, \-1 is returned, and
384 is set appropriately.
388 Search permission is denied for one of the directories
389 in the path prefix of
392 .BR path_resolution (7).)
396 is not a valid open file descriptor.
402 Too many symbolic links encountered while traversing the path.
411 does not exist or is a dangling symbolic link.
415 is an empty string and
421 Out of memory (i.e., kernel memory).
424 A component of the path prefix of
432 refers to a file whose size, inode number,
433 or number of blocks cannot be represented in, respectively, the types
438 This error can occur when, for example,
439 an application compiled on a 32-bit platform without
440 .I -D_FILE_OFFSET_BITS=64
443 on a file whose size exceeds
447 The following additional errors can occur for
452 is not a valid file descriptor.
455 Invalid flag specified in
462 is a file descriptor referring to a file other than a directory.
465 was added to Linux in kernel 2.6.16;
466 library support was added to glibc in version 2.4.
471 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
472 .\" SVr4 documents additional
474 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
475 .\" documents additional
479 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
484 According to POSIX.1-2001,
486 on a symbolic link need return valid information only in the
488 field and the file type of the
493 POSIX.1-2008 tightens the specification, requiring
495 to return valid information in all fields except the mode bits in
502 fields may be less portable.
503 (They were introduced in BSD.
504 The interpretation differs between systems,
505 and possibly on a single system when NFS mounts are involved.)
508 Older kernels and older standards did not support nanosecond timestamp
510 Instead, there were three timestamp
511 .RI fields\(em st_atime ,
514 .IR st_ctime \(emtyped
517 that recorded timestamps with one-second precision.
519 Since kernel 2.5.48, the
521 structure supports nanosecond resolution for the three file timestamp fields.
522 The nanosecond components of each timestamp are available
523 via names of the form
524 .IR st_atim.tv_nsec ,
525 if suitable feature test macros are defined.
526 Nanosecond timestamps were standardized in POSIX.1-2008,
527 and, starting with version 2.12,
528 glibc exposes the nanosecond component names if
530 is defined with the value 200809L or greater, or
532 is defined with the value 700 or greater.
533 Up to and including glibc 2.19,
534 the definitions of the nanoseconds components are also defined if
539 If none of the aforementioned macros are defined,
540 then the nanosecond values are exposed with names of the form
543 .SS C library/kernel differences
544 Over time, increases in the size of the
546 structure have led to three successive versions of
558 on 32-bit platforms such as i386.
559 The first two versions were already present in Linux 1.0
560 (albeit with different names);
561 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
562 .\" various versions of the structure definitions
563 the last was added in Linux 2.4.
564 Similar remarks apply for
569 The kernel-internal versions of the
571 structure dealt with by the different versions are, respectively:
573 .IR __old_kernel_stat
574 The original structure, with rather narrow fields, and no padding.
579 field and padding added to various parts of the structure to
580 allow for future expansion.
590 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
591 and various other enlarged fields and further padding in the structure.
592 (Various padding bytes were eventually consumed in Linux 2.6,
593 with the advent of 32-bit device IDs and nanosecond components
594 for the timestamp fields.)
598 wrapper function hides these details from applications,
599 invoking the most recent version of the system call provided by the kernel,
600 and repacking the returned information if required for old binaries.
602 .\" A note from Andries Brouwer, July 2007
604 .\" > Is the story not rather more complicated for some calls like
607 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
609 .\" The idea is here not so much that syscalls change, but that
610 .\" the definitions of struct stat and of the types dev_t and mode_t change.
611 .\" This means that libc (even if it does not call the kernel
612 .\" but only calls some internal function) must know what the
613 .\" format of dev_t or of struct stat is.
614 .\" The communication between the application and libc goes via
615 .\" the include file <sys/stat.h> that defines a _STAT_VER and
616 .\" _MKNOD_VER describing the layout of the data that user space
617 .\" uses. Each (almost each) occurrence of stat() is replaced by
618 .\" an occurrence of xstat() where the first parameter of xstat()
619 .\" is this version number _STAT_VER.
621 .\" Now, also the definitions used by the kernel change.
622 .\" But glibc copes with this in the standard way, and the
623 .\" struct stat as returned by the kernel is repacked into
624 .\" the struct stat as expected by the application.
625 .\" Thus, _STAT_VER and this setup cater for the application-libc
626 .\" interface, rather than the libc-kernel interface.
628 .\" (Note that the details depend on gcc being used as c compiler.)
630 On modern 64-bit systems, life is simpler: there is a single
632 system call and the kernel deals with a
634 structure that contains fields of a sufficient size.
636 The underlying system call employed by the glibc
638 wrapper function is actually called
640 or, on some architectures,
641 .\" strace(1) shows the name "newfstatat" on x86-64
644 The following program calls
646 and displays selected fields in the returned
651 #include <sys/types.h>
652 #include <sys/stat.h>
656 #include <sys/sysmacros.h>
659 main(int argc, char *argv[])
664 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
668 if (lstat(argv[1], &sb) == \-1) {
673 printf("ID of containing device: [%lx,%lx]\en",
674 (long) major(sb.st_dev), (long) minor(sb.st_dev));
676 printf("File type: ");
678 switch (sb.st_mode & S_IFMT) {
679 case S_IFBLK: printf("block device\en"); break;
680 case S_IFCHR: printf("character device\en"); break;
681 case S_IFDIR: printf("directory\en"); break;
682 case S_IFIFO: printf("FIFO/pipe\en"); break;
683 case S_IFLNK: printf("symlink\en"); break;
684 case S_IFREG: printf("regular file\en"); break;
685 case S_IFSOCK: printf("socket\en"); break;
686 default: printf("unknown?\en"); break;
689 printf("I\-node number: %ld\en", (long) sb.st_ino);
691 printf("Mode: %lo (octal)\en",
692 (unsigned long) sb.st_mode);
694 printf("Link count: %ld\en", (long) sb.st_nlink);
695 printf("Ownership: UID=%ld GID=%ld\en",
696 (long) sb.st_uid, (long) sb.st_gid);
698 printf("Preferred I/O block size: %ld bytes\en",
699 (long) sb.st_blksize);
700 printf("File size: %lld bytes\en",
701 (long long) sb.st_size);
702 printf("Blocks allocated: %lld\en",
703 (long long) sb.st_blocks);
705 printf("Last status change: %s", ctime(&sb.st_ctime));
706 printf("Last file access: %s", ctime(&sb.st_atime));
707 printf("Last file modification: %s", ctime(&sb.st_mtime));
721 .BR capabilities (7),