The Linux command 'stat' is used for displaying status information of Linux files and file systems. In this article, I will show some useful examples.

stat command syntax

stat [OPTION]... FILE...

Default output

In the simplest form, the stat command can be used by supplying a file name as an argument :

#stat sysfs_fc_tools.tar
  File: `sysfs_fc_tools.tar'
  Size: 40960         Blocks: 80         IO Block: 4096   regular file
Device: fd02h/64770d    Inode: 28311964    Links: 1
Access: (0644/-rw-r--r--)  Uid: (  502/atlas001)   Gid: (  502/   atlas)
Access: 2013-07-26 13:36:52.055602317 -0700
Modify: 2013-07-04 10:38:54.934504000 -0700
Change: 2013-07-04 10:39:14.004644700 -0700

So we see that in above output, most useful information related to file 'sysfs_fc_tools.tar' was displayed.

Display file system status using -f flag

If the supplied argument is not a file but a file system instead, then -f flag can be used with the stat command to display the status of a file system.

#stat -f /home
  File: "/home"
    ID: a1fe9726942d5906 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 463119708  Free: 459600050  Available: 436074930
Inodes: Total: 117628928  Free: 117578428

In the above output, all the information related to file system /home was displayed. Also,  if '-f' is not used then stat treats the input as a normal file :

See the different

#stat  /home
  File: `/home'
  Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 2           Links: 7
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-12-11 21:39:42.018425632 -0800
Modify: 2013-11-29 21:37:45.297532773 -0800
Change: 2013-11-29 21:37:45.297532773 -0800

Print specific information through '--format' flag

The flag '--format' can be used to print specific information. The following set of examples illustrates its

usage :
#stat xfs_irecover.c --format=%o
#stat xfs_irecover.c --format=%b
#stat xfs_irecover.c --format=%B
#stat xfs_irecover.c --format=%g

Format specifiers:

    %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

Other type of format output --printf

#stat xfs_irecover.c --printf="filename %n\nsize %s\nblocks %b\nblksize %B\nioblksize %o\ninode %i\n"
filename xfs_irecover.c
size 15624
blocks 32
blksize 512
ioblksize 4096
inode 28314664

Output information in terse form using -t flag

#stat -t xfs_irecover.c
xfs_irecover.c 15624 32 81a4 502 502 fd02 28314664 1 0 0 1386877779 1379477645 1379477645 4096

So we see that the output above was displayed in a specific non human readable format, good for programmers


Comments powered by CComment