Stat command

Displaying Files and File System information

The following are examples of how to use the Linux "stat" command to display detailed information relating to files and file systems.

Linux stat command examples

The Linux stat command is a tool that can be used for displaying detailed information relating to a file or file system on a Linux system. By default the stat command should be available on most distributions of Linux. In the examples below, a CentOS Linux distribution was used.

Linux Stat Command Syntax

The basic syntax of the "statstrong>" command is as follows:

stat [OPTION]... FILE...

       Display file or file system status.

       -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  new‐
              line after each use of FORMAT

              like  --format, but interpret backslash escapes, and do not out‐
              put 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

              output version information and exit

stat - no parameters passed

The simplest use of the "stat" command is to pass the name of a file to the command. In the example below you can see the default output from the stat command. Basic information such as size, file type, Inode information, Number of links, Access, Modification and Change date and time stamps are displayed. The stat command is often used to view Modification/access times on a file. (A more detailed way of displaying creation time and access time stamps on a file is to use the debugfs command)

# stat anaconda-ks.cfg 
  File: ‘anaconda-ks.cfg’
  Size: 1967      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 135         Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2015-11-21 16:52:25.992046601 +0000
Modify: 2015-11-21 16:52:25.998046601 +0000
Change: 2015-11-21 16:52:25.998046601 +0000
 Birth: -

stat -f - Displaying File System Status

If you wish to view information that relates to a file system and not an individual file, you may pass the "-f" parameter followed by a filesystem:

# stat -f /opt
  File: "/opt"
    ID: fd0600000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 509440     Free: 501208     Available: 501208
Inodes: Total: 2048000    Free: 2047997

Format Sequences

To display specific information relating to a file or file system, you may use what is known as a format sequence. By passing the parameter "-c" or "--format" for files and "-f" or "--file-system" for file systems. You can specify individual points of interest such as inode number, mount point etc.. A list of all the parameters that can be passed for files and file systems is listed at the bottom of the page.

stat --format=%i FileName

As an example we are specifying that we would like to display the inode information of the specified file.

# stat --format=%i anaconda-ks.cfg 

From the above we can see that the inode number for the file "anaconda-ks.cfg" is "135".

stat --format=%i%g%G FileName

In this example we are specifying multiple format sequences that we would like to display.

# stat --format=%i%g%G anaconda-ks.cfg 

In the above example, multiple parameters were passed to the "--format" option. An explanation of these is listed below:

inode number: %i
group ID of owner: %g
group name of owner: %G
file name: anaconda-ks.cfg

The output from the file indicates that the inode number is "135", the group ID of the owner is "0" and the group name of the owner is "root".

stat -f -c %a /opt

The above example of the stat command is used against a file system. In this example, we use the "-f" option indicating that we are passing a file system. The "-c" flag is used to specify the format sequence.

In the example:

%a - free blocks available to non-superuser

# stat -f -c %a /opt

For a full list of parameters that can be used against a file system, see the list below.

Format Sequences for files

The following format sequences are for use with files:

       %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

       %T     minor device type in hex,  for  character/block  device  special

       %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

Format Sequences for File Systems

The following format sequences are for use with File Systems:

       %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