Monday, January 23, 2012

Unix Stat Command: How To Identify File Attributes


Question: How do I find out all the available file attributes. i.e I would like to know more about a file or directory than what the ls -l command displays.
Answer: Everything in Unix is treated as files. This includes devices, directories and sockets — all of these are files. Stat command displays file or filesystem status as explained in this article.

File Stat – Display Information About File

For example, to find out more information about 101hacks.txt file, execute the stat commandas shown below.
$ stat 101hacks.txt
  File: `/home/sathiyamoorthy/101hacks.txt'
  Size: 854        Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d Inode: 1058122     Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/ sathiya)   Gid: ( 1000/ sathiya)
Access: 2009-06-28 19:29:57.000000000 +0530
Modify: 2009-06-28 19:29:57.000000000 +0530
Change: 2009-06-28 19:29:57.000000000 +0530

Details of Linux Stat Command Output

  • File: `/home/sathiyamoorthy/101hacks.txt’ – Absolute path name of the file.
  • Size: 854 – File size in bytes.
  • Blocks: 8 – Total number of blocks used by this file.
  • IO Block: 4096 – IO block size for this file.
  • regular file – Indicates the file type. This indicates that this is a regular file. Following are available file types.
    • regular file. ( ex: all normal files ).
    • directory. ( ex: directories ).
    • socket. ( ex: sockets ).
    • symbolic link. ( ex: symbolic links. )
    • block special file ( ex: hard disk ).
    • character special file. ( ex: terminal device file ).
  • Device: 801h/2049d  – Device number in hex and device number in decimal
  • Inode: 1058122 – Inode number is a unique number for each file which is used for the internal maintenance by the file system.
  • Links: 1 – Number of links to the file
  • Access: (0600/-rw——-): Access specifier displayed in both octal and character format. Let us see explanation about both the format.
  • Uid: ( 1000/ sathiya) – File owner’s user id and user name are displayed.
  • Gid: ( 1000/ sathiya) – File owner’s group id and group name are displayed.
  • Access: 2009-06-28 19:29:57.000000000 +0530 – Last access time of the file.
  • Modify: 2009-06-28 19:29:57.000000000 +0530 – Last modification time of the file.
  • Change: 2009-06-28 19:29:57.000000000 +0530 – Last change time of the inode data of that file.

Dir Stat – Display Information About Directory

You can use the same command to display the information about a directory as shown below.
$ stat /home/ramesh
File: `/home/ramesh'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 5521409 Links: 7
Access: (0755/drwxr-xr-x) Uid: ( 401/ramesh) Gid: ( 401/ramesh)
Access: 2009-01-01 12:17:42.000000000 -0800
Modify: 2009-01-01 12:07:33.000000000 -0800
Change: 2009-01-09 12:07:33.000000000 -0800

Details of File Permission:

File Permission In Octal Format

This information about the file is displayed in the Access field when you execute stat command. Following are the values for read, write and execute permission in Unix.
  • Value Meaning
  • 4 Read Permission
  • 2 Write Permission
  • 1 Execute Permission

File Permission In Character Format

This information about the file is displayed in the Access field when you execute stat command.
  • File Type: First bit of the field mentions the type of the file.
  • User Permission: 2nd, 3rd and 4th character specifies the read, write and execute permission of the user.
  • Group Permission: 5th, 6th and 7th character specifies the read, write and execute permission of the group.
  • Others Permission: 8th, 9th and 10th character specifies the read, write and execute permission of the others.

Display Information About File System

You can also use stat command to display the file system information as shown below.
$ stat -f /
  File: "/"
    ID: 0        Namelen: 255     Type: ext2/ext3
Blocks: Total: 2579457    Free: 1991450    Available: 1860421    Size: 4096
Inodes: Total: 1310720    Free: 1215875