5+ “stat” Command Usage Examples in Linux

«
»

This tutorial explains Linux “stat” command, options and its usage with examples.

stat – display file or file system status

Description :

advertisement

Displays the detailed status of a particular file or a file system.

Usage :
stat [OPTION]… FILE…

Options :

-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 SELinux security context
-t, –terse
print the information in terse form
–help
display this help and exit
–version
output version information and exit

The valid format sequences for files (without –file-system):

%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
%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
%n	 File name
%N	 Quoted file name with dereference if symbolic link
%o	 I/O block size
%s	 Total size, in bytes
%t	 Major device type in hex
%T	 Minor device type in hex
%u	 User ID of owner
%U	 User name of owner
%x	 Time of last access
%X	 Time of last access as seconds since Epoch
%y	 Time of last modification
%Y	 Time of last modification as seconds since Epoch
%z	 Time of last change
%Z	 Time of last change as seconds since Epoch

Valid format sequences for file systems:

advertisement
%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	 Block size (for faster transfers)
%S	 Fundamental block size (for block counts)
%t	 Type in hex
%T	 Type in human readable form

Examples :

1. A basic example

$ stat test.txt 
  File: `test.txt'
  Size: 1014      	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 1448800     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-07-21 17:20:33.548997182 +0530
Modify: 2011-08-16 23:27:19.648480473 +0530
Change: 2011-08-16 23:27:19.648480473 +0530

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 /dev/sda5
  File: "/dev/sda5"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 383415     Free: 383325     Available: 383325
Inodes: Total: 383415     Free: 382530

3. Display file system status without using -f flag

advertisement

So we see that in the above output, all the information related to file system /dev/sda5 was displayed. Also, if -f is not used then stat treats the input as a normal file :

$ stat /dev/sda5
  File: `/dev/sda5'
  Size: 0         	Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d	Inode: 1881        Links: 1     Device type: 8,5
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2012-07-21 20:48:02.578827617 +0530
Modify: 2012-07-21 20:48:02.578827617 +0530
Change: 2012-07-21 20:48:02.578827617 +0530

4. Using format strings to print specific information through ‘–format’ flag

The is done using the -b option.

$ stat --format=%g test.txt
0
$ stat --format=%G test.txt
root
$ stat --format=%i test.txt
1448800
$ stat --format=%n test.txt
testdisk.log
$ stat --format=%g%G%i%n test.txt 
0root1448800test.txt

5. Using format strings for file systems

The same flag ‘–format’ can be used to display selected information for file systems :

$ stat --file-system -c%n /dev/sda5
/dev/sda5
$ stat --file-system -c%d /dev/sda5
382530
$ stat --file-system -c%c /dev/sda5
383415

6. Output information in terse form using -t flag

advertisement
$ stat -t test.txt
test.txt 1014 8 81a4 0 0 805 1448800 2 0 0 1342871433 1313517439 1342884993 4096

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn