linux 文件属性
关于属性的结构
在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)
linux 文件属性的更多相关文章
- linux文件属性详细说明
1. Linux 文件属性概说: Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容: [root@localhost ...
- linux文件属性权限相关
一个linux目录或者文件,都会有一个所属主和所属组. 所属主,即文件的拥有者,而所属组,即该文件所属主所在的一个组. linux文件属性 包括文件类型 - d l b c s 依次表示 普通文件 ...
- Linux学习之CentOS(四)----Linux文件属性、所有者、群组、其他组及文件权限操作简要总结
Linux文件属性.所有者.群组.其他组及文件权限操作简要总结 首先介绍一个重要的知识点:文件属性控制权限 [root@www ~]# ls -al total 156 drwxr-x--- 4 ro ...
- Linux文件属性及权限
一.Linux文件属性: 例如: drwxr-xr-x 2 hdy hdy 4096 11月 28 00:18 桌面 drwxr-xr-x 2 hdy hdy 4096 11月 28 00:18 桌面 ...
- Linux 文件属性及权限_007
Linux一切皆文件: Llinux系统的文件或目录的属性主要包括:索引节点.文件类型.文件权限.链接数.所属的用户和用户组.最近修改时间等. Llinux文件属性及权限图形说明: Linux文件属性 ...
- Linux文件属性,类型,ls -lhi解释行列
Linux文件属性(描述信息) -i inode节点号 -h 人类可读 ls -lhi 1703938 drwxr-xr-x 2 rsync rsync 4.0K Jun 7 07:24 gamese ...
- Linux 文件属性及修改权限
输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...
- Linux文件属性与权限
一.在Linux里面,任何一个文件都具有“User,Group,Others”(用户.用户组.其他人)三种身份 二.用户组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个用户组的 ...
- 15、Linux 文件属性和测试( chgrp,chown,chmod和-e -f -d -s
一.更改文件属性 1.chgrp:更改文件属组 语法: chgrp [-R] 属组名文件名 参数选项 -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件 ...
- Linux学习之十二-Linux文件属性
Linux文件属性 在Linux中,对于每个文件都有相应属性,以Linux中root用户家目录下新建文件a.txt为例,在a.txt中输入几个字符 使用命令ls -ild a.txt查看文件的权限等 ...
随机推荐
- codeforces 680B B. Bear and Finding Criminals(水题)
题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #includ ...
- poj 3689 树形dp
思路: 每个点有三种状态,本身有塔,被子节点的塔覆盖,被父节点的塔覆盖. #include<map> #include<set> #include<cmath> # ...
- ORA-01502错误成因和解决方法
这个错误是由于索引失效造成的,重建索引后,问题就解决了. 我们看到,当使用类似 alter table xxxxxx move tablespace xxxxxxx 命令后,索引就会失效. 当然,作为 ...
- 全新jquery多点滑动幻灯片——全屏动画animateSlide
首页banner的酷炫效果多来自全屏大图的幻灯片动画,下面提供一种完美兼容的jquery动画特效:全新jquery多点滑动幻灯片——全屏动画animateSlide(代码完全原创). 直接上代码,把h ...
- Shanghai InfoSys .NET engineer telephone interview
Collect the answers,interested friends from research. 1,Interface and Abstract difference? 2,Generic ...
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...
- 北大ACM(POJ1006-Biorhythms)
Question:http://poj.org/problem?id=1006 问题点:孙子定理 Memory: 248K Time: 0MS Language: C++ Result: Accept ...
- text-overflow:ellipsis; 使用
ul li{ height:25px; line-height:25px; width:200px; overflow:hidden; white-space:nowrap;-moz-text-ove ...
- php删除数组中相同的元素,只保留一个相同元素
<?php// 删除数组中相同元素,只保留一个相同元素function formatArray($array){sort($array);$tem = ”;$temarray = array() ...
- C# 类构造函数赋值里属性与字段赋值注意项
public class Test { public Test(int age) { this.Age=age;//如果这里使用的是this.age=age;那么属性里的判断将不会执行 } priva ...