stat From Zero: Read a File's Identity Card and Its Four Timestamps
ls shows you one short line per file. stat reads out the whole record: size, blocks, inode, permissions, owner, and four timestamps that each move for a different reason. This guide runs every command for real on GNU coreutils 9.4 — including the measured finding that chmod moves ctime and leaves mtime alone, and that --printf prints no trailing newline.

Here is a situation you have probably run into: you download a file, glance at ls -l, the size looks right, the name looks right — and still something nags at you. When exactly was it changed? Who owns it? Are the permissions 640 or 644? How big is that subdirectory, really? ls only shows you a small slice: name, size, permissions, modification time, owner. The file's full identity card is not in ls. It is in stat. stat is a small tool in GNU coreutils whose only job is reading a file's metadata: it never reads the file's contents, it reads the one page of records the filesystem keeps for that file — size, inode number, link count, permissions, owner, four timestamps, and the filesystem it lives on. This guide runs every command for real on coreutils 9.4: the default output explained line by line, picking your own fields, which of the four timestamps moves when, the mystery of the 9-byte symlink, what a directory's 60 bytes actually mean — plus two details the manual states only briefly and that only a real run makes obvious: chmod moves ctime and leaves mtime alone, and --printf prints no trailing newline, so its output glues itself to the next line.
First, get stat's job straight. It only ever answers one question: what does the filesystem have on record about this file? It cannot tell you whether the file's contents were changed — that is sha256sum's job — and it cannot tell you what the file says — that is cat or less. stat reads the inode. Every file on a filesystem has an inode, and the inode holds the metadata: size, type, permissions, owner, link count, timestamps. You hand stat a filename and it reads that record out loud. Because it only reads metadata, it is fast, and it never touches a single byte of the file's contents — a 10 GB file and an 11-byte file take stat exactly the same time. Once that clicks, a lot of "why" questions answer themselves: why stat can tell you instantly how big a file is but cannot tell you whether it contains a certain word, and why the "size" it reports for a directory is not the size you expected.
The basic use is a single line: stat hello.txt. Our test file hello.txt holds one line, "hello vibe", 11 bytes in total. The default output reads like this: File: hello.txt; Size: 11, Blocks: 8, IO Block: 4096, regular file; Device: 0,88, Inode: 62, Links: 1; Access: (0660/-rw-rw----), Uid: (0/root), Gid: (0/root); then four lines for Access, Modify, Change, and Birth. Take it apart line by line. File is the name you passed in. Size is the length of the file's contents in bytes: 11. Blocks is how many blocks the filesystem actually allocated for it: 8. IO Block is the filesystem's suggested transfer size: 4096. "regular file" is the file type. Device is the device number of the filesystem holding the file. Inode is this file's number in the filesystem: 62. Links is the hard link count: 1, meaning exactly one name points at this inode. The Access line shows the permissions twice, 0660 in octal and -rw-rw---- in human-readable form. Uid and Gid are the owning user and group. The last four lines are the timestamps, and they get a section of their own.
The Size and Blocks line is the one that confuses people most: how can an 11-byte file take up 8 blocks? First, the unit: Blocks are counted in 512-byte units, not bytes. If you want to confirm, %B tells you how many bytes each block is; here %B is 512, and 8 times 512 is 4096. A filesystem hands out space in whole blocks, so even the tiniest file occupies at least one full block — 11 bytes of content sitting in 4096 bytes of disk. It works the other way too: a big file full of zeros can occupy almost no blocks at all. That is a sparse file: large Size, small Blocks. Keep the two numbers apart in your head: Size is "how long is the content", Blocks is "how much disk did the filesystem actually set aside". When the disk is full and you are hunting for the cause, you look at Blocks and df. When you are checking whether a download arrived whole, you look at Size. Each number minds its own business, and mixing them up leads to wrong conclusions.
The default output tells you everything, which is exactly why it feels wordy day to day. stat gives you -c (--format) so you can order a la carte: stat -c '%n %s bytes, type %F, links %h' hello.txt prints hello.txt 11 bytes, type regular file, links 1. A percent sign followed by a letter is one field. The ones you will reach for most are: %n file name, %s size in bytes, %F file type (regular file, directory, symbolic link), %h hard link count, %i inode number, %a permissions in octal, %A permissions in human-readable form, %u owner user id, %U owner user name, %g owner group id, %G owner group name, %y modification time in human-readable form, %Y modification time in seconds since 1 January 1970, %x access time, %z status change time, %w birth time, %o optimal I/O transfer size hint, %m mount point. You can pass several files at once — stat -c '%n %s bytes' hello.txt sub/a.txt prints one line per file. You can also put plain text in the format string, a colon here, the word "bytes" there, and the output comes out laid out exactly the way you wrote it. In scripts, -c is the form you will use most, because every column in the output is one you chose yourself, so parsing it never trips over the spaces and colons of the default output.
--printf and -c are close siblings with exactly one difference: -c adds a newline at the end of each line of output, and --printf does not. Here is the real run: stat --printf='%s' hello.txt prints 11, and then the cursor stays right after the 11, so the next shell prompt lands on the same line and the terminal shows "11<-- the prompt lands right after the number". stat -c '%s' hello.txt, by contrast, prints 11 and a proper newline. The manual says it plainly: --printf interprets backslash escapes but does not output a mandatory trailing newline; if you want a newline, include \n in the format. So the correct form is stat --printf='%n: %s bytes, mode %#03a, owner %U\n' hello.txt, which prints hello.txt: 11 bytes, mode 0640, owner root. When do you want --printf? When you are assembling one custom line out of several fields, or when you want exact control over newlines and separators. When do you want -c? When you just want one or two fields quickly. Remember the difference, or the day you build output with --printf in a script, several lines of results will glue themselves into one, and you will spend a while figuring out why.
Permissions and ownership are the two groups of fields people ask stat about most. stat -c '%a octal, %A symbolic, owner %U:%G' hello.txt printed, in our run, 640 octal, -rw-r----- symbolic, owner root:root. %a is the octal permission, 640. %A is the symbolic form, -rw-r-----, identical to the first column of ls -l. If you want the octal form with a leading zero, looking like 0640, use %#03a: the # asks for the leading zero that marks the number as octal, and 03 pads it to at least three digits. Owners come in two spellings each: %U is the user name (root) and %u is the user id (0); %G and %g do the same for the group. One boundary to draw here: this guide only covers how to read permissions. Changing them belongs to another guide, "Linux file permissions from zero", which covers chmod, umask, and the nobody user — nothing about changing permissions is taught here. One reassurance is worth stating: stat is read-only. It never changes anything about a file, so running it against files in production is exactly as safe as looking at them.
The four timestamps are the part of stat most often misunderstood, so first line up the names with their meanings. Access (atime) is the last time the file was read. Modify (mtime) is the last time the content changed. Change (ctime) is the last time the metadata changed. Birth is the moment the file was created. In daily use, mtime is the one you reach for: "was this file changed recently?" is a question about mtime. atime is "when was it last read" — reading a file moves it, although many filesystems use a relatime policy for performance, so one read does not always update it; treat atime as a hint, never as evidence. ctime is "when did the metadata last change": changing permissions, owner, or the name all move it. Birth is set once, when the file is created, and never moves again. The four timestamps each mind their own business, and mixing them up produces wrong conclusions. The most common mistake is taking ctime for the creation time. It is not. Birth is.
A real run makes the point better than any definition. We first pinned hello.txt's modification time to a date nobody will confuse with today: touch -d '2020-01-02 03:04:05' hello.txt, then stat -c 'mtime=%y' printed mtime=2020-01-02 03:04:05.000000000 +0000. Then ctime: stat -c 'ctime=%z' printed today — the moment the touch command itself ran, because touch changed mtime and also pushed ctime to now. Next, change only the permissions, not the content: chmod 600 hello.txt. Check again: mtime still reads 2020-01-02 03:04:05, not moved a second; ctime has moved to now. The conclusion fits in one sentence: chmod moves ctime and leaves mtime alone. mtime only recognises changes to content; ctime recognises every change, including permissions, owner, and name — changes where the file itself is untouched but its record changed. Read it the other way round and it becomes a diagnostic tool: a file whose mtime is three years old and whose ctime is yesterday was definitely re-permissioned or re-owned yesterday, while its contents have not moved in three years. That is what stat is really for when something goes wrong: the four timestamps together are a short resume of everything that has happened to a file lately.
Birth deserves a paragraph of its own, because it is the odd one out: the only timestamp written exactly once, at the moment the file is created, and never touched by any later operation. In our run, hello.txt's Birth was the moment printf created it, identical at first to Access, Modify, and Change. But not every filesystem records a creation time. When the filesystem does not keep one, %w prints a dash -, and %W, the seconds-since-1970 form, prints 0. The manual says exactly that: %w is the time of file birth, human-readable, "-" if unknown. So if you see Birth: - on an old filesystem or on some network filesystems, stat is not broken — the filesystem simply never recorded that piece of information. In scripts, never assume Birth has a value; check for the dash first before using it.
Symbolic links are stat's other touchstone. Here is the real run: ln -s hello.txt link.txt creates a link pointing at hello.txt. stat -c '%n size=%s type=%F' link.txt prints link.txt size=9 type=symbolic link. Add -L: stat -L -c '%n size=%s type=%F' link.txt prints link.txt size=11 type=regular file. The same name, two answers, and the difference is -L (--dereference). Without -L, stat reports the link itself: 9 bytes, exactly the length of the target path text "hello.txt", and the type is symbolic link. With -L, stat follows the link and reports the target: 11 bytes, regular file. The default of not following matters: if you want to know what the link itself looks like, run plain stat; if you want to know what the file at the other end looks like, add -L. Plenty of people trip here: they stat a symlink, see 9 bytes, and conclude the target file is 9 bytes — when 9 is only the length of the path text.
A directory's "size" is the most counterintuitive number of all. The real run: mkdir sub, put a 1-byte file sub/a.txt inside it, then stat -c '%n %s bytes type=%F' sub prints sub 60 bytes type=directory. The directory holds a single 1-byte file, yet stat calls the directory 60 bytes — because stat reports the size of the directory file itself, the table of names and inode numbers, not the total of everything inside it. The more files a directory holds, the bigger that number gets; delete the files and it does not shrink back right away. If you want "how much disk does this directory and everything in it take", that is du's job, covered in an earlier guide, "Linux disk usage from zero: df, du, and finding big files". stat minds the record book; totalling is somebody else's work. Seen the other way round, this is exactly stat's value: when you want a directory's own metadata — permissions, owner, modification time — stat gives it to you in one step, and du cannot.
stat has a second face: -f (--file-system), which reports on the filesystem the file lives on instead of the file itself. The real run: stat -f -c 'type=%T block size=%s total blocks=%b free blocks=%f' . printed type=tmpfs block size=4096 total blocks=131072 free blocks=129914. %T is the filesystem type, here tmpfs; %s is the block size, 4096; %b is the total block count, 131072; %f is the free block count. Note that in -f mode the same percent letters mean completely different things: %s is no longer the file size but the block size, and %b is no longer the allocated block count but the total block count. The manual lists the two sets of formats separately precisely so you do not mix them. When do you use -f? When you want to know what kind of filesystem a file lives on — ext4 or tmpfs, how big its blocks are, how much room is left. The df guide covers "the disk is full, first see which disk and which kind of full" — a checkup for the whole machine. stat -f is "which building does this file live in, and how many rooms are free" — a question about one spot. The two complement each other; they do not repeat each other.
Script writers love -t (--terse) most: one line, every field in a fixed order, separated by spaces, easiest for a machine to read. The real run: stat -t hello.txt printed hello.txt 11 8 8180 0 0 58 62 1 0 0 1577934245 1577934245 1796237710 1796237710 4096. In order, that is: file name, size, block count, raw mode in hex, owner id, group id, device number, inode number, link count, device type, access time, modification time, status change time, birth time — all four as seconds since 1970 — and the optimal I/O block size. The -c format string that produces the same line is %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o, and the manual prints it verbatim. The seconds forms (%Y, %X, %Z, %W) are the most useful in scripts: to test "was this file changed within the last hour", subtract %Y from date +%s — no need to parse a human date like "2020-01-02 03:04:05". You can even ask for precision: %.3Y prints to milliseconds, %.Y to nanoseconds — the manual gives the examples, %.3Y printing something like 1288929712.114.
The seconds forms also make comparisons between files trivial, which is where stat earns its keep in automation. Suppose a build script must rebuild only when a source file is newer than its output: read both modification times with stat -c %Y source.c and stat -c %Y output.o, and compare the two numbers with a plain integer test — newer means a bigger number, no date parsing anywhere. The same pattern guards configuration reloads: a daemon that watches its config file can poll stat -c %Y config.ini once a second and reload only when the number moves. And because %Y is just an integer, you can do arithmetic on it: the output of date +%s minus the output of stat -c %Y file is the file's age in seconds, and dividing by 3600 gives its age in hours. Human-readable dates are for humans; scripts should always ask stat for the seconds.
Two fields people usually overlook are worth a paragraph. %m is the mount point: which mount point this file lives under. On one machine, /home can be its own disk while /tmp is a tmpfs in memory, and stat -c '%m' tells you at once where a given file actually lives. In our run, hello.txt sat in /tmp, which is tmpfs — that is why -f mode reported the type as tmpfs, and it also explains why the file has a Birth timestamp at all: tmpfs records creation times, while some older filesystems and network filesystems may show a dash instead. The device number (Device: 0,88) and the inode number (Inode: 62) are completely different on every machine and every filesystem, so never copy them into an article or a script as if they were fixed values. There is also a rarely used option, --cached, taking always, never, or default, which controls whether stat uses the filesystem's cached attributes. On a local disk you will never feel the difference; on a remote filesystem such as NFS it matters — always takes the cached, possibly stale values and is fast, never synchronises with the server every time and is slow but fresh. You will not need it daily, but when you hit the strange case of "another machine just changed this file and stat here still shows the old time", that is the switch to remember.
One more practical corner: filenames with spaces, quotes, or newlines in them. stat -c '%n' prints the name exactly as it is, which is fine for reading but dangerous for scripts — a filename with a space in it breaks naive parsing, and a filename with a newline in it breaks line-based parsing altogether. That is what %N is for: it prints the name quoted, with dereferencing if the file is a symbolic link. The quoting style is controlled by the QUOTING_STYLE environment variable, and the default is shell-escape-always, which quotes in a way you can paste straight back into a shell. The manual lists the styles: literal prints the name as-is, shell quotes only when the name needs it, shell-escape also escapes non-printable characters using the shell's dollar-quote form, and c-maybe uses C-style quoting. For everyday use the rule is simple: when a human reads the output, %n is enough; when a script reads the output, prefer %N or, better still, feed the filenames to stat with null separation from find -print0 and read them with a loop that understands nulls. Filenames on Linux can contain almost anything except a slash and a null byte, so sooner or later you will meet one with a space, a quote, or a newline — and the day you do, the difference between %n and %N is the difference between a script that works and one that silently processes the wrong file.
A file that is not there gets a blunt answer from stat: an error, and exit code 1. The real run: stat nope.txt printed stat: cannot statx 'nope.txt': No such file or directory, rc=1. Note the wording, "cannot statx" — newer coreutils calls the statx system call underneath, so the error mentions statx rather than stat. When you pass several files and one of them is missing, stat reports the ones it can and errors on the one it cannot, and the exit code is still 1. The real run: stat -c '%n %s bytes' hello.txt sub/a.txt, with sub/a.txt not yet created, first printed hello.txt 11 bytes, then the error, then rc=1. Remember this when you write scripts: do not judge by whether there was output — check the exit code. If even one file is missing, the exit code is 1, even if every other file was reported normally.
Finally, three recipes you can use as they are. First, find the most recently changed file in a directory: stat -c '%Y %n' * | sort -n | tail -1. %Y is the modification time in seconds, sort -n orders numerically, tail -1 takes the last line — that is the file changed most recently. Second, check whether a deployment actually landed: record stat -c %Y app.py before deploying, run it again after, and if the seconds changed, the file was really replaced; if they did not, the deploy script never wrote it — more reliable than comparing sizes, because equal size with different content is common. Third, a patrol report: stat -c '%n %s %a %Y' release/* prints one line per file with name, size, permissions, and modification seconds — ready to paste into a report or feed into a monitoring script. One more, less obvious: stat -c %s on a downloaded file, compared with the byte count on the release page. If the sizes disagree, do not bother installing it. And to confirm the contents were not swapped, go back to the sha256sum guide's fingerprint check: size can catch "downloaded halfway", it cannot catch "contents replaced".
Four traps beginners fall into, collected here in one place. First, taking ctime for the creation time: ctime is Change time, it moves whenever metadata moves; the creation time is called Birth, read it with %w. Second, taking stat's Size for disk usage: Size is the length of the content, disk usage is Blocks; a 1-byte file occupies 4096 bytes of disk, and a 1 GB file full of zeros may occupy almost no disk at all. Third, statting a symlink and forgetting -L: seeing 9 bytes and concluding the target is 9 bytes, when 9 is only the length of the path text. Fourth, building output with --printf and forgetting \n: several lines of results glue into one, and you think the command printed only one line. And one more basic point: stat never reads contents, so it cannot answer "does this file contain this line" (that is grep's job), "is this file text" (that is file's job), or "how big is this directory in total" (that is du's job). Every tool minds one patch of ground; stat minds the record book.
To keep this guide from repeating ones already published, here are the boundaries. ls has no guide of its own yet, but the idea is worth separating: ls lists "what is in this directory", stat reads out "the record of this one file" — one is an inventory, the other is a file. The file permissions guide teaches how to change permissions (chmod, umask); this guide only teaches how to read them, and teaches nothing about changing. The df and du guides cover "where did the disk space go"; this guide's -f only mentions the filesystem in passing, and its focus stays on single files. The sha256sum guide covers "were the contents tampered with"; this guide covers "what does the record say" — one minds content, the other minds metadata, and a download check needs both: stat first to see whether the size matches, then sha256sum to see whether the fingerprint matches. The find and xargs guide covers "finding files by criteria"; once you have found one and want its full record, hand it to stat. Each guide minds its own patch, and together they add up to seeing a file clearly.
By now you have one more skill in hand: instead of only seeing ls's one short line, you can pull out any file's complete record — how big, whose, what permissions, when it was read, when its contents changed, when its record changed, when it was created, and which filesystem it lives on. The next time someone asks you "was this file actually touched", do not answer in a hurry. Run stat first: mtime and ctime side by side, and the answer speaks for itself. Run the demo in this guide yourself — change a permission once, make a symlink, weigh a directory — and the four timestamps and the two size numbers will stop being letters in a manual and become experience of your own.

Comments (0)
Sources
Related articles
Uppercase a passage, strip the digits out of a phone number, squeeze five spaces into one, delete the carriage returns Windows left behind — tr does all of it one character at a time. This guide runs every command for real on GNU coreutils 9.4, including three measured findings: a short SET2 repeats its last character, complement deletion eats the newline too, and tr takes no filename at all.
Downloaded an installer or a model weights file — how do you know it arrived intact? sha256sum computes a 64-hex fingerprint for any file: change one byte and the fingerprint changes completely. This guide runs every command for real on GNU coreutils 9.4: fingerprints, manifests, bulk checks, deliberate tampering, a missing file, quiet mode — plus the measured finding that --ignore-missing skips missing files, not mismatched ones.
Waiting for a build, a log, a status flip — much of the time you don't need a script, just one command that re-runs every few seconds and pins the result to the screen. This guide runs every watch command for real on procps-ng 4.0.4: the default interval, the header, -d highlighting, -g stop-on-change, -q stop-on-unchanged, -e freeze-on-error — plus the measured finding that -e's exit code is always 8, whatever the manual promises.