In addition to ls can get a simple metadata, access to detailed metadata file (using system calls): stat (2)
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int stat(const char *path,struct stat *buf);
Function: get file status information
parameter:
path: Specifies the name of the file (including path)
buf: receiving status information for a file (note its type)
return value:
Success: 0
Failed: -1, errno is provided
Original link: https: //blog.csdn.net/qq_42216173/article/details/83088657
Data file system divided into two categories, namely, data and metadata.
Data: means that the average actual data file;
Metadata: used to describe the characteristics of a file system data, such as access rights, file owner, and the distribution information file data blocks and the like;
View metadata files need to use a command: stat
Stat action command information to display the status information of the files, the output ratio of the output of the ls command in more detail.
Example:
[root@centos7mage ~]# stat /etc/sysconfig/network-scripts/ifcfg-eth0
File: ‘/etc/sysconfig/network-scripts/ifcfg-eth0’
Size: 118 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33569832 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:net_conf_t:s0
Access: 2018-10-11 09:46:00.346000000 +0800
Modify: 2018-08-21 16:13:37.654010000 +0800
Change: 2018-08-21 16:13:37.655010000 +0800
Birth: -
File: file name;
Size: File Size;
Blocks: number of disk blocks occupied;
IO Block: IO block size;
regular file: This is the type of show file, which is a common file
Device: device is located;
Inode: Inode node number;
Links: the number of linked; (ln command can be used to add file hard links or soft links)
Access (first): Access;
Uid: uid number and owner;
Gid: gid number and is a group;
Access (second): the last file access time;
Modify: file modification time;
Chang: changing the time of the file.
Modify the file timestamps need to use a command: touch
touch command has two functions:
First, for the existing file time stamp is updated to the current system time (default), their data will be preserved intact;
Second, to create a new empty file.
Example:
Create a new blank file:
[Root @ centos6 app] # touch huangshizhang
[Root @ centos6 app] # stat huangshizhang
File: `huangshizhang '
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 12 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-10-25 09:56:35.134854810 +0800
Modify: 2018-10-25 09:56:35.134854810 +0800
Change: 2018-10-25 09:56:35.134854810 +0800
Modify the file timestamp:
[Root @ centos6 app] # touch huangshizhang
[Root @ centos6 app] # stat huangshizhang
File: `huangshizhang '
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 12 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-10-25 09:56:53.831856377 +0800
Modify: 2018-10-25 09:56:53.831856377 +0800
Change: 2018-10-25 09:56:53.831856377 +0800
The following article text link: https: //www.cnblogs.com/sylar5/p/6491033.html
The use and method of use in the stat function linux
stat function to explain
Header file: #include <sys / stat.h>
#include <unistd.h>
Defined function: int stat (const char * file_name, struct stat * buf);
Function Description: filename file information acquired by the file name, and is stored in a structure referred to in stat buf
Return Value: returns 0 if executed successfully, return -1 failure, an error code is stored in errno
error code:
ENOENT file_name parameter specifies the file does not exist
ENOTDIR path directory exists but is not a true directory
ELOOP want to have too many open files symbolic links problem, the upper limit of 16 symbolic links
EFAULT buf argument is invalid pointer to the memory space can not exist
Access to the document is rejected EACCESS
ENOMEM Insufficient memory core
ENAMETOOLONG parameter file_name path name is too long
#include<sys/stat.h>
#include<unistd.h>
#include<stdio.h>
int main () {
struct stat buf;
stat("/etc/hosts", &buf);
printf("/etc/hosts file size = %d\n", buf.st_size);
}
/*************************************************************************/
struct stat {
dev_t st_dev; // device number file
ino_t st_ino; // node
mode_t st_mode; // file types and access permissions
nlink_t st_nlink; // number of connections connected to the hard file, the file is a newly created
uid_t st_uid; // User ID
gid_t st_gid;//组ID
dev_t st_rdev; // (device type) if this file is a device file for the device number
off_t st_size; // number of bytes of the file (file size)
unsigned long st_blksize; // block size (file system I / O buffer size)
unsigned long st_blocks;//块数
time_t st_atime; // last visited
time_t st_mtime; // Last modified time
time_t st_ctime; // last change time (of property)
};
st_mode the previously described several cases defines the following:
S_IFMT 0170000 file type bit mask
S_IFSOCK 0140000 scoket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 general files
Block means S_IFBLK 0060000
S_IFDIR 0040000 Catalog
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 04000 file (setuser-idonexecution) Bit
S_ISGID 02000 file (setgroup-idonexecution) Bit
sticky bit S_ISVTX 01000 files
S_IRUSR (S_IREAD) 00400 file owner has permission to read
S_IWUSR (S_IWRITE) 00200 file owner can write with authority
S_IXUSR (S_IEXEC) 00100 owner of the file has executable permission
S_IRGRP 00040 can groups of users with read permission
S_IWGRP 00020 can groups of users with write permission
S_IXGRP 00010 User Group with executable permissions
S_IROTH 00004 other users with read permission can
S_IWOTH 00002 other user with write permissions
S_IXOTH 00001 other users with executable permissions
The document type definition of the above check these types of macros are defined in the POSIX:
Whether S_ISLNK (st_mode) determines that the symbolic link
S_ISREG (st_mode) whether general files
S_ISDIR (st_mode) whether the directory
S_ISCHR (st_mode) whether a character device file
S_ISBLK (s3e) whether the FIFO
S_ISSOCK (st_mode) whether the socket
If a directory with the sticky bit (S_ISVTX), to indicate a file in this directory can only be the owner of the file, or the root directory owner to delete or rename.
struct statfs {
longf_type; // file system type longf_bsize; // block size longf_blocks; // block number longf_bfree; // block longf_bavail idle; // available block longf_files; // total file node longf_ffree; // file node idle fsid_t f_fsid; / / file system idlongf_namelen; // maximum length of the file name longf_spare [6]; // spare for later};
stat, fstat and lstat function (UNIX)
#include#include
intstat(constchar*restrict pathname,structstat *restrict buf);
stat provides file name, file corresponds to acquire property. The general feeling is no file open when doing so.
intfstat(intfiledes,structstat *buf);
fstat get the file attributes corresponding to the file descriptor. When the file open such operations
intlstat(constchar*restrict pathname,structstat *restrict buf);
lstat connection file
Three function Returns: 0 if successful, compared, or -1 if the error.
Given a pathname, stat function returns a structure of information related to the file with this name, fstat function to obtain information on the open file descriptor filedes. STAT lstat function is similar, but when the named file is a symbolic link, lstat returns information about the connection symbols, the symbol instead of the connection information file referenced.
The second parameter is a pointer to a structure that we should provide. These functions are completed by the structure pointed to by buf. The actual definition of the structure may vary from one implementation vary, but the basic form is:
struct stat{
mode_t st_mode; /*file tpye &mode (permissions)*/ ino_t st_ino; /*i=node number (serial number)*/ dev_t st_rdev; /*device number for special files*/ nlink_t st_nlink; /*number of links*/ uid_t st_uid; /*user id of owner*/ gid_t st_gid; /*group ID of owner*/ off_t st_size; /*size in bytes for regular files*/ time_t st_atime; /*time of last access*/ time_t st_mtime; /*time of last modification*/ time_t st_ctime; /*time of last file status change*/longst_blksize;/*best I/O block size */longst_blocks;/*number of 512-byte blocks allocated*/};
Note that, other than the last two, all the other members of the system are the basic data types. We'll explain each of the members of this structure in order to understand the file attributes.
Use stat function most likely ls-l command, you can get all the information about a file with its.
1) functions are acquired attribute file (normal file, directory, pipe, socket, characters, blocks).
Prototype:
#include
intstat(constchar*restrict pathname,structstat *restrict buf);
Provide the name of the file, get the file corresponding property.
intfstat(intfiledes,structstat *buf);
Get the file attributes corresponding file descriptors.
intlstat(constchar*restrict pathname,structstat *restrict buf);
Connection command file description, acquiring file attribute.
2) corresponding to the file attributes
struct stat {
mode_t st_mode; // file corresponding to the model, files, directories, etc.
ino_t st_ino; // inode 节点 No.
dev_t st_dev; // device number
dev_t st_rdev; // number of special equipment
nlink_t st_nlink; the number of connections // file
uid_t st_uid; // file owner
gid_t st_gid; // file corresponding group owner
off_t st_size; // common file, the file corresponding to the number of bytes
time_t st_atime; // file was last accessed time
time_t st_mtime; // contents of the file was last modified time
time_t st_ctime; // file status change time
blksize_t st_blksize; // file corresponding to the content block size
blkcnt_t st_blocks; // number of blocks corresponding to the content files};
Function can be provided above, it returns a structure that holds information about the file.