服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux中stat函数和stat命令使用详解

Linux中stat函数和stat命令使用详解

2020-07-22 14:00小石王 Linux

这篇文章主要介绍了Linux中stat函数和stat命令使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

stat函数和stat命令

linux文件里的【inode = index node】解释:要理解inode必须了解磁盘和【目录项】,inode实际是连接【目录项】和磁盘的中间物质。

图里的大圈代表硬件的磁盘,里面的小圈代表某个文件存储在磁盘上了。

【inode = index node】的node(承载node信息的结构体是:stat,stat的定义在后面 )里面有:

  1. 文件大小
  2. 文件的最后修改时间
  3. 文件的所属用户
  4. 文件的权限
  5. 硬链接计数(ls -l 显示出来的数字)
  6. 块位置:指定文件存储在磁盘的具体位置。

下图中的hello是个普通文件,hello.hard是hello的硬链接

文件夹里放的就是每个文件的【目录项】如下图,【目录项】里有:

  1. 文件名
  2. 该目录项的大小
  3. 文件的类型
  4. inode

Linux中stat函数和stat命令使用详解

如何查看文件的【inode】呢?使用【-i】选项

?
1
ls -li 文件名

执行结果:

ys@ys-VirtualBox:~/lianxi1$ ls -li hello hello.hard
3801352 -rw-rw-r-- 2 ys ys 0 4月  24 11:01 hello
3801352 -rw-rw-r-- 2 ys ys 0 4月  24 11:01 hello.hard

发现hello和hello.hard的inode(3801352)是相同的,也就说明了,只在磁盘上存了一份。

如何查看目录项呢?用emacs或者vim打开目录(lianxi1),截图如下。但是看不到文件的【inode】。

Linux中stat函数和stat命令使用详解

1,stat函数:取得指定文件的文件属性,文件属性存储在结构体stat里。

?
1
2
3
4
5
6
7
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
 
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

struct stat 结构体:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct stat {
    dev_t  st_dev;   /* ID of device containing file */
    ino_t  st_ino;   /* Inode number */
    mode_t st_mode;  /* File type and mode */
    nlink_t st_nlink;  /* Number of hard links */
    uid_t  st_uid;   /* User ID of owner */
    gid_t  st_gid;   /* Group ID of owner */
    dev_t  st_rdev;  /* Device ID (if special file) */
    off_t  st_size;  /* Total size, in bytes */
    blksize_t st_blksize;  /* Block size for filesystem I/O */
    blkcnt_t st_blocks;  /* Number of 512B blocks allocated */
 
    /* Since Linux 2.6, the kernel supports nanosecond
     precision for the following timestamp fields.
     For the details before Linux 2.6, see NOTES. */
 
    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 */
 
   #define st_atime st_atim.tv_sec  /* Backward compatibility */
   #define st_mtime st_mtim.tv_sec
   #define st_ctime st_ctim.tv_sec
   };

st_dev:设备ID,不太常用
st_ino:【inode】,【inode】是啥?不知道就看上面关于【inode】的解释
st_mode:文件的类型和权限,共16位,如下图。

  • 0-11位控制文件的权限
  • 12-15位控制文件的类型

