0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use stat Command in Linux: Syntax and Examples

Use the stat command in Linux to display detailed information about files or directories, such as size, permissions, and timestamps. The basic syntax is stat [options] filename, which outputs file metadata in a readable format.
📐

Syntax

The basic syntax of the stat command is:

  • stat [options] <file>: Displays detailed information about the specified file or directory.
  • options: Optional flags to customize output, like -c for custom format.
  • file: The path to the file or directory you want to inspect.
bash
stat [options] <file>
💻

Example

This example shows how to use stat to get detailed info about a file named example.txt. It displays size, permissions, owner, and timestamps.

bash
stat example.txt
Output
File: example.txt Size: 1234 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 131073 Links: 1 Access: 2024-06-01 10:00:00.000000000 +0000 Modify: 2024-06-01 09:50:00.000000000 +0000 Change: 2024-06-01 09:55:00.000000000 +0000 Birth: -
⚠️

Common Pitfalls

Common mistakes when using stat include:

  • Trying to use stat on a non-existent file, which causes an error.
  • Not specifying the correct file path, leading to unexpected results.
  • Confusing stat output with ls -l output; stat shows more detailed metadata.

Always check the file path and existence before running stat.

bash
stat nonexistentfile.txt
# Output: stat: cannot stat 'nonexistentfile.txt': No such file or directory
Output
stat: cannot stat 'nonexistentfile.txt': No such file or directory
📊

Quick Reference

OptionDescription
-c Custom output format using format sequences
-fDisplay file system status instead of file status
-tPrint output in terse form
--helpShow help message and exit
--versionShow version information and exit

Key Takeaways

Use stat filename to get detailed file information including size, permissions, and timestamps.
Always verify the file path exists to avoid errors with stat.
Use -c option to customize the output format for scripting needs.
Remember stat shows more detailed metadata than ls -l.
Use stat -f to get file system information instead of file details.