【Linux】命令 - stat

Posted by 西维蜀黍 on 2021-10-20, Last Modified on 2023-02-16

Usage

# Show file properties such as size, permissions, creation and access dates among others:
$ stat file

# Same as above but verbose (more similar to linux's stat):
$ stat -x file

# Show only octal file permissions:
$ stat -f %Mp%Lp file

# Show owner and group of the file:
$ stat -f "%Su %Sg" file

# Show the size of the file in bytes:
$ stat -f "%z %N" file

含义

$ stat 10g_file.img
  File: 10g_file.img
  Size: 10737418240	Blocks: 20971528   IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 524359      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/      sw)   Gid: ( 1000/      sw)
Access: 2021-10-20 00:05:29.925335381 +0800
Modify: 2021-10-20 00:04:17.067027796 +0800
Change: 2021-10-20 00:04:17.067027796 +0800
 Birth: -
 
$  stat sw_test/10g_file.img
  File: sw_test/10g_file.img
  Size: 10737418240	Blocks: 20971528   IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 2228230     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/      sw)   Gid: ( 1000/      sw)
Access: 2021-10-20 00:05:29.925335381 +0800
Modify: 2021-10-20 00:05:58.946511217 +0800
Change: 2021-10-20 00:05:58.946511217 +0800
 Birth: - 
  • Device: where inode resides

    • Each inode (as well as the associated file) resides in a filesystem that is hosted on a device. That device is identified by the combination of its major ID (which identifies the general class of device) and minor ID (which identifies a specific instance in the general class).
  • Inode: Inode number

    • Each file in a filesystem has a unique inode number.
    • Inode numbers are guaranteed to be unique only within a filesystem (i.e., the same inode numbers may be used by different filesystems, which is the reason that hard links may not cross filesystem boundaries). This field contains the file’s inode number.
  • Links: Link count

    • This field contains the number of hard links to the file.
    • Additional links to an existing file are created using link(2).
  • Uid: User Id

    • This field records the user ID of the owner of the file.
    • For newly created files, the file user ID is the effective user ID of the creating processThe user ID of a file can be changed using chown(2).
  • Gid: Group ID

    • The inode records the ID of the group owner of the file.
    • For newly created files, the file group ID is either the group ID of the parent directory or the effective group ID of the creating process, depending on whether or not the set-group-ID bit is set on the parent directory (see below). The group ID of a file can be changed using chown(2).
  • Device represented by this inode

    • If this file (inode) represents a device, then the inode records the major and minor ID of that device.
  • Size: File size

    • 单位是byte

    • This field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.

      $ ln -s 10g_file.img 10g_file2.img
      $ stat 10g_file2.img
        File: 10g_file2.img -> 10g_file.img
        Size: 12        	Blocks: 0          IO Block: 4096   symbolic link
      Device: fd00h/64768d	Inode: 524364      Links: 1
      Access: (0777/lrwxrwxrwx)  Uid: ( 1000/      sw)   Gid: ( 1000/      sw)
      Access: 2021-10-21 00:06:43.400067996 +0800
      Modify: 2021-10-21 00:06:38.166801009 +0800
      Change: 2021-10-21 00:06:38.166801009 +0800
       Birth: -
      $ stat 10g_file.img
        File: 10g_file.img
        Size: 10737418240	Blocks: 20971528   IO Block: 4096   regular file
      Device: fd00h/64768d	Inode: 524359      Links: 1
      Access: (0664/-rw-rw-r--)  Uid: ( 1000/      sw)   Gid: ( 1000/      sw)
      Access: 2021-10-20 00:05:29.925335381 +0800
      Modify: 2021-10-20 00:04:17.067027796 +0800
      Change: 2021-10-20 00:04:17.067027796 +0800
      
  • Preferred block size for I/O

    • This field gives the “preferred” blocksize for efficientfilesystem I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)
  • Blocks: Number of blocks allocated to the file

    • This field indicates the number of blocks allocated to the file, 512-byte units, (This may be smaller than st_size/512 when the file has holes.)
    $ cat 1.log
    123
    $ stat 1.log
      File: 1.log
      Size: 4         	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 524451      Links: 1
    Access: (0664/-rw-rw-r--)  Uid: ( 1000/      sw)   Gid: ( 1000/      sw)
    Access: 2021-10-21 00:11:17.608576194 +0800
    Modify: 2021-10-21 00:11:17.608576194 +0800
    Change: 2021-10-21 00:11:17.608576194 +0800
     Birth: -
    
     # 可以看到,这个非常小的文件,还是会占用一个IO block(4KB),因此这里的 Blockes 是 8,因为8*512byte=4096byte=4KB
    
  • Access: Last access timestamp (atime)

    • Some filesystem types allow mounting in such a way that file and/or directory accesses do not cause an update of the atime timestamp. (See noatime, nodiratime, and relatime in mount(8), and related information in mount(2).) In addition, the atime timestamp is not updated if a file is opened with the O_NOATIME flag; see open(2).
  • Birth: File creation (birth) timestamp (btime)

    • not returned in the stat structure); statx.stx_btime
    • The file’s creation timestamp. This is set on file creation and not changed subsequently.
    • The btime timestamp was not historically present on UNIX systems and is not currently supported by most Linux filesystems.
  • Modify: Last modification timestamp (mtime)

    • This is the file’s last modification timestamp. It is changed by file modifications, for example, by mknod(2), truncate(2), utime(2), and write(2) (of more than zero bytes). Moreover, the mtime timestamp of a directory is changed by the creation or deletion of files in that directory. The mtime timestamp is not changed for changes in owner, group, hard link count, or mode.
  • Change: Last status change timestamp (ctime)

    • This is the file’s last status change timestamp. It is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

Reference