0-2比特位:其他用户权限
3-5比特位:组用户权限
6-8比特位:本用户权限
9-11比特位:特殊权限
12-15比特位:文件类型(因为文件类型只有7中,所以用12-14位就够了

Linux中stat函数和stat命令使用详解

文件类型的宏如下(下面的数字是8进制):

  • S_IFSOCK 0140000 socket
  • S_IFLNK 0120000 symbolic link(软连接)
  • S_IFREG 0100000 regular file(普通文件)
  • S_IFBLK 0060000 block device(块设备文件)
  • S_IFDIR 0040000 directory(目录)
  • S_IFCHR 0020000 character device(字符设备文件)
  • S_IFIFO 0010000 FIFO(管道)
?
1
2
3
4
5
6
7
8
判断文件类型的函数,返回truefalse
 S_ISREG(stat.st_mode) is it a regular file?
 S_ISDIR(stat.st_mode) directory?
 S_ISCHR(stat.st_mode) character device?
 S_ISBLK(stat.st_mode) block device?
 S_ISFIFO(m) FIFO (named pipe)?
 S_ISLNK(stat.st_mode) symbolic link? (Not in POSIX.1-1996.)
 S_ISSOCK(stat.st_mode) socket? (Not in POSIX.1-1996.)

文件权限的宏如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
S_ISUID  04000 set-user-ID bit
 S_ISGID  02000 set-group-ID bit (see below)
 S_ISVTX  01000 sticky bit (see below)
 
 S_IRWXU  00700 owner has read, write, and execute permission
 S_IRUSR  00400 owner has read permission
 S_IWUSR  00200 owner has write permission
 S_IXUSR  00100 owner has execute permission
 
 S_IRWXG  00070 group has read, write, and execute permission
 S_IRGRP  00040 group has read permission
 S_IWGRP  00020 group has write permission
 S_IXGRP  00010 group has execute permission
 
 S_IRWXO  00007 others (not in group) have read, write, and
      execute permission
 S_IROTH  00004 others have read permission
 S_IWOTH  00002 others have write permission
 S_IXOTH  00001 others have execute permission
  • st_nlink:硬连接计数
  • st_uid:这个文件所属用户的ID
  • st_gid:这个文件所属用户的组ID
  • st_rdev:特殊设备的ID,不太常用
  • st_size:文件的大小
  • st_blksize:不明是干啥的
  • st_blocks:不明是干啥的
  • struct timespec st_atim:最后访问的时间
  • struct timespec st_mtim:最后修改的时间
  • struct timespec st_ctim:最后状态改变的时间
?
1
2
3
4
5
6
7
struct timespec {
    __kernel_time_t tv_sec;  /* seconds */当前时间到1970.1.1 00:00:00的秒数
    long        tv_nsec;    /* nanoseconds *//纳秒数(不知道从哪到哪的)
};
1s 秒  = 1000ms 毫秒
1ms 毫秒 = 1000us 微秒
1us 微秒 = 1000ns 纳秒

pathname:文件名

返回值:0代表成功;-1代表失败,并设置error

例子:statbuf是结构体stat,可以看出来st_mode是个10进制的数字。

Linux中stat函数和stat命令使用详解

st_mode

用gdb显示st_mode,发现返回的st_mode是个10进制的数字,用gdb的【p/o】(o代表用8进制表示)命令把10进制的33204转换成了8进制的【0100664】,第一个0代笔是8进制,后三位的【100】代表文件类型,从上面的说明可以看出来【100】代表普通文件,最后三位的【664】代表这个文件的权限(本用户:rw-,组用户:rw-,其他用户:r--)。所以从st_mode里就可以得知文件的类型和权限设置(只使用了16个比特位,真的好节省空间,牛逼!)

st_uid

st_gid

发现st_uid和st_gid是1000,但这个1000怎么和用户对应上呢,查看/etc/passwd文件,发现用于ys的uid和gid都是1000,所以就对应上了。

Linux中stat函数和stat命令使用详解

stat命令,是stat函数对应,执行结果如下:

?
1
2
3
4
5
6
7
8
ys@ys-VirtualBox:~/lianxi1$ stat hello
 File: hello
 Size: 11   Blocks: 8   IO Block: 4096 regular file
Device: 801h/2049d  Inode: 3801352  Links: 2
Access: (0764/-rwxrw-r--) Uid: ( 1000/  ys) Gid: ( 1000/  ys)
Access: 2019-04-24 17:02:39.199461489 +0800
Modify: 2019-04-24 16:54:16.407461489 +0800
Change: 2019-04-24 17:03:44.927461489 +0800

2,getpwuid函数:返回/etc/passwd文件里指定uid的行,把这一行的信息放入结构体passwd中。虽然返回值是指针,但不需要调用free函数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
 
struct passwd {
 char *pw_name;  /* username */
 char *pw_passwd;  /* user password */
 uid_t pw_uid;  /* user ID */
 gid_t pw_gid;  /* group ID */
 char *pw_gecos;  /* user information */
 char *pw_dir;  /* home directory */
 char *pw_shell;  /* shell program */
};

3,getgrgid函数:返回/etc/group文件里指定gid的行,把这一行的信息放入结构体group中。虽然返回值是指针,但不需要调用free函数。

?
1
2
3
4
5
6
7
8
9
10
11
12
#include <sys/types.h>
#include <grp.h>
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);
 
