stat(2)
๐ stat(2) โ get file status ๐
okay bestie, sit DOWN. this is the one. the queens of "hey what IS this file actually" ๐ this page lives in the System Calls Manual, section 2, so you know it's the real kernel-adjacent deal.
โจ NAME โจ
stat, fstat, lstat, fstatat โ get file status ๐ฆ
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ SYNOPSIS ๐
#include <sys/stat.h>
int stat(const char *restrict path,
struct stat *restrict statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *restrict path,
struct stat *restrict statbuf);
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int fstatat(int dirfd, const char *restrict path,
struct stat *restrict statbuf, int flags);
๐ธ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐ธ
lstat():
/* Since glibc 2.20 */ _DEFAULT_SOURCE
|| _XOPEN_SOURCE >= 500
|| /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
|| /* glibc 2.19 and earlier */ _BSD_SOURCE
fstatat():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
no cap, if your build is yelling at you about feature test macros, it's THIS. it's always this. ๐ญ
๐ซ DESCRIPTION ๐ซ
so these functions go and get info about a file and dump it into the buffer that statbuf is pointing at. โจ and here's the gag: you don't need any permissions on the file itself. nope. BUT โ for stat(), fstatat(), and lstat() โ you DO need execute (search) permission on every single directory in path that leads to the file. the whole paper trail, bestie. ๐
stat() and fstatat() retrieve information about the file pointed to by path. the fstatat() differences are explained lower down, don't worry ๐ซถ
lstat() is literally identical to stat(), EXCEPT: if path is a symbolic link, it gives you the info about the link itself, not the file the link points to. it's giving "no, I'm talking about the middleman". ๐
fstat() is also identical to stat(), except the file is specified by the file descriptor fd instead of a path. iconic, no path needed. ๐
๐ง The stat structure ๐ง
all of these system calls return a stat structure (peep stat(3type) for the deets).
Note: for performance and simplicity reasons, different fields in thestatstructure may contain state information from different moments during the execution of the system call. for example, ifst_modeorst_uidis changed by another process by callingchmod(2)orchown(2),stat()might return the oldst_modetogether with the newst_uid, or the oldst_uidtogether with the newst_mode. yes. it's a lil chaotic in there. it's not a bug, it's a lifestyle. ๐
๐ฆ fstatat() ๐ฆ
fstatat() is the more general interface for accessing file information, and it can still provide exactly the behavior of each of stat(), lstat(), and fstat(). one function, three moods. ๐
If path is relative, then it's interpreted relative to the directory referred to by the file descriptor dirfd โ rather than relative to the current working directory of the calling process, which is what stat() and lstat() do for a relative pathname. ๐
If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like stat() and lstat()). bestie behavior. ๐ซถ
If path is absolute, then dirfd is ignored. periodt.
flags can either be 0, or include one or more of the following flags ORed together:
AT_EMPTY_PATH(since Linux 2.6.39) โ Ifpathis an empty string (orNULL, since Linux 6.11) operate on the file referred to bydirfd(which may have been obtained using theopen(2)O_PATHflag). In this case,dirfdcan refer to any type of file, not just a directory, and the behavior offstatat()is similar to that offstat(). IfdirfdisAT_FDCWD, the call operates on the current working directory. This flag is Linux-specific; define_GNU_SOURCEto obtain its definition. ๐กAT_NO_AUTOMOUNT(since Linux 2.6.38) โ Don't automount the terminal ("basename") component ofpath. Since Linux 3.1 this flag is ignored. Since Linux 4.11 this flag is implied. ๐๐จ (it retired, basically)AT_SYMLINK_NOFOLLOWโ Ifpathis a symbolic link, do not dereference it: instead return information about the link itself, likelstat(). (By default,fstatat()dereferences symbolic links, likestat().) ๐
See openat(2) for an explanation of the need for fstatat(). ๐
๐ RETURN VALUE ๐
- On success, zero is returned. slay.
- On error,
-1is returned, anderrnois set to indicate the error. ๐ญ
๐ ERRORS ๐
EACCESโ Search permission is denied for one of the directories in the path prefix ofpath. (See alsopath_resolution(7).)EBADFโfdis not a valid open file descriptor.EBADFโ (fstatat())pathis relative butdirfdis neitherAT_FDCWDnor a valid file descriptor.EFAULTโ Bad address.EINVALโ (fstatat()) Invalid flag specified inflags.ELOOPโ Too many symbolic links encountered while traversing the path. ๐ENAMETOOLONGโpathis too long.ENOENTโ A component ofpathdoes not exist or is a dangling symbolic link.ENOENTโpathis an empty string andAT_EMPTY_PATHwas not specified inflags.ENOMEMโ Out of memory (i.e., kernel memory).ENOTDIRโ A component of the path prefix ofpathis not a directory.ENOTDIRโ (fstatat())pathis relative anddirfdis a file descriptor referring to a file other than a directory.EOVERFLOWโpathorfdrefers to a file whose size, inode number, or number of blocks cannot be represented in, respectively, the typesoff_t,ino_t, orblkcnt_t. This error can occur when, for example, an application compiled on a 32-bit platform without-D_FILE_OFFSET_BITS=64callsstat()on a file whose size exceeds(1<<31)-1bytes. ๐ฅ
๐ STANDARDS ๐
POSIX.1-2024. we love a current standard. โจ
๐ HISTORY ๐
stat(),fstat(),lstat()โ SVr4, 4.3BSD, POSIX.1-2001.fstatat()โ POSIX.1-2008. Linux 2.6.16, glibc 2.4.
According to POSIX.1-2001, lstat() on a symbolic link need return valid information only in the st_size field and the file type of the st_mode field of the stat structure. POSIX.1-2008 tightens the specification, requiring lstat() to return valid information in all fields except the mode bits in st_mode. glow up. ๐
Use of the st_blocks and st_blksize fields may be less portable. (They were introduced in BSD. The interpretation differs between systems, and possibly on a single system when NFS mounts are involved.) ๐ซ
๐งฌ C library/kernel differences ๐งฌ
Over time, increases in the size of the stat structure have led to three successive versions of stat(): sys_stat() (slot __NR_oldstat), sys_newstat() (slot __NR_stat), and sys_stat64() (slot __NR_stat64) on 32-bit platforms such as i386. The first two versions were already present in Linux 1.0 (albeit with different names); the last was added in Linux 2.4. Similar remarks apply for fstat() and lstat(). a whole trilogy, fr fr. ๐
The kernel-internal versions of the stat structure dealt with by the different versions are, respectively:
__old_kernel_statโ The original structure, with rather narrow fields, and no padding.statโ Largerst_inofield and padding added to various parts of the structure to allow for future expansion.stat64โ Even largerst_inofield, largerst_uidandst_gidfields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits, and various other enlarged fields and further padding in the structure. (Various padding bytes were eventually consumed in Linux 2.6, with the advent of 32-bit device IDs and nanosecond components for the timestamp fields.)
The glibc stat() wrapper function hides these details from applications, invoking the most recent version of the system call provided by the kernel, and repacking the returned information if required for old binaries. thank you glibc, we appreciate you ๐ซถ
On modern 64-bit systems, life is simpler: there is a single stat() system call and the kernel deals with a stat structure that contains fields of a sufficient size. no drama. โจ
The underlying system call employed by the glibc fstatat() wrapper function is actually called fstatat64() or, on some architectures, newfstatat(). plot twist! ๐ญ
๐ EXAMPLES ๐
The following program calls lstat() and displays selected fields in the returned stat structure. copy it, run it, be obsessed with it:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
int
main(int argc, char *argv[])
{
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (lstat(argv[1], &sb) == -1) {
perror("lstat");
exit(EXIT_FAILURE);
}
printf("ID of containing device: [%x,%x]\n",
major(sb.st_dev),
minor(sb.st_dev));
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ju\n", (uintmax_t) sb.st_ino);
printf("Mode: %jo (octal)\n",
(uintmax_t) sb.st_mode);
printf("Link count: %ju\n", (uintmax_t) sb.st_nlink);
printf("Ownership: UID=%ju GID=%ju\n",
(uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
printf("Preferred I/O block size: %jd bytes\n",
(intmax_t) sb.st_blksize);
printf("File size: %jd bytes\n",
(intmax_t) sb.st_size);
printf("Blocks allocated: %jd\n",
(intmax_t) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
ls(1), stat(1), access(2), chmod(2), chown(2), readlink(2), statx(2), utime(2), stat(3type), capabilities(7), inode(7), symlink(7)
and that's the whole page bestie ๐ tysm for reading, this one came from Linux man-pages 6.20, dated 2026-02-08 โ go forth and lstat() something, you've got this ๐โจ๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1995, Nicolai Langfeldt <janl@ifi.uio.no>
- Copyright 2006-2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms