Using command stat to display file status

The stat command can be use to check file status ( like file properties on Windows platform) or check the file system status on Linux. It comes from GNU Coreutils package.

[joseph@fedora ~]$ type -t stat
[joseph@fedora ~]$ type -p stat
[joseph@fedora ~]$ rpm -qf /usr/bin/stat

The stat command will gather information about file or file system status. This command is useful for checking file permission, inode, exact file size and symbolic link, for example:

[joseph@fedora ~]$ stat filename
  File: `filename'
  Size: 1242            Blocks: 8          IO Block: 4096   regular file
Device: 807h/2055d      Inode: 11649036    Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/coremail)   Gid: (  500/coremail)
Access: 2010-06-23 10:16:34.000000000 +0800
Modify: 2010-01-11 12:21:14.000000000 +0800
Change: 2010-06-23 10:17:10.000000000 +0800

The output of this command show size, filetype, permission, user id, group id and so on. for the specified file “filename“. The following are some of the flags and arguments that can be used for the stat command:

-f, --filesystem  display filesystem status instead of file status
-c  --format=FORMAT  use the specified FORMAT instead of the default
-L, --dereference  follow links
-Z, --context print the security context
-t, --terse  print the information in terse form
    --help  display this help and exit
    --version  output version information and exit

From the help file for the command stat above, there is some options to use with the stat command.
(These examples are using stat command on Redhat Enterprise Linux 4 (update 8) with SELinux disabled.)

Display file system status by specifing a file name:

[joseph@fedora ~]$ stat -f filename
  File: "filename"
    ID: 0        Namelen: 255     Type: ext2/ext3
Blocks: Total: 31926232   Free: 4386961    Available: 2765200    Size: 4096
Inodes: Total: 16220160   Free: 15069803

Display file size for a specified file:

[joseph@fedora ~]$ stat -c %s filename

Display file size and file name for a specified file:

[joseph@fedora ~]$ stat -c "%s %n" filename
1242 filename

Here the %s and %n are format sequences for files, a complete list are attached below:
Note that they are valid without –filesystem.

%A Access rights in human readable form
%a Access rights in octal
%B The size in bytes of each block reported by `%b'
%b Number of blocks allocated (see %B)
%D Device number in hex
%d Device number in decimal
%F File type
%f Raw mode in hex
%G Group name of owner
%g Group ID of owner
%h Number of hard links
%i Inode number
%N Quoted File name with dereference if symbolic link
%n File name
%o IO block size
%s Total size, in bytes
%T Minor device type in hex
%t Major device type in hex
%U User name of owner
%u User ID of owner
%X Time of last access as seconds since Epoch
%x Time of last access
%Y Time of last modification as seconds since Epoch
%y Time of last modification
%Z Time of last change as seconds since Epoch
%z Time of last change

Format sequences for file systems:
Note that they are valid only when –filesystem (or -f) option enabled.

%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
%C - Security context in SELinux
%i File System id in hex
%l Maximum length of filenames
%n File name
%s Optimal transfer block size
%T Type in human readable form
%t Type in hex

From the example above, the command stat can be use to check the file or filesystem status, this quite useful if you want to check the size of file, file IO block, file inode number, number of link and access permission granted to the file. For more information on stat command:

# info stat
# man stat
Share Button

Leave a comment

Your email address will not be published. Required fields are marked *