struct group {
 char *gr_name;  /* group name */
 char *gr_passwd;  /* group password */
 gid_t gr_gid;   /* group ID */
 char **gr_mem;   /* NULL-terminated array of pointers
        to names of group members */
};

4,localtime函数:传入从stat函数里得到的st_mtim.tv_sec(当前时间到1970.1.1 00:00:00的秒数),得到结构体tm。虽然返回值是指针,但不需要调用free函数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm {
 int tm_sec; /* Seconds (0-60) */
 int tm_min; /* Minutes (0-59) */
 int tm_hour; /* Hours (0-23) */
 int tm_mday; /* Day of the month (1-31) */
 int tm_mon; /* Month (0-11) */
 int tm_year; /* Year - 1900 */
 int tm_wday; /* Day of the week (0-6, Sunday = 0) */
 int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
 int tm_isdst; /* Daylight saving time */
};

5,lstat函数:stat碰到软链接,会追述到源文件,穿透;lstat并不会穿透。

例子:模仿ls -l 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>//getpwuid
#include <stdlib.h>
#include <time.h>//localtime
#include <grp.h>//getgrgid
 
int main(int argc, char* argv[]){
 
 struct stat sbuf;
 //stat(argv[1], &sbuf);
 lstat(argv[1], &sbuf);
 
 char str[11] = {0};
 memset(str, '-', (sizeof str - 1));
 
 //文件类型
 if(S_ISREG(sbuf.st_mode)) str[0] = '-';
 if(S_ISDIR(sbuf.st_mode)) str[0] = 'd';
 if(S_ISCHR(sbuf.st_mode)) str[0] = 'c';
 if(S_ISBLK(sbuf.st_mode)) str[0] = 'b';
 if(S_ISFIFO(sbuf.st_mode)) str[0] = 'p';
 if(S_ISLNK(sbuf.st_mode)) str[0] = 'l';
 if(S_ISSOCK(sbuf.st_mode)) str[0] = 's';
 
 //本用户的文件权限
 if(sbuf.st_mode & S_IRUSR) str[1] = 'r';
 if(sbuf.st_mode & S_IWUSR) str[2] = 'w';
 if(sbuf.st_mode & S_IXUSR) str[3] = 'x';
 
 //本用户的组的文件权限
 if(sbuf.st_mode & S_IRGRP) str[4] = 'r';
 if(sbuf.st_mode & S_IWGRP) str[5] = 'w';
 if(sbuf.st_mode & S_IXGRP) str[6] = 'x';
 
 //其他用户的文件权限
 if(sbuf.st_mode & S_IROTH) str[7] = 'r';
 if(sbuf.st_mode & S_IWOTH) str[8] = 'w';
 if(sbuf.st_mode & S_IXOTH) str[9] = 'x';
 
 char ymd[20] = {0};
 //取得日期和时间
 struct tm* tm = localtime(&sbuf.st_atim.tv_sec);
 sprintf(ymd, "%2d月 %2d %02d:%02d", tm->tm_mon + 1, tm->tm_mday,
     tm->tm_hour + 1,tm->tm_sec);
 
 //-rw-r--r-- 1 ys ys 134 4月 25 09:21 st2.c
 printf("%s %ld %s %s %ld %s %s\n", str, sbuf.st_nlink,
     getpwuid(sbuf.st_uid)->pw_name, getgrgid(sbuf.st_gid)->gr_name,
     sbuf.st_size, ymd, argv[1]);
 return 0;
}

6,access函数:判断调用程序的用户对于指定文件的权限(可读?可写?可执行?)

?
1
2
#include <unistd.h>
int access(const char *pathname, int mode);

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <unistd.h>//access
 
int main(int argc, char* argv[]){
 if(access(argv[1], R_OK) == 0)
 printf("read ok\n");
 if(access(argv[1], W_OK) == 0)
 printf("write ok\n");
 if(access(argv[1], X_OK) == 0)
 printf("exe ok\n");
 if(access(argv[1], F_OK) == 0)
 printf("exists\n");
}

