Query file information in linux

Source: Internet
Author: User
Tags bit set lstat

The stat System Call series include fstat, stat, and lstat, which are used to return "Related File status information". The difference between the three is that the source file setting method is different.


First, I would like to introduce a very important "VIP" character. It is a struct type used by fstat, stat, and lstat. It is called struct stat. It can be said that without the support of this struct stat, the above three system calls will be unable to proceed.

The definition of this struct stat struct in different UNIX/Linux systems is slightly different, but you don't have to worry about it, which won't affect our use.

In the struct stat struct, we commonly use and each platform must have the following domains:

St_mode File Permission and file type information (remember this)

Inode associated with the file

Device Where st_dev saves files

UID of the owner of the st_uid File

GID Number of the owner of the st_gid File

Last time st_atime file was accessed

The last modification time of the permissions, owner, group, or content of the st_ctime File

The last modification time of the content of the st_mtime file. (The difference with st_ctime is obvious)

St_nlink: Number of hard connections on the file

I extracted the original definition of the struct stat struct of solaris (UNIX) and fedora (Linux) respectively: You can compare them to find that they are indeed different, however, the main domains are identical.

Struct stat definition of solaris:

Struct stat {
Dev_t st_dev;
Ino_t st_ino;
Mode_t st_mode;
Nlink_t st_nlink;
Uid_t st_uid;
Gid_t st_gid;
Dev_t st_rdev;
Off_t st_size;
Timestruc_t st_atim;
Timestruc_t st_mtim;
Timestruc_t st_ctim;
Blksize_t st_blksize;
Blkcnt_t st_blocks;
Char st_fstype [_ ST_FSTYPSZ];

Definition of struct stat of fedora:

Struct stat
_ Dev_t st_dev;/* Device .*/
Unsigned short int _ pad1;
_ Ino_t st_ino;/* File serial number .*/
_ Mode_t st_mode;/* File mode .*/
_ Nlink_t st_nlink;/* Link count .*/
_ Uid_t st_uid;/* User ID of the file's owner .*/
_ Gid_t st_gid;/* Group ID of the file's group .*/
_ Dev_t st_rdev;/* Device number, if device .*/
Unsigned short int _ pad2;
_ Off_t st_size;/* Size of file, in bytes .*/
_ Blksize_t st_blksize;/* Optimal block size for I/O .*/
_ Blkcnt_t st_blocks;/* Number 512-byte blocks allocated .*/
Struct timespec st_atim;/* Time of last access .*/
Struct timespec st_mtim;/* Time of last modification .*/
Struct timespec st_ctim;/* Time of last status change .*/
Unsigned long int _ unused4;
Unsigned long int _ unused5;


You must have noticed that when listing the fields above, I used st_mode.Simhei orangeThe reason is that this domain is not as easy to use as other domains, and the values of other domains are obvious, while the st_mode domain requires some macros for use. In fact, in general, these macros are some external numbers of binary numbers with a specific position of 1. We use them and st_mode to perform the "&" operation to get some specific information.

The file type flag includes:

S_IFBLK: a file is a special block device.

S_IFDIR: The file is a directory

S_IFCHR: a file is a special character device.

S_IFIFO: The file is a FIFO device.

S_IFREG: The file is a common file (REG is regular)

S_IFLNK: The file is a symbolic link.

Other mode labels include:

S_ISUID: SUID bit set in the file

S_ISGID: the SGID bit is set in the file.

S_ISVTX: the sticky bit is set in the file.

The mask used to explain the st_mode flag includes:

S_IFMT: file type

S_IRWXU: read, write, and execute permissions of the owner, which can be divided into S_IXUSR, S_IRUSR, S_IWUSR

S_IRWXG: read/write/execute permissions of the group, which can be divided into S_IXGRP, S_IRGRP, S_IWGRP

S_IRWXO: read/write/execute permissions of other users, which can be divided into S_IXOTH, S_IROTH, S_IWOTH

There are also some macro definitions used to help determine the file type, which are different from the above macros. These are macros with parameters, similar to the usage of functions:

S_ISBLK: test whether it is a special block Device File

S_ISCHR: test whether it is a special character Device File

S_ISDIR: test whether it is a directory (I guess this macro is used in the source code implementation of find.-type d)

S_ISFIFO: test whether the device is a FIFO device.

S_ISREG: test whether it is a common file

S_ISLNK: test whether it is a symbolic link

S_ISSOCK: test whether it is a socket


We have learned about struct stat and various st_mode macros, and now we can work with them with stat System calls!

Int fstat (int filedes, struct stat * buf );

Int stat (const char * path, struct stat * buf );

Int lstat (const char * path, struct stat * buf );

Smart people can see at a glance that the first parameter of fstat is different from the other two, right! The difference between fstat and the other two systems is that fstat system calls accept a "file descriptor", while the other two directly accept "full file path ". The file descriptor can be obtained only after being called by the open system, and the file path can be directly written.

The difference between stat and lstat: When a file is a symbolic link, lstat returns the information of the symbolic link itself, while stat returns the information of the file to which the link points. (It seems a bit dizzy. In this case, lstat has an l more than stat, so it has the ability to process symbolic link files. Therefore, when a symbolic link file is encountered, of course, lstat will not be used. The stat system does not have this capability. It can only close one eye of the symbolic link file and directly process the file indicated by the link)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.