Linux‎ > ‎

Linux stat command examples

Thank you for visiting this page, this page has been update in another link Linux stat command examples


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

stat command syntax
The syntax of stat command is :

stat [OPTION]... FILE...

stat command examples

1. A basic example

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.

2. 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 :
#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

The above output indicates that /home was treated as normal file in absence of -f flag.

3. Using format strings to 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
4096
#stat xfs_irecover.c --format=%b
32
#stat xfs_irecover.c --format=%B
512
#stat xfs_irecover.c --format=%g
502

So we see that each format specifier displays output relevant to it. Also if all the format specifiers are given in one go, the output contains status information related to all of them. If you visit the man page of stat command, you will see the following list of 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
    ...
    ...

4. Use another 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

5. 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 format, good for programmers.




Comments