stat command in Linux with Examples
stat - display file or file system status
stat [OPTION]... FILE...
stat is a linux command line utility that displays a detailed information about a file or a file system. It retrieves information such as file type; access rights in octal and human-readable; SELinux security context string; time of file creation, last data modification time, and last accessed in both human-readable and in seconds since Epoch, and much more.
It has an option to specify a custom format instead of the default, for displaying information.
The options for
stat commands are:
-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 sequence for files are:
%a access rights in octal (note '#' and '0' printf flags) %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 %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 data modification, human-readable %Y time of last data modification, seconds since Epoch %z time of last status change, human-readable %Z time of last status change, seconds since Epoch
The valid FORMAT sequence for file-system are:
%a free blocks available to non-superuser %b total data blocks in file system %c total file nodes in file system %d free file nodes in file system %f free blocks in file system %i file system ID in hex %l maximum length of filenames %n file name %s block size (for faster transfers) %S fundamental block size (for block counts) %t file system type in hex %T file system type in human readable form
1. When invoked without any options,
stat command displays the following file information like - File name, file size, number of allocated blocks, blocksize, file type, device number, inode number, number of hard links, access file permissions in numiric or symbolic form, last time the file was accessed, last time the file’s content was modified, and last time the file’s attribute or content was changed.
$ stat sample.htm File: 'sample.htm' Size: 3496 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 6424759 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-21 18:29:56.264944616 +0530 Modify: 2021-05-05 17:04:27.680727000 +0530 Change: 2021-05-05 17:08:22.448449292 +0530 Birth: -
-f option to get the information about the file system on which the file resides.
$ stat -f sample.htm File: "sample.htm" ID: 27a574dcbaa73116 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 36999792 Free: 6519334 Available: 4634074 Inodes: Total: 9412608 Free: 8940847
When invoked with
-f option, it displays file sytem information like - File, file system ID in Hex format, maximum length of file names, type of file system, size of each block on the file system, number of total blocks on the file system, number of free blocks in the file system, number of free block available to non-root users, total number of inodes and number of free inodes in the file system.
3. The stat command with
-t option displays the information in a terse format.
$ stat --terse sample.htm sample.htm 3496 8 81b4 1000 1000 805 6424759 1 0 0 1621601996 1620214467 1620214702 0 4096
4. For files that are symbolic link,
stat command would display information related with symbolic file, which means it would not follow the symbolic link. To follow symbolic link and show information about the linked file use
$ stat sample.html File: 'sample.html' -> 'sample.htm' Size: 10 Blocks: 0 IO Block: 4096 symbolic link Device: 805h/2053d Inode: 6424790 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-22 14:39:39.656058591 +0530 Modify: 2021-05-22 14:39:39.652058596 +0530 Change: 2021-05-22 14:39:39.652058596 +0530 Birth: - $ stat -L sample.html File: 'sample.html' Size: 3496 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 6424759 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ expert) Gid: ( 1000/ expert) Access: 2021-05-21 18:29:56.264944616 +0530 Modify: 2021-05-05 17:04:27.680727000 +0530 Change: 2021-05-05 17:08:22.448449292 +0530 Birth: -
stat command with
-c option allows you to use a particular or custom format instead of the default, it prints a newline after each use of format sequence. Some examples are shown below.
$ stat -c=%A views.py =-rw-rw-r-- $ stat -c="%F" views.py =regular file $ stat -c="%n,%F" views.py =views.py,regular file $ stat -c='%a:%F' views.py =664:regular file $ stat -c=%d:%i views.py =2069:3806061 $ stat -c="%U,%F,%s" views.py =expert,regular file,1779
stat command with
-c option enables interpreting of backslash escapes sequences and turns off printing of a trailing newline. You need to use \n in the format to print a new line.
$ stat --printf='Name: %n\nPermissions: %a\n' apps.py Name: apps.py Permissions: 664 $ stat --printf='%U\n%G\n%C\n%z\n' apps.py expert expert stat: failed to get security context of 'apps.py': No data available ? 2021-04-17 06:15:26.011158935 +0530 $ stat --printf='%n\n%a\n%b\n' apps.py apps.py 664 8 $ stat --printf='%i\n' apps.py 3806053 $ stat --printf='%a:%u\n' apps.py 664:1000 $ stat --printf='%d:%i\n' apps.py 2069:3806053 $ stat --printf='Name: %n\nThe time of last data modification: %y\n' apps.py Name: apps.py The time of last data modification: 2021-04-17 06:15:26.011158935 +0530 $ stat --printf="%A %U %s" apps.py -rw-rw-r-- expert 142$ $ stat --printf="%A %U %s\n" apps.py -rw-rw-r-- expert 142