← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 13 of 13

stat

In this page:

  1. stat

stat

stat reports detailed metadata about a file, including size, permissions, and timestamps, more comprehensively than ls -l. Its exact output format differs slightly between Linux and macOS, but the core information it exposes is the same. It's often used in scripts that need to check things like file size or modification time programmatically.

Note: Use stat -c %s file on Linux to get just the file size in bytes, suitable for capturing in a variable.

Example: stat

bash
#!/bin/bash
echo "some sample content" > info.txt

stat info.txt

size=$(stat -c %s info.txt 2>/dev/null || stat -f %z info.txt)
echo "File size in bytes: $size"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.