Fundamentals 6 min read

Mastering the Linux stat Command: Retrieve Detailed File Metadata Efficiently

This guide explains how to install the GNU coreutils stat command, interpret its comprehensive default output, use the terse option for compact results, and craft custom formats with --printf, providing practical examples and a reference list of format sequences for Linux file metadata extraction.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Linux stat Command: Retrieve Detailed File Metadata Efficiently

Installing the stat command

The stat utility is part of the GNU coreutils package, which is included by default in most Linux distributions. If it is missing, install it with your package manager (e.g., apt install coreutils or yum install coreutils) or compile the package from source at www.gnu.org.

Basic usage and default output

Running stat <file> displays a human‑readable summary of the file’s metadata, including size, block count, I/O block size, device, inode, link count, permissions, UID/GID, SELinux context, and timestamps for access, modification, change, and birth.

$ stat planets.xml
File: planets.xml
Size: 325 Blocks: 8 IO Block: 4096 regular file
Device: fd03h/64771d Inode: 140217 Links: 1
Access: (0664/-rw-rw-r--)  Uid: (1000/tux)  Gid: (100/users)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2021-08-17 18:26:57.281330711+1200
Modify: 2021-08-17 18:26:58.738332799+1200
Change: 2021-08-17 18:26:58.738332799+1200
Birth: 2021-08-17 18:26:57.281330711+1200

Compact (terse) output

For scripts or when you need a single line without headings, use the --terse (or -t) option:

$ stat --terse planets.xml
planets.xml 325881b4100977100 fd03 14021710016291816171629181618162918161816291816174096 unconfined_u:object_r:user_home_t:s0

Custom formatting with --printf

The --printf option lets you define the exact output using format sequences similar to the printf command. Each attribute has a specifier (e.g., %C for SELinux context, %n for file name, %a for permissions).

$ stat --printf="%n
%C
" planets.xml
planets.xml
unconfined_u:object_r:user_home_t:s0

$ stat --printf="Name: %n
Modified: %y
" planets.xml
Name: planets.xml
Modified: 2021-08-17 18:26:58.738332799+1200

Common format sequences

%a

– access permissions %F – file type %n – file name %U – user name %u – user ID %g – group ID %w – birth time %y – modification time

All available specifiers are listed in the stat manual page and the coreutils info documentation.

When to use stat

If you have previously parsed ls -l output, you’ll appreciate stat ’s flexibility. You can retrieve the full set of metadata or just the fields you need, making it a powerful tool for scripts, system audits, and troubleshooting file‑related issues.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashstatfile metadatacoreutils
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.