Stat command in Linux with examples
Last Updated :
08 Sep, 2024
The 'stat' command is an invaluable tool in the Linux command suite, providing detailed statistics about files and file systems. It displays crucial information such as file size, permissions, ownership, and timestamps, making it an essential utility for system administrators and power users.
What is the 'stat' command?
'stat' serves as a versatile utility that fetches and displays data about files and directories. From determining file types to monitoring file system usage, 'stat' helps users gain insights into various file attributes which are crucial for system management and file handling operations.
Syntax:
 The basic syntax of using stat command is as follows:
stat --options filenames
Stat can accept one or more filenames as an input to the stat command. Now let's see one example
stat /etc/resolv.conf
Output:

Detailed File Information
When executed, 'stat' provides a wealth of information about each specified file:
- File: The name of the provided file. If the provided file is a symlink, then the name will be different.
- Size: The size of a given file in Bytes.
- Blocks: Total number of allocated blocks to the file to store on the hard disk.
- IO Block: The size of every allocated block in bytes.
- File type: The file may be of the following types: Regular files, special files, directories, or symbolic links.
- Device: Device number in hexadecimal format.
- Inode: Inode number of the file.
- Links: Number of hard links of the file.
- Access: Access permissions in the numeric and symbolic methods.
- Context: The field stores SELinux security context.
- Access: The last time at which the file was accessed.
- Modify: The last time at which file was modified.
- Change: The last time the at which file’s attribute or content was changed.
- Birth: The time at which the file was created.
Now let's see how can we display information about the file system.
Displaying the File System Information
stat command can provide the file system information when we provide file name with the -f (--file-system) options. Syntax of using the -f option with the stat command:
stat -f filename
Here is one example of the -f option :

We can also provide the directory or file system as an input to the stat command as follows:
stat -f /

The information we get for the filesystem from the statÂ
- File: The name of provided file.
- ID: File system ID in hexadecimal format.
- Namelen: The maximum length (number of characters) of a file name.
- Fundamental block size: Total size of each block on the file system.
- Blocks:
- Total: Total number of blocks in the file system
- Free: Total number of free blocks in the file system
- Available: Total number of free blocks available for non-root users
- Inodes:
- Total: Total number of inodes in the file system.
- Free: Total number of free inodes in the file system.
Using Stat With Multiple File
To get the information about multiple files using the stat command, just mention the filenames separated by space:
stat locale.conf login.defs
This will show the information about the mentioned files.

Dereference Symlinks
If we provide the symbolic link file to the stat command, then it will give the information about the symbolic link instead of the original file. Here is one example:

Now let's see how we can dereference Symlinks. To follow or dereference the symbolic and display information about the file of which the symbolic like point stat provide -L (--dereference) option. We can use this option to dereference the symbolic links. Here Example:
stat -L /etc/localtime
The output will be like this:

We can see that stat is showing information about the /bin folder.
Customizing the OutputÂ
 stat provides us the option to customize output instead of using the default output format. There are two ways by which we can customize the output
The first is using the '-c' option or the '--format' option with the format of the output. If we provide the multiple files to the stat command with the '--format' option, then it will automatically add the new line after each operand's output:
stat --format='%n' /etc/passwd

Another way to print customized output is using the '--print' option with the format. With this option, we can provide trailing characters to the format for example if we want to add a new line after each line output we can use '\n' trailing characters with a backslash. Here is one of using '--print' option
stat --print='%n\n' /etc/passwd

To know all format sequences, read the man page of the stat using the man command. The stat can also display the information in the terse form. We can display information in the terse form with '-terse' option:
stat -t /etc/passwd

Understanding the Timestamps
Timestamps show the information about the timezone. Generally, the timestamps show the time at which the file was created from the UTC timezone. if the value is positive, then the file was created ahead of the UTC timezone and if the value is negative then the file is created before the UTC timezone.
For example:

In the above example, the value of the timestamp is +0530 which means the file is created 5 hours 30 minutes ahead of UTC time. Here is one note for stat command, your shell may have its own version of the stat. To know about your  shell stat read the man page using the man command:
man stat

Conclusion
The 'stat' command is a powerful tool for Linux users needing detailed file and file system information. Its versatility in handling multiple files, customizable outputs, and comprehensive display of attributes makes it indispensable for effective file management and system administration tasks.
Similar Reads
vmstat command in Linux with Examples
vmstat command in Linux/Unix is a performance monitoring command of the system as it gives the information about processes, memory, paging, block IO, disk, and CPU scheduling. All these functionalities makes the command vmstat also known as virtual memory statistic reporter. The very first report pr
3 min read
mpstat Command in Linux with Examples
mpstat is a powerful and versatile command-line tool within a Linux system that allows detailed reporting of various processor-related statistics. Indeed, part of the sysstat package, mpstat delivers comprehensive CPU utilization and performance data, thus becoming an essential utility for both syst
5 min read
Strace command in Linux with Examples
'strace' is a powerful tool for monitoring and diagnosing processes in Linux. It is primarily used for debugging programs, troubleshooting issues, intercepting and recording system calls, and tracing running processes. It provides valuable insights into how a program interacts with the system, espec
4 min read
sum command in Linux with Examples
The sum command in Linux is used to calculate the checksum and block count of files. This command helps in verifying file integrity by comparing the calculated checksum with an expected value. The sum command can operate on one or more files, and when no file is specified, it reads from standard inp
3 min read
slocate command in Linux with Examples
slocate command in Linux is used to find the files in your system. This is a secure version of the Linux command locate which is also used for file searching, similar to locate command it creates a database of the file locations for faster searching. But it is much more secure than locate command be
3 min read
seq command in Linux with Examples
The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a
4 min read
whatis Command in Linux with Examples
whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument. Syntax of the `whatis` command
5 min read
pmap command in Linux with Examples
The pmap command in Linux is a powerful utility used to display the memory map of a process. A memory map provides insight into how memory is allocated and distributed within a running process. This can be incredibly useful for developers and system administrators when debugging memory issues, optim
3 min read
lshw command in Linux with Examples
The 'lshw' (List Hardware) command in Linux/Unix is a powerful tool for extracting detailed information about the system's hardware configuration. This tool retrieves data from files in the '/proc' directory and is capable of reporting on a wide array of components, including memory configuration, C
3 min read
sync command in Linux with Examples
sync command in Linux is used to synchronize cached writes to persistent storage. If one or more files are specified, sync only them, or their containing file systems. Syntax: sync [OPTION] [FILE]... Note: Nothing is being shown in the screenshots just because sync command makes the cache in the bac
1 min read