stat

file systemLinux/Unix
The stat command is one of the most frequently used commands in Linux/Unix-like operating systems. stat Display file or file system status

Quick Reference

Command Name:

stat

Category:

file system

Platform:

Linux/Unix

Basic Usage:

stat [options] [arguments]

Common Use Cases

    Syntax

    stat [options] file...

    Options

    Option Description
    -L, --dereference Follow links
    -f, --file-system Display file system status instead of file status
    -c, --format=FORMAT Use the specified FORMAT instead of the default
    --printf=FORMAT Like --format, but interpret backslash escapes
    -t, --terse Print the information in terse form
    --help Display help and exit
    --version Output version information and exit
    Format Sequence Description
    %a Access rights in octal
    %A Access rights in human-readable form
    %b Number of blocks allocated
    %B The size in bytes of each block
    %d Device number in decimal
    %D Device number in hex
    %f Raw mode in hex
    %F File type
    %g Group ID of owner
    %G Group name of owner
    %h Number of hard links
    %i Inode number
    %m Mount point
    %n File name
    %N Quoted file name with dereference if symbolic link
    %o Optimal I/O transfer size hint
    %s Total size, in bytes
    %t Major device type in hex, for character/block device special files
    %T Minor device type in hex, for character/block device special files
    %u User ID of owner
    %U User name of owner
    %w Time of file birth, or -1
    %W Time of file birth as seconds since Epoch, or 0
    %x Time of last access
    %X Time of last access as seconds since Epoch
    %y Time of last data modification
    %Y Time of last data modification as seconds since Epoch
    %z Time of last status change
    %Z Time of last status change as seconds since Epoch
    File System Format Sequence Description (with -f option)
    %a Free blocks available to non-superuser
    %b Total data blocks in file system
    %c Total file nodes in file system
    %d Free file nodes in file system
    %f Free blocks in file system
    %i File system ID in hex
    %l Maximum length of filenames
    %n File name
    %s Block size (for faster transfers)
    %S Fundamental block size (for block counts)
    %t Type in hex
    %T Type in human-readable form

    Examples

    How to Use These Examples

    The examples below show common ways to use the stat command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    stat file.txt
    Display detailed status information about file.txt.
    stat -f /home
    Display filesystem status of the /home filesystem.
    stat -c "%n %s %A" file.txt
    Display the name, size, and permissions of file.txt. # Advanced Examples Advanced # Show file owner and group stat -c "%U %G" file.txt # Show inode number stat -c "%i" file.txt # Show file access times stat -c "Access: %x\nModify: %y\nChange: %z" file.txt # Format size in human-readable form stat -c "File: %n\nSize: %s bytes (%h)" file.txt # Check if a file is a regular file and executable if stat -c "%F %a" file.txt | grep -q "regular file.*[0-7][0-7][1357]"; then echo "Regular executable file" fi # Compare modification times of two files file1_time=$(stat -c "%Y" file1.txt) file2_time=$(stat -c "%Y" file2.txt) if [ "$file1_time" -gt "$file2_time" ]; then echo "file1.txt is newer than file2.txt" fi # Batch process files by type for file in *; do type=$(stat -c "%F" "$file") case "$type" in "regular file") echo "Regular: $file" ;; "directory") echo "Dir: $file" ;; "symbolic link") echo "Link: $file" ;; esac done # Create a simple backup system based on modification time for file in *.txt; do mod_time=$(stat -c "%y" "$file" | cut -d' ' -f1) cp "$file" "backup/$file.$mod_time" done # Check disk usage percentage stat -f -c "Used: %p%" /home # Show formatted details for multiple files stat -c "File: %n\nSize: %s\nPerms: %A\nOwner: %U\nGroup: %G\n" *.txt

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `stat` command is a versatile utility in Unix-like operating systems that displays detailed information about files, directories, or file systems. Unlike simpler commands like `ls`, which provide basic file listings, `stat` offers comprehensive metadata about files and the underlying file systems they reside on. At its core, `stat` provides access to the data returned by the stat() system call, which retrieves file status information from the inode table. This information includes file size, permissions, ownership, various timestamps, inode numbers, device information, and more. The command can also show information about file systems when used with the `-f` option. One of the most powerful features of `stat` is its highly customizable output format. Using the `-c` (or `--format`) option, users can specify exactly which information they want displayed and how it should be formatted. This makes `stat` extremely useful in scripts where specific file attributes need to be extracted and processed. Key uses of the `stat` command include: 1. File Inspection: Examining detailed metadata about files for troubleshooting or information gathering. 2. Timestamp Analysis: Checking file creation, modification, and access times, which is useful for forensics, backup management, and determining file usage patterns. 3. Permission and Ownership Verification: Obtaining detailed information about file permissions and ownership in both numeric and symbolic formats. 4. Filesystem Analysis: When used with the `-f` option, examining filesystem characteristics like total size, free space, and filesystem type. 5. Scripting and Automation: Extracting specific file attributes in a scriptable format for use in shell scripts and automation tasks. 6. Inode Information: Retrieving low-level inode details that aren't visible through standard file listing commands. 7. Device Information: For special files, showing the major and minor device numbers and other device-specific details. The `stat` command is particularly valuable for system administrators, developers, and advanced users who need to go beyond the basic file information provided by commands like `ls`. Its flexibility in formatting output makes it an essential tool for shell scripting and detailed file system analysis. It's worth noting that while the basic functionality of `stat` is consistent across Unix-like systems, there can be slight variations in available options and format specifiers between different implementations, such as between GNU stat (common on Linux) and BSD stat (found on macOS and BSD systems).

    Related Commands

    These commands are frequently used alongside stat or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the stat command works in different scenarios.

    $ stat
    View All Commands