先用ls -l 查看/usr/include/time.h文件的权限,结果如下

?
1
2
ys@ys-VirtualBox:~/lianxi$ ls -l /usr/include/time.h
-rw-r--r-- 1 root root 10360 4月 17 2018 /usr/include/time.h

用ys用户执行例子程序,查看/usr/include/time.h文件,结果如下。因为time.h是属于root用户的,对于其他用户来说是[r--],所以得出下面的结果。

?
1
2
3
ys@ys-VirtualBox:~/lianxi$ ./ac /usr/include/time.h
read ok
exists

还是用ys用户执行,但是加上sudo,结果如下。发现结果和root用户相同。因为加了sudo,就编程了root用户。

?
1
2
3
4
5
ys@ys-VirtualBox:~/lianxi$ sudo ./ac /usr/include/time.h
[sudo] password for ys:
read ok
write ok
exists

7,truncate函数:截断文件和扩展文件的大小

?
1
2
3
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);

path:文件
length:
length大于原来文件的大小,则扩展文件的大小至length
length小于原来文件的大小,则截断文件的大小至length

8,link函数:创建硬链接

?
1
2
#include <unistd.h>
int link(const char *oldpath, const char *newpath);

返回值:成功返回0,失败返回-1,并设置errno。

9,symlink函数:创建软链接

?
1
2
#include <unistd.h>
int symlink(const char *target, const char *linkpath);

返回值:成功返回0,失败返回-1,并设置errno。

10,readlink函数:找到软链接对应的实际文件,把文件的名字放入buf里。注意:硬链接不行。

?
1
2
#include <unistd.h>
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

返回值:成功返回写入buf的字节数,失败返回-1,并设置errno。

11,unlink函数:删除软硬链接,也可以删除文件。

?
1
2
#include <unistd.h>
int unlink(const char *pathname);

返回值:成功返回0,失败返回-1,并设置errno。

有个特殊用法:下面的open代码想要创建hello文件,然后直接用unlink删除,但是能写入成功,ret是大于0的,程序执行完,发现没有做成hello文件。

结论:当执行unlink后,计数为0后,但,发现别的进程还引用这个文件,这个时间点,unlink不会删除这个文件,等这个进程结束后,再删除,所以下面的write代码能够写入成功。
利用这个特点可以实现:在线观看视频时,实际是把视频文件下载到了本地(然后代码里,使用unlink),看完后视频文件的计数为0,就自动删除了,不怕视频被泄露出去。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(){
 int fd = open("hello", O_WRONLY | O_CREAT, 0666);
 unlink("hello");
 int ret = write(fd, "aaa", 4);
 if(ret > 0){
 printf("write OK\n");
 }
 
}

12,chown函数:改变文件的所属用户和组

?
1
2
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);

pathname:文件

owner:用户ID(数字的)/etc/passwd

group:组ID(数字的)/etc/group

返回值:0成功,-1失败。

13,rename函数:重命名

?
1
2
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);

oldpath :原来的文件名后者目录

newpath:新的文件名后者目录

返回值:0成功,-1失败。

14,getcwd函数:获得当前工作的目录

?
1
2
#include <unistd.h>
char *getcwd(char *buf, size_t size);

buf:当前工作的目录

size:缓冲区大小

返回值:成功返回当前工作的目录 失败返回NULL

15,chdir函数:改变进程的工作目录

?
1
2
#include <unistd.h>
int chdir(const char *path);

path:目标工作目录

返回值:0成功,-1失败

16,mkdir函数:创建目录

?
1
2
3
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

pathname:目标工作目录mode:mode & ~umask & 0777 。注意,如果没有x权限,则无法cd进入这个目录。返回值:0成功,-1失败

17,rmdir函数:删除目录,目录必须是空目录,也就是里面没有任何文件。

?
1
2
#include <unistd.h>
int rmdir(const char *pathname);

18,opendir函数:打开目录

?
1
2
3
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

name:目录名

返回值:a pointer to the directory stream

19,readdir函数:读目录

?
1
2
3
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

dirp:opendir函数的返回值

返回值:结构体dirent,可以理解成最上面说的【目录项】NULL代表读到末尾或者有错误  NULL以外代表目录项的内容

