Stat command in Linux with examples

23 Aug, 2021
The stat is a  command which gives information about the file and filesystem. Stat command gives information such as the size of the file, access permissions and the user ID and group ID, birth time access time of the file. Stat command has another feature, by which it can also provide the file system information. This is the best tool to use when we want the information of any file.
Now let’ see one by one how can we use the stat command.
 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:
Following is the information we get about the file when we run the stat command.
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.
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 systemFree: Total number of free blocks in the file systemAvailable: Total number of free blocks available for non-root users
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.
Total: Total number of inodes in the file system.
Free: Total number of free inodes in the file system.
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.
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.
 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
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
linux-command
Linux-file-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
nohup Command in Linux with Examples
mv command in Linux with examples
chmod command in Linux with examples
Array Basics in Shell Scripting | Set 1
Introduction to Linux Operating System
Basic Operators in Shell Scripting