The Linux / Unix stat command

The Unix / Linux stat command displays information about files and folders.
Used without any option, the stat command displays the same info as ls -l, but nicer structured.

But stat can be customized to display only the information you need.

Syntax: stat path/to/folder
AND: stat path/to/file

Stat’s output is the same, for both files and folders.

Display full info about files or folders with stat:

Dir info:

$ stat /etc
File: `/etc'
Size: 12288 Blocks: 24 IO Block: 4096 directory
Device: 801h/2049d Inode: 786433 Links: 147
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-06-15 00:45:47.145960830 +0300
Modify: 2012-06-15 00:06:11.569871033 +0300
Change: 2012-06-15 00:06:11.569871033 +0300

File info:

stat /etc/hosts
File: `/etc/hosts'
Size: 220 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 786605 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-06-14 23:36:41.273805021 +0300
Modify: 2012-05-23 01:02:45.112116304 +0300
Change: 2012-05-23 01:02:45.120116304 +0300

By default, used without any option, stat displays the file/folder size, owner and group, inode number, access / modification time and the last inode change date.

How to display the file system with stat:

$ stat -f /
File: "/"
ID: 829f5624d47ea20 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 4902319 Free: 4092902 Available: 3843878
Inodes: Total: 1245184 Free: 1094524

Or, grep the output by type:
$ stat -f / | grep -i type
ID: 829f5624d47ea20 Namelen: 255 Type: ext2/ext3

Use your customized stat:

Display only the file’s inode: stat -c %i /path/to/file

Display only the file permissions, in octal mode: stat -c %a /path/to/file
Display only the file permissions, in rwx format: stat -c %A /path/to/file

Display only the file’s owner and the owner’s UID: stat -c “%u %U” /path/to/file

Display only the file’s group and the group’s GID: stat -c “%g %G” /path/to/file

For more stat info, type man stat in your terminal/konsole.

I have aliased this stat command:

$ alias mstat
alias mstat='stat -c "|P|: %a |T|: %F |U G|: %u %U %g %G %n"'

My mstat output:
$ mstat /
|P|: 755 |T|: directory |U G|: 0 root 0 root /

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

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

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!