20,closedir函数:关闭目录

?
1
2
3
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);

dirp:opendir函数的返回值

21,strerron函数:打印出errno对应的文字信息。

?
1
2
#include <string.h>
char *strerror(int errnum);

errnum的宏放在文件:/usr/include/asm-generic/errno.h

例子:

?
1
2
3
4
5
6
7
#include <string.h>
#include <stdio.h>
#include <asm-generic/errno.h>//EDEADLK
int main(){
 char* buf = strerror(EDEADLK);
 printf("%s\n", buf);//Resource deadlock avoided
}

22,dup和dup2函数:文件描述符的重定向

?
1
2
3
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);

dup:和open类似,先打开一个新的文件描述符,让新的文件描述符也指向:oldfd指向的地方。成功返回新打开的文件描述符;失败返回-1.

dup2:先消除newfd的指向再让newfd指向oldfd指向的地方成功返回newfd;失败返回-1.

Linux中stat函数和stat命令使用详解

例子:调用printf2次,第一次printf把内容写到文件;第二次printf把内容打印到屏幕。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(){
 
 int oldfd = dup(STDOUT_FILENO);
 int fd = open("www", O_WRONLY | O_CREAT, 0666);
 dup2(fd, STDOUT_FILENO);
 printf("aaaa\n");
 fflush(stdout);
 int ret = dup2(oldfd, STDOUT_FILENO);
 //int ret = dup2(oldfd, 6);
 //perror("dup2:");
 printf("reg:%d\n", ret);
 printf("aaaa\n");
 close(fd);
}

到此这篇关于Linux中stat函数和stat命令使用详解的文章就介绍到这了,更多相关Linux stat函数和stat命令内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/xiaoshiwang/p/10764243.html

延伸 · 阅读

精彩推荐
  • Linuxlinux中rmdir命令使用详解(删除空目录)

    linux中rmdir命令使用详解(删除空目录)

    今天学习一下linux中命令: rmdir命令。rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的 ...

    linux命令大全5372019-11-19
  • LinuxLinux lnmp下无法使用mail发邮件的两种解决方法

    Linux lnmp下无法使用mail发邮件的两种解决方法

    在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix。下面为大家一一介绍下 ...

    Linux之家4042019-09-17
  • LinuxLinux上设置用户通过SFTP访问目录的权限的方法

    Linux上设置用户通过SFTP访问目录的权限的方法

    这篇文章主要介绍了Linux上设置用户通过SFTP访问目录的权限的方法,SFTP可以理解为使用SSH协议进行FTP传输的协议,因而同时要对OpenSSH进行相关设置,需要的朋...

    OSChina10022019-06-19
  • LinuxLinux常用的日志文件和常用命令

    Linux常用的日志文件和常用命令

    成功地管理任何系统的关键之一,是要知道系统中正在发生什么事。 Linux 中提供了异常日志,并且日志的细节是可配置的。Linux 日志都以明文形式存储,所...

    Linux教程网2632020-04-18
  • Linux详解Linux系统下PXE服务器的部署过程

    详解Linux系统下PXE服务器的部署过程

    这篇文章主要介绍了Linux系统下PXE服务器的部署过程,包括对PXE的API架构作了一个基本的简介,需要的朋友可以参考下...

    运维之道9812019-07-04
  • Linux手把手教您在 Linux 上使用 GPG 加解密文件

    手把手教您在 Linux 上使用 GPG 加解密文件

    在本教程中,我将告诉你如何用 GPG 加密和解密文件。这是一个简单的教程,你可以在你的 Linux 系统上尝试所有的练习。这将帮助你练习 GPG 命令,并在你...

    Linux中国6962021-12-15
  • Linux确保Linux系统安全的前提条件 漏洞防护

    确保Linux系统安全的前提条件 漏洞防护

    Linux 作为开放式的操作系统受到很多程序员的喜爱,很多高级程序员都喜欢编写Linux操作系统的相关软件。这使得Linux操作系统有着丰富的软件支持,还有无...

    Linux之家2642020-04-11
  • LinuxLinux中环境变量配置的步骤详解

    Linux中环境变量配置的步骤详解

    Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的...

    Myths7882022-02-10