Get file modification time
Print the last-modification timestamp of a file — useful for cache invalidation, build-system checks, and freshness validation.
How to get file modification time in each shell
Bashunix
stat -c %y file.txtZshunix
stat -c %y file.txtFishunix
stat -c %y file.txtPowerShellwindows
(Get-Item file.txt).LastWriteTimecmd.exewindows
forfiles /m file.txt /c "cmd /c echo @fdate @ftime"`@fdate @ftime` are locale-dependent (e.g. `05/16/2026 06:30 PM` US, `16.05.2026 18:30` DE). For ISO-8601 shell out to PowerShell.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- Four timestamps on Unix: **atime** (last access — read or stat), **mtime** (last MODIFICATION — content changed), **ctime** (inode CHANGE — content OR metadata, including chmod/chown — NOT creation time!), **btime/crtime** (birth/creation — added in ext4, only available via `stat -c %w` on newish coreutils). `ctime` is the most-misnamed timestamp in computing; it does NOT mean creation. macOS HFS+/APFS track all four; ext4 + btrfs + XFS track all four.
- GNU `stat -c %y file` outputs locale-formatted (`2026-05-16 06:30:00.123456789 +0800`). For ISO-8601-strict: `stat -c %Y file | date -d @- -Iseconds` (epoch then format) or `date -d "$(stat -c %y file)" -Iseconds`. macOS BSD: `stat -f "%Sm" -t "%FT%T%z" file` (the `-t` formats the time per strftime). pwsh: `(Get-Item file).LastWriteTime.ToString("o")` ISO-8601 with milliseconds.
- noatime mount option: many production servers mount filesystems with `noatime` (or `relatime` — atime only updates when a read occurs after mtime/ctime). After `noatime`, `stat -c %x` always equals the most recent of mtime/ctime — not actually the last access. macOS APFS is `noatime` by default since 10.13. Don't rely on atime for "last-accessed" telemetry without checking the mount options.
- Cross-shell mtime comparison: `[ "$(stat -c %Y a)" -gt "$(stat -c %Y b)" ] && echo "a is newer"` (epoch seconds). pwsh `(Get-Item a).LastWriteTime -gt (Get-Item b).LastWriteTime`. `make` and other build systems compare with nanosecond precision via `stat -c %Y.%N` GNU only — BSD `stat` strips nanoseconds, breaking sub-second `make` checks. Use `make -W file` if you have rebuild-too-often bugs on macOS.
Related commands
Related tasks
- Get a file size in bytes— Print the size of a file in bytes — for quotas, validation, and disk-usage scripts.
- Find files modified today— List every file changed in the last 24 hours, recursively from the current directory.
- Watch a file for changes— React when a file is modified — for tail-style log viewing, hot-reload tooling, or "wait until this completes" automation.
- Find files larger than 100MB— List files exceeding a size threshold — useful for cleanup, quota enforcement, and identifying log-rotation gaps.