1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
3 .\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified by Michael Haardt <michael@moria.de>
8 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
10 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
11 .\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
12 .\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
13 .\" Modified 1999-07-06 by aeb & Albert Cahalan
14 .\" Modified 2000-01-07 by aeb
15 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
16 .\" 2007-06-08 mtk: Added example program
17 .\" 2007-07-05 mtk: Added details on underlying system call interfaces
19 .TH STAT 2 2021-08-27 "Linux" "Linux Programmer's Manual"
21 stat, fstat, lstat, fstatat \- get file status
24 .RI ( libc ", " \-lc )
27 .B #include <sys/stat.h>
29 .BI "int stat(const char *restrict " pathname ,
30 .BI " struct stat *restrict " statbuf );
31 .BI "int fstat(int " fd ", struct stat *" statbuf );
32 .BI "int lstat(const char *restrict " pathname ,
33 .BI " struct stat *restrict " statbuf );
35 .BR "#include <fcntl.h> " "/* Definition of " AT_* " constants */"
36 .B #include <sys/stat.h>
38 .BI "int fstatat(int " dirfd ", const char *restrict " pathname ,
39 .BI " struct stat *restrict " statbuf ", int " flags );
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
49 /* Since glibc 2.20 */ _DEFAULT_SOURCE
50 || _XOPEN_SOURCE >= 500
51 .\" _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
52 || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
53 || /* Glibc 2.19 and earlier */ _BSD_SOURCE
59 _POSIX_C_SOURCE >= 200809L
64 These functions return information about a file, in the buffer pointed to by
66 No permissions are required on the file itself, but\(emin the case of
70 .BR lstat ()\(emexecute
71 (search) permission is required on all of the directories in
73 that lead to the file.
78 retrieve information about the file pointed to by
89 is a symbolic link, then it returns information about the link itself,
90 not the file that the link refers to.
95 except that the file about which information is to be retrieved
96 is specified by the file descriptor
99 .SS The stat structure
100 All of these system calls return a
105 .\" Background: inode attributes are modified with i_mutex held, but
106 .\" read by stat() without taking the mutex.
108 for performance and simplicity reasons, different fields in the
110 structure may contain state information from different moments
111 during the execution of the system call.
116 is changed by another process by calling
123 together with the new
127 together with the new
132 system call is a more general interface for accessing file information
133 which can still provide exactly the behavior of each of
139 If the pathname given in
141 is relative, then it is interpreted relative to the directory
142 referred to by the file descriptor
144 (rather than relative to the current working directory of
145 the calling process, as is done by
149 for a relative pathname).
159 is interpreted relative to the current working
160 directory of the calling process (like
172 can either be 0, or include one or more of the following flags ORed:
174 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
175 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
178 is an empty string, operate on the file referred to by
180 (which may have been obtained using the
186 can refer to any type of file, not just a directory, and
189 is similar to that of
195 the call operates on the current working directory.
196 This flag is Linux-specific; define
198 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
199 to obtain its definition.
201 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
202 Don't automount the terminal ("basename") component of
204 Since Linux 3.1 this flag is ignored.
205 Since Linux 4.11 this flag is implied.
207 .B AT_SYMLINK_NOFOLLOW
210 is a symbolic link, do not dereference it:
211 instead return information about the link itself, like
215 dereferences symbolic links, like
220 for an explanation of the need for
223 On success, zero is returned.
224 On error, \-1 is returned, and
226 is set to indicate the error.
230 Search permission is denied for one of the directories
231 in the path prefix of
234 .BR path_resolution (7).)
238 is not a valid open file descriptor.
247 nor a valid file descriptor.
254 Invalid flag specified in
258 Too many symbolic links encountered while traversing the path.
267 does not exist or is a dangling symbolic link.
271 is an empty string and
277 Out of memory (i.e., kernel memory).
280 A component of the path prefix of
289 is a file descriptor referring to a file other than a directory.
295 refers to a file whose size, inode number,
296 or number of blocks cannot be represented in, respectively, the types
301 This error can occur when, for example,
302 an application compiled on a 32-bit platform without
303 .I \-D_FILE_OFFSET_BITS=64
306 on a file whose size exceeds
311 was added to Linux in kernel 2.6.16;
312 library support was added to glibc in version 2.4.
317 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
318 .\" SVr4 documents additional
320 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
321 .\" documents additional
325 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
330 According to POSIX.1-2001,
332 on a symbolic link need return valid information only in the
334 field and the file type of the
339 POSIX.1-2008 tightens the specification, requiring
341 to return valid information in all fields except the mode bits in
348 fields may be less portable.
349 (They were introduced in BSD.
350 The interpretation differs between systems,
351 and possibly on a single system when NFS mounts are involved.)
353 .SS C library/kernel differences
354 Over time, increases in the size of the
356 structure have led to three successive versions of
368 on 32-bit platforms such as i386.
369 The first two versions were already present in Linux 1.0
370 (albeit with different names);
371 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
372 .\" various versions of the structure definitions
373 the last was added in Linux 2.4.
374 Similar remarks apply for
379 The kernel-internal versions of the
381 structure dealt with by the different versions are, respectively:
384 The original structure, with rather narrow fields, and no padding.
389 field and padding added to various parts of the structure to
390 allow for future expansion.
400 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
401 and various other enlarged fields and further padding in the structure.
402 (Various padding bytes were eventually consumed in Linux 2.6,
403 with the advent of 32-bit device IDs and nanosecond components
404 for the timestamp fields.)
408 wrapper function hides these details from applications,
409 invoking the most recent version of the system call provided by the kernel,
410 and repacking the returned information if required for old binaries.
412 .\" A note from Andries Brouwer, July 2007
414 .\" > Is the story not rather more complicated for some calls like
417 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
419 .\" The idea is here not so much that syscalls change, but that
420 .\" the definitions of struct stat and of the types dev_t and mode_t change.
421 .\" This means that libc (even if it does not call the kernel
422 .\" but only calls some internal function) must know what the
423 .\" format of dev_t or of struct stat is.
424 .\" The communication between the application and libc goes via
425 .\" the include file <sys/stat.h> that defines a _STAT_VER and
426 .\" _MKNOD_VER describing the layout of the data that user space
427 .\" uses. Each (almost each) occurrence of stat() is replaced by
428 .\" an occurrence of xstat() where the first parameter of xstat()
429 .\" is this version number _STAT_VER.
431 .\" Now, also the definitions used by the kernel change.
432 .\" But glibc copes with this in the standard way, and the
433 .\" struct stat as returned by the kernel is repacked into
434 .\" the struct stat as expected by the application.
435 .\" Thus, _STAT_VER and this setup cater for the application-libc
436 .\" interface, rather than the libc-kernel interface.
438 .\" (Note that the details depend on gcc being used as c compiler.)
440 On modern 64-bit systems, life is simpler: there is a single
442 system call and the kernel deals with a
444 structure that contains fields of a sufficient size.
446 The underlying system call employed by the glibc
448 wrapper function is actually called
450 or, on some architectures,
451 .\" strace(1) shows the name "newfstatat" on x86-64
454 The following program calls
456 and displays selected fields in the returned
460 .\" SRC BEGIN (stat.c)
465 #include <sys/stat.h>
466 #include <sys/sysmacros.h>
470 main(int argc, char *argv[])
475 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
479 if (lstat(argv[1], &sb) == \-1) {
484 printf("ID of containing device: [%jx,%jx]\en",
485 (uintmax_t) major(sb.st_dev),
486 (uintmax_t) minor(sb.st_dev));
488 printf("File type: ");
490 switch (sb.st_mode & S_IFMT) {
491 case S_IFBLK: printf("block device\en"); break;
492 case S_IFCHR: printf("character device\en"); break;
493 case S_IFDIR: printf("directory\en"); break;
494 case S_IFIFO: printf("FIFO/pipe\en"); break;
495 case S_IFLNK: printf("symlink\en"); break;
496 case S_IFREG: printf("regular file\en"); break;
497 case S_IFSOCK: printf("socket\en"); break;
498 default: printf("unknown?\en"); break;
501 printf("I\-node number: %ju\en", (uintmax_t) sb.st_ino);
503 printf("Mode: %jo (octal)\en",
504 (uintmax_t) sb.st_mode);
506 printf("Link count: %ju\en", (uintmax_t) sb.st_nlink);
507 printf("Ownership: UID=%ju GID=%ju\en",
508 (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
510 printf("Preferred I/O block size: %jd bytes\en",
511 (intmax_t) sb.st_blksize);
512 printf("File size: %jd bytes\en",
513 (intmax_t) sb.st_size);
514 printf("Blocks allocated: %jd\en",
515 (intmax_t) sb.st_blocks);
517 printf("Last status change: %s", ctime(&sb.st_ctime));
518 printf("Last file access: %s", ctime(&sb.st_atime));
519 printf("Last file modification: %s", ctime(&sb.st_mtime));
535 .BR capabilities (7),