美文网首页程序员
linux 文件属性

linux 文件属性

作者: 陈伟志 | 来源:发表于2016-06-28 18:17 被阅读0次

关于属性的结构

在linux下文件和文件夹都被认为是文件, 所以以下的这个属性对文件和文件夹通用
获取属性的函数有stat/fstat/lstat/fstat

struct    stat{
mode_t    st_mode; //文件类型和读写权限
ino_t    st_ino; 
dev_t    st_dev;
dev_t    st_rdev;
nlink_t    st_nlink;
uid_t    st_uid; //文件拥有者的ID
gid_t    st_gid; //文件拥所在的用户组
offt_t    st_size; //文件大小
struct timespec    st_atime;
struct timespec    st_mtime;
struct timespec    st_ctime;
blksize_t    st_blksize;
blkcnt_t    st_blocks;
}

struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
    printf("lstat error");
    exit(1);
}

文件类型

linux下文件分为以下几种:
1.普通文件(regular file), 判断函数为S_ISREG();
2.目录文件(directory file), 判断函数为S_ISDIR();
3.块特殊文件(block special file), S_ISBLK();
3.字符特殊文件(character special file), S_ISCHR();
5.进程通信管道文件(FIFO), S_ISFIFO();
6.套接字(socket), S_ISSOCK();
7.符号链接(symbolic link), S_ISLNK();

struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
    printf("lstat error");
    exit(1);
}

if(S_ISREG(buf.st_mode))
    printf("this is a regular file");
else if(S_ISDIR(buf.st_mode))
    printf("this is a directory");
else if(..)
...
else
    printf("unknown file type");

测试访问权限

文件有读/写/执行三种权限, 文件的拥有者对文件可能有读/写/执行的权限, 同组的可能有读/执行的权限, 而不同组的可能连读的权限都没有
所以在我们对已存在的文件进行读写操作时, 可以先进行访问权限判断
int access(const char *pathname, int mode), mode有三个供选择参数:R_OK, W_OK, X_OK, 分别是读/写/执行

char *pathname="./test.txt";
if(access(pathname,R_OK) < 0){
    perror("access error");
    exit(1);
}else
    printf("read access");

int fd;
if((fd=open(pathname,O_RDONLY))<0){
    printf("open error");
    exit(1);
}else
    printf("open for reading");

更改访问权限

更改文件的访问权限条件需至少需满足一项:1.超级用户进程进行更改; 2.文件拥有者的进程进行更改
文件权限的设置分成三部分, 分别作用于:
1.文件的拥有者,S_IRUSR,S_IWUSR,S_IXUSR,三合一的写法为S_IRWXU
2.同组的用户,S_IRGRP,S_IWGRP,S_IXGRP,三合一的写法为S_IRWXG
3.其它组的用户,S_IROTH,S_IWOTH,S_IXOTH,三合一的写法为S_IRWXO

以上权限依次为读, 写, 执行, 三合一的包含三个权限

更改的函数有chmod/fchmod/fchmodat

struct stat buf;
char *pathname="./test.txt";
if(stat(pathname,&buf)<0){
    printf("stat error");
    exit(1);
}

//只关掉S_IXGRP
if(chmod(pathname,buf.st_mode & ~S_IXGRP)<0)
    printf("chmod error");

//不管当前的权限, 以绝对的方式设置
if(chmod(pathname,S_IRUSR|S_IWUSR|S_IRGRP)<0)
    printf("chmod error");

更改文件拥有者

该操作在大多数linux系统中需要root来执行
chown/fchown/fchownat/lchown
int chown (const char *pathname, uid_t owner, gid_t group);

//假设当前系统有个普通用户组的用户,ID和组ID都为1000
//以root身份运行, sudo root
char *pathname="./test.txt";
if(chown(pathname,1000,1000)<0)
    printf("chown failed");

文件大小

struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname,&buf)<0){
    printf("lstat error");
    exit(1);
}
printf("file size: %lu",buf.st_size);

另外可以用truncate(pathname,0)将文件大小设置成0,也就是擦除文件内容

其它

创建文件: int creat(const char *path, mode_t mode) mode为访问权限,用open也可以创建文件
文件重命名: int rename(const char *oldname, const char *newname)
删除文件: int remove(const char *pathname)

相关文章

  • 文件和目录权限

    1、Linux权限介绍2、Linux文件属性3、如何改变Linux文件属性权限4、新建文件默认权限 1、Linux...

  • linux 文件属性与目录管理

    linux 文件属性与目录管理 linux 文件属性 linux 文件基本属性 当为[ d ]则是目录 当为[ -...

  • Linux 文件权限

    Linux chmod命令详解linux 文件属性与权限

  • linux操作常用命令:文件目录查询修改等操作

    linux 命令: 显示文件属性介绍:文件属性在文件系统的安全管理方面起很重要的作用,linux下lsattr命令...

  • day10预习笔记

    Linux文件类型及查找命令实践 Linux文件属性概述 文件属性:文件的大小时间 类型 权限 属主 索引节点:文...

  • linux 查漏补缺 2

    Linux 下软件安装相关 2.1 文件属性和可执行属性 2.1.1 文件属性 文件属性 rwx 中 r 表示 r...

  • Linux文件权限管理

    Linux文件权限管理 linux文件属性 改变权限 查看用户组

  • 软件测试知识学习路线

    Linux基础◇ Linux系统的了解与环境的熟悉◇ 常用的Linux命令◇ Linux文件属性、权限、用户...

  • LINUX文件权限

    系统环境:CentOS Linux release 7.3.1611 x86_64 GNU/Linux 文件属性 ...

  • Linux文件属性

    Linux教程:http://www.fdlly.com/m/linux这篇文章主要介绍了Linux文件属性,结合...

网友评论

    本文标题:linux 文件属性

    本文链接:https://www.haomeiwen.com/subject/nwmtjttx.html