Get a file size in bytes
Print the size of a file in bytes — for quotas, validation, and disk-usage scripts.
How to get a file size in bytes in each shell
Bashunix
stat -c %s file.txtZshunix
stat -c %s file.txtFishunix
stat -c %s file.txtPowerShellwindows
(Get-Item file.txt).Lengthcmd.exewindows
for %A in (file.txt) do @echo %~zA`%~zA` returns the size of file `%A` in bytes (decimal). In a `.bat` file double the percent: `%%A` and `%%~zA`.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- macOS BSD `stat` uses DIFFERENT flags than GNU: `stat -c %s` (GNU/Linux) vs `stat -f%z file` (BSD/macOS). On BSD `%s` returns mode bits, not size — a silent-wrong answer if you forget to differentiate. Cross-platform portable: `wc -c < file` (reads file vs stat metadata — slightly slower but works everywhere) or detect via `stat -c %s file 2>/dev/null || stat -f%z file`.
- `wc -c` and `stat` BOTH report the byte-on-disk size — file SIZE, not file SPACE-USED (which includes filesystem block-padding). For space-used use `du -b file` (GNU only, --apparent-size) or `du -k file` (1KB blocks; multiply by 1024). On a sparse file (mostly zeros, e.g. VM disk image), `stat`/`wc -c` report the LOGICAL size; `du` reports the physical-blocks-allocated size. They can differ by orders of magnitude.
- pwsh `(Get-Item file).Length` returns `[long]` bytes — direct, no parsing. For directories `(Get-ChildItem dir -Recurse | Measure-Object -Property Length -Sum).Sum` walks contents. Beware `Get-Item directory` returns a `DirectoryInfo` whose `.Length` is NOT defined (throws on access) — use `Get-ChildItem` and sum.
- Human-readable sizes: `ls -lh file | awk '{print $5}'` (auto-suffixed); `du -h file | cut -f1`; pwsh `\"{0:N0}\" -f (Get-Item file).Length` for thousands-separator. Note: GNU `ls -lh` and `du -h` are binary (KiB/MiB — 1024-based) by default; `ls --si` / `du --si` switch to decimal (KB/MB — 1000-based, matches drive-vendor labels). Mixing units is a common confusion source — a "100 GB" drive holds ~93 GiB.
Related commands
Related tasks
- Get file modification time— Print the last-modification timestamp of a file — useful for cache invalidation, build-system checks, and freshness validation.
- Find files larger than 100MB— List files exceeding a size threshold — useful for cleanup, quota enforcement, and identifying log-rotation gaps.
- Show disk usage by folder— See which subdirectories consume the most disk space — for "where did 50GB go" investigations or post-clean audits.
- Find the largest files in a directory— Identify the top N largest files under a directory tree, sorted by size.