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
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
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
lsmod command in Linux with Examples lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command lin
1 min read
uname command in Linux with Examples Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
printf command in Linux with Examples The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read
username Command in Linux With Examples Linux as an operating system holds the capabilities of handling multiple users each with a username and a display name (Full Name). So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added it
4 min read
readelf command in Linux with Examples When we compile source code, an object file is generated of the program and with the help of linker, this object files gets converted to a binary file which, only the machine can understand. This kind of file follows some structures one of which is ELF(Executable and Linkable Format). And to get the
4 min read
df command in Linux with Examples Ever felt the chilling fear of a "disk full" error message on your Linux machine? Fear not, for the mighty df command stands ready to guide you through the treacherous terrain of disk space management! This article delves deep into the df command, equipping you with the knowledge and skills to navig
5 min read
as command in linux with examples as command is the portable GNU assembler in Linux. Using as command, we can read and assemble a source file. The main purpose of 'as' is to assemble the output of the GNU compiler of the C language.as command reads and assembles the .s File. Also, whenever you do not specify the file, it normally re
3 min read