The basic use of stat command display file in Linux

Time:2020-2-23

Usage: stat [options]… File
Displays the status of a file or file system.

– L, — Reference follow link
– F, — file system displays the file system status rather than the file status
– C — format = format use the specified output format instead of the default value, and change a new line with the specified format every time
— printf = format is similar to — format, but the backslash escape character will be interpreted instead of newline
End of output. If you still want to use newline, you can use the
Add “\ n”
– t, – terse output in compact format
— help display this help message and exit
— version displays version information and exits

Valid file format sequence (do not use — File System):

% a octal permission
% a output permissions in a readable way
% B calculate allocated blocks (see% B)
% B outputs the size of each block reported by% B in bytes
% C SELinux security environment string
% d decimal device number
% d hex device number
% f hex raw mode
% f file type
Group ID of file% G
Group name of file% G
% h number of hard links
% I inode number
% m mount point
% n file name
% n if the object is a symbolic link, display other file names referenced
% o I / O pack size
% s total size in bytes
% t hex master type
% t hex sub device type
The owner ID of the% u file
Primary user name of the% u file
% w file creation time, if unknown “-” will be displayed
% w file creation time in seconds from UNIX original year, if unknown “-” will be displayed
% x last visited
% x last access time in seconds since the UNIX first year
% y last modified
% y last modified in seconds since the UNIX first year
% Z last changed
% Z last changed in seconds from UNIX original

Valid file system format sequence:

% a remaining blocks available to non superusers
% B total number of data blocks in the file system
% C total number of file nodes in the file system
% d number of free file nodes in the file system
% F number of free blocks in the file system
% I hex file system ID
% I maximum allowed file name length
% n file name
% s block size (for fast transfer)
% s base block size (for block count)
% t hex type description
% t readable type description

Note: your shell contains its own version of stat program, which will overwrite the corresponding ones mentioned here
Edition. Check your shell documentation for the options it supports.

The LS command and many of its parameters provide some very useful file information. Another less well-known command, stat, provides some more useful information.
The following shows how to use this command on the executable “oracle” (located in the $oracle_home / bin directory).

Copy code

The code is as follows:

# cd $ORACLE_HOME/bin
# stat oracle
File: `oracle’
Size: 93300148 Blocks:182424 IO Block:4096 Regular File
Device: 343h/835d Inode: 12009652 Links: 1
Access: (6751/-rwsr-s–x) Uid:( 500/ oracle) Gid:( 500/ dba)
Access: 2006-08-04 04:30:52.000000000 -0400
Modify: 2005-11-02 11:49:47.000000000 -0500
Change: 2005-11-02 11:55:24.000000000 -0500

Note the information you get with this command: in addition to the usual file size (which you can also get with the LS – L command), you get the number of blocks the file occupies. Generally, the block size of Linux is 512 bytes, so a file with a size of 93300148 bytes will occupy (93300148 / 512 =) 182226.85 blocks. Because the blocks are completely occupied, the file uses some integer blocks. The exact number of blocks can be obtained without guessing.
You can also obtain the GID and uid of file ownership and the octal representation of permissions (6751) from the above output. If you want to restore a file to the same permissions it now has, you can use Chmod 6751 Oracle instead of spelling those permissions explicitly.
The most useful part of the above output is the file access timestamp information. The output shows that the file was accessed at 04:30:52 on August 4, 2006 (displayed next to “access:”), that is, 4:30:52 am on August 4, 2006. This is when someone started using the database. The modification time of this file is 2005-11-02 11:49:47 (displayed next to “modify:”). Finally, the time stamp next to “change:” shows when the state of the file changed.
The stat command modifier – f displays information about the file system, not the file:

Copy code

The code is as follows:

# stat -f oracle
File: “oracle”
ID: 0 Namelen:255 Type:ext2/ext3
Blocks: Total: 24033242 Free: 15419301 Available: 14198462 Size: 4096
Inodes: Total: 12222464 Free: 12093976

The other option – t displays exactly the same information, just on one line:

Copy code

The code is as follows:

# stat -t oracle
oracle 93300148 182424 8de9 500 500 343 12009652 1 0 0 1154682061
1130950187 1130950524 4096

This is very useful for shell scripts, where you can use a simple cut command to get values for further processing.