Linux stat Command Tutorial With Examples – POFTUT

Linux stat Command Tutorial With Examples


Linux stat command used to display files and file system information like permission, size etc. In this tutorial we will look various usage examples.

Show File Information

All information about a file can be get without providing any parameter. We will only provide the file name.

$ stat a.txt
Show File Information
Show File Information

As we can see from screenshot there is information about file name , size, blocks, inode, device, access,modify,change dates, user and group id and access rights.

Show File System Information

We can get information about the file system too. But we need some rights to read these file system attributes. This right can  be provided with sudo command or root user.

$ stat /dev/vda1
Show File System Information
Show File System Information

As we can see from screenshot there is information about file name , size, blocks, inode, device, access,modify,change dates, user and group id and access rights.

Show File Permissions

File permissions about a file can be get like below. First command will output humen readable format, second will print octal format.

$ stat --format %A a.txt

OR

$ stat --format %a a.txt

Show SELinux Security Context

SELinux is security implementation about Linux operating systems. Files are restricted according to this security context. This context information can be get like below.

$ stat --format %C a.txt

Show File Type

File type like regular, block, link etc. can be printed with the following command. First command will show in human readable format. Second command will show octal format.

$ stat --format %F a.txt

OR

$ stat --format %f a.txt
Show File Type
Show File Type

Show User

Every file in Linux have a owner user. This user information can be get with stat command. First command will show human readable format, second command will show in decimal number of the user id.

$ stat --format %U a.txt

OR

$ stat --format %u a.txt
Show User
Show User

Show Group

Every file in Linux have a owner group. This group information can be get with stat command. First command will show human readable format, second command will show in decimal number of the group id.

$ stat --format %G a.txt

OR

$ stat --format %g a.txt
Show Group
Show Group

Show Inode Number

Inode numbers are used to specify files in file systems. this inode number can be printed with stat command like below.

$ stat --format %i a.txt
Show Inode Number
Show Inode Number

Show Number Of Hard Links

Linux provides ability to link existing files. Those are two type soft link and hard link. The hard link count can be printed like below.

LEARN MORE  How To Change Directory In CMD (MS-DOS and PowerShell)?

$ stat –format %h a.txt

Show Number Of Hard Links
Show Number Of Hard Links

Show Total Size

The total size of the provided file can be printed like below with stat command.

$ stat --format %s a.txt

Show Last Modification

Las modification time can be printed like below. First will print in  a human readable format and second command will print epoch time.

$ stat --format %y a.txt

OR

$ stat --format %Y a.txt
Show Last Modification
Show Last Modification

Show Last Change

Last change time can be printed like below. First will print in  a human readable format and second command will print epoch time.

$ stat --format %z a.txt

OR

$ stat --format %Z a.txt
Show Last Change
Show Last Change

Show Last Access

Last access time can be printed like below. First will print in  a human readable format and second command will print epoch time.

$ stat --format %x a.txt

OR

$ stat --format %X a.txt
Show Last Access
Show Last Access

Leave a Comment