Table of Contents
In this article, I will take you through 6 Popular Examples of stat command in Linux/Unix. stat is an open source command Line tool to check the File and Filesystem stats. This tool will provide useful information about file/filesystem size, total blocks, available blocks, file permissions, file access time, file modify time, file change time etc. We will go through multiple examples to understand the usage of stat command in Linux.
Syntax
stat [OPTION]… FILE…
Examples of stat command in Linux/Unix
Also Read : 8 Most Popular mkdir command in Linux with Examples
Example 1: How to check the version of stat command in Linux/Unix
If you want to check the stat command in Linux then you need to use stat --version
command as shown below. As you can see from below output, current stat version is 8.22
.
[root@localhost ~]# stat --version stat (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Michael Meskes.
NOTE:
root
user to run all the below commands.You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.Example 2: How to check Filesystem status using stat command in Linux/Unix
If you want to check the Filesystem status then you need to use -f
option with stat command in Linux as shown below. In this example, we are trying to check the status of /dev
filesystem using stat -f /dev
command.
[root@localhost ~]# stat -f /dev File: "/dev" ID: 0 Namelen: 255 Type: tmpfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 355189 Free: 355189 Available: 355189 Inodes: Total: 355189 Free: 354803
-f : display file system status instead of file status.
Below are the output parameters of stat command in Linux/Unix.
File : Name of the given file.
ID : id of the given file
Namelen : Maximum Length of File Name
Type : type of filesystem (for only Filesystems)
Size : size of the file in bytes.
Blocks : Number of allocated blocks the file takes.
IO Block : Block size in bytes
File type : Regular File, Soft Link or Hard Link of original file etc.
Device : Device number in hex and decimal.
Uid : User ID /File Owner Name.
Gid : Group ID/File Group Name.
Inode : Inode number.
Links : Total number of hard links.
Access : File permissions in the numeric and symbolic methods.
Context : SELinux status
Access : Last File Access time.
Modify : Last File Modified time.
Change : Last File Attribute change time.
Birth : File creation time (only supported in Unix).
Example 3: How to Check File Status using stat command in Linux/Unix
If you want to check the file status then you need to use below stat command in Linux. Format specifiers can be used to get only the required information about a file and filesystem in Linux/Unix instead of showing complete information. In this example, we are trying to check the status of file testfile.txt
using stat testfile.txt
as specified below.
[root@localhost ~]# stat testfile.txt File: ‘testfile.txt’ Size: 58 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 67352527 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-06-18 03:51:36.358476235 -0400 Modify: 2020-06-18 03:51:26.877245520 -0400 Change: 2020-06-18 03:51:26.883745450 -0400 Birth: -
Example 4: How to Use Format Specifiers with stat command in Linux
There are multiple format specifiers available to be used with stat
command in Linux. We will go through all of them one by one in below sections.
[root@localhost ~]# stat --format="%a" /etc/ssh/sshd_config 600
%a : access rights in octal
[root@localhost ~]# stat --format="%A" /etc/ssh/sshd_config -rw-------
%A : access rights in human readable form
[root@localhost ~]# stat --format="%b" /etc/ssh/sshd_config 8
%b : number of blocks allocated (see %B)
[root@localhost ~]# stat --format="%B" /etc/ssh/sshd_config 512
%B : the size in bytes of each block reported by %b
[root@localhost ~]# stat --format="%C" /etc/ssh/sshd_config system_u:object_r:etc_t:s0
%C : SELinux security context string
[root@localhost ~]# stat --format="%d" /etc/ssh/sshd_config 64768
%d : device number in decimal
[root@localhost ~]# stat --format="%D" /etc/ssh/sshd_config fd00
%D : device number in hex
[root@localhost ~]# stat --format="%f" /etc/ssh/sshd_config 8180
%f : raw mode in hex
[root@localhost ~]# stat --format="%F" /etc/ssh/sshd_config regular file
%F : file type
[root@localhost ~]# stat --format="%g" /etc/ssh/sshd_config 0
%g : group ID of owner
[root@localhost ~]# stat --format="%G" /etc/ssh/sshd_config root
%G : group name of owner
[root@localhost ~]# stat --format="%h" /etc/ssh/sshd_config 1
%h : number of hard links
[root@localhost ~]# stat --format="%i" /etc/ssh/sshd_config 219047
%i : inode number
[root@localhost ~]# stat --format="%m" /etc/ssh/sshd_config /
%m : mount point
[root@localhost ~]# stat --format="%n" /etc/ssh/sshd_config /etc/ssh/sshd_config
%n : file name
[root@localhost ~]# stat --format="%N" /etc/ssh/sshd_config ‘/etc/ssh/sshd_config’
%N : quoted file name with dereference if symbolic link
[root@localhost ~]# stat --format="%o" /etc/ssh/sshd_config 4096
%o : optimal I/O transfer size hint
[root@localhost ~]# stat --format="%s" /etc/ssh/sshd_config 3907
%s : total size, in bytes
[root@localhost ~]# stat --format="%t" /etc/ssh/sshd_config 0
%t : major device type in hex, for character/block device special files
[root@localhost ~]# stat --format="%T" /etc/ssh/sshd_config 0
%T : minor device type in hex, for character/block device special files
[root@localhost ~]# stat --format="%u" /etc/ssh/sshd_config 0
%u : user ID of owner
[root@localhost ~]# stat --format="%U" /etc/ssh/sshd_config root
%U : user name of owner
[root@localhost ~]# stat --format="%w" /etc/ssh/sshd_config -
%w : time of file birth, human-readable; – if unknown
[root@localhost ~]# stat --format="%W" /etc/ssh/sshd_config 0
%W : time of file birth, seconds since Epoch; 0 if unknown
[root@localhost ~]# stat --format="%x" /etc/ssh/sshd_config 2020-06-19 02:14:29.116567465 -0400
%x : time of last access, human-readable
[root@localhost ~]# stat --format="%X" /etc/ssh/sshd_config 1592547269
%X : time of last access, seconds since Epoch
[root@localhost ~]# stat --format="%y" /etc/ssh/sshd_config 2019-08-08 21:40:39.000000000 -0400
%y : time of last modification, human-readable
[root@localhost ~]# stat --format="%Y" /etc/ssh/sshd_config 1565314839
%Y : time of last modification, seconds since Epoch
[root@localhost ~]# stat --format="%z" /etc/ssh/sshd_config 2020-04-23 16:10:02.267286283 -0400
%z : time of last change, human-readable
[root@localhost ~]# stat --format="%Z" /etc/ssh/sshd_config 1587672602
%Z : time of last change, seconds since Epoch
Example 5: How to follow Link using stat command in Linux/Unix
If you want to follow the Links then you need to use -L
option with stat command in Linux as shown below. In this example, we are trying to follow the Link of file /etc/ssh/sshd_config
using stat -L /etc/ssh/sshd_config
command.
[root@localhost ~]# stat -L /etc/ssh/sshd_config File: ‘/etc/ssh/sshd_config’ Size: 3907 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 219047 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-06-19 02:14:29.116567465 -0400 Modify: 2019-08-08 21:40:39.000000000 -0400 Change: 2020-04-23 16:10:02.267286283 -0400 Birth: -
-L : follow links
Example 6: How to check other options of stat command in Linux/Unix
If you want to check all the other options of stat command in Linux then you need to use stat --help
command as shown below.
[root@localhost ~]# stat --help Usage: stat [OPTION]... FILE... Display file or file system status. Mandatory arguments to long options are mandatory for short options too. -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; output a newline after each use of FORMAT --printf=FORMAT like --format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT -t, --terse print the information in terse form --help display this help and exit --version output version information and exit The valid format sequences for files (without --file-system): %a access rights in octal %A access rights in human readable form %b number of blocks allocated (see %B) %B the size in bytes of each block reported by %b %C SELinux security context string %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, human-readable; - if unknown %W time of file birth, seconds since Epoch; 0 if unknown %x time of last access, human-readable %X time of last access, seconds since Epoch %y time of last modification, human-readable %Y time of last modification, seconds since Epoch %z time of last change, human-readable %Z time of last change, seconds since Epoch
Recommended Posts:-
10 Useful iproute2 tools examples to Manage Network Connections in Linux
The Power of Linux History Command in Bash Shell
stat command in Linux: Man Page
How to Use Linux History Command
Linux History Command Tutorial for Beginners
Getting Started: Setup FreeRadius Server
5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux