Linux文件相关函数-文件夹信息

文件信息相关

stat 查看文件信息

获得文件详细信息

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *buf);



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 */	// 用户id
    gid_t     st_gid;         /* group ID of owner */	// 组id
    dev_t     st_rdev;        /* device ID (if special file) */	// 特殊文件时有用
    off_t     st_size;        /* total size, in bytes */	// 文件大小
    blksize_t st_blksize;     /* blocksize for filesystem I/O */	// 块大小
    blkcnt_t  st_blocks;      /* number of 512B 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 */	// 上一次状态变化时间
    
    #define st_atime st_atim.tv_sec      /* Backward compatibility */
    #define st_mtime st_mtim.tv_sec
    #define st_ctime st_ctim.tv_sec
}

struct timespec {
    __kernel_time_t tv_sec; /* seconds */	  // 当前时间到1970年1月1日0:0:0的秒数
    long        tv_nsec;    /* nanoseconds */ // 纳秒数
};
// 1s = 1000ms(毫秒)
// 1ms = 1000us(微秒)
// 1us = 1000ns(纳秒)

参数

pathname 文件名

struct stat *buf 传出参数,定义 struct stat sb; &sb

返回值

成功返回0 失败返回-1 设置errno.

getpwuid 查看用户信息

获取用信息

#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 */
};

getgrgid 查看组信息

查看组信息

#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;        /* group members */
};

localtime 查看时间

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
struct tm {
	int tm_sec;         /* seconds */
	int tm_min;         /* minutes */
	int tm_hour;        /* hours */
	int tm_mday;        /* day of the month */
	int tm_mon;         /* month */
	int tm_year;        /* year */
	int tm_wday;        /* day of the week */
	int tm_yday;        /* day in the year */
	int tm_isdst;       /* daylight saving time */
};

stat/lstat区别

stat遇到链接,会追溯到源文件, 穿透!!

lstat并不会穿透

access查看文件权限

#include <unistd.h>

int access(const char *pathname, int mode);

mode

  • R_OK
  • W_OK
  • X_OK
  • F_OK

返回值

  • 如果有权限或者文件存在 对应返回0
  • 失败返回-1 设置erron

chmod 设置权限

#include <sys/stat.h>

int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

truncate 截断文件

#include <unistd.h>
#include <sys/types.h>

int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

link系列

link 给文件制造一个新名 (硬链接)

#include <unistd.h>

int link(const char *oldpath, const char *newpath);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

symlink 制造软连接

#include <unistd.h>

int symlink(const char *oldpath, const char *newpath);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

readlink 读取软连接

#include <unistd.h>

ssize_t readlink(const char *path, char *buf, size_t bufsiz);

返回值

  • 成功返回读到的软连接的长度
  • 失败返回-1 设置erron

unlink 删除硬链接计数

#include <unistd.h>

int unlink(const char *pathname);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

改变文件属性

chown 改变用户和组

#include <unistd.h>

int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

rename 改变文件名称

#include <stdio.h>

int rename(const char *oldpath, const char *newpath);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

文件目录相关

getcwd 获得当前工作路径

#include <unistd.h>

char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
char *get_current_dir_name(void);

参数

  • buf 传出参数,路径
  • size 缓冲大小

返回值

  • 成功返回 路径的指针
  • 失败返回NULL

chdir 改进程工作目录

#include <unistd.h>

int chdir(const char *path);
int fchdir(int fd);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

mkdir 创建目录

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

参数

  • mode 0777

返回值

  • 成功返回0
  • 失败返回-1 设置erron

rmdir 删除目录 --只能为空

#include <unistd.h>

int rmdir(const char *pathname);

返回值

  • 成功返回0
  • 失败返回-1 设置erron

opendir 打开目录

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

返回值

  • 成功返回目录流指针
  • 失败返回NULL

readdir 读目录

#include <dirent.h>

struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

struct dirent {
	ino_t          d_ino;       /* inode number */
	off_t          d_off;       /* not an offset; see NOTES */
	unsigned short d_reclen;    /* length of this record */
	unsigned char  d_type;      /* type of file; not supported by all file system types */
	char           d_name[256]; /* filename */
};

返回值

  • NULL代表读到文件末尾或发生错误
  • 读到目录项的内容

closedir 关闭文件目录

#include <sys/types.h>

#include <dirent.h>

int closedir(DIR *dirp);

实例 统计文件夹下普通文件的个数

命令:find ./ -type f | wc -l

#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>


int DirCount(char* dirname);

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        printf("error");
        return 0;
    }
    int count = DirCount(argv[1]);
    printf("The count of dir is %d\n",count);

    return 0;
}

int DirCount(char* dirname)
{
    printf("%s\n",dirname);
    int count = 0;
    DIR *dirp = opendir(dirname);
    if(dirp == NULL)
    {
        perror("opendir error");
        return -1;
    }
    struct dirent *dentp = NULL;
    while((dentp = readdir(dirp))!=NULL)
    {
        if(dentp->d_type == DT_DIR)
        {
            if(strcmp(".",dentp->d_name) == 0||strcmp("..",dentp->d_name) ==0)
            { 
                continue;   
            }
            char newdirname[256] = {0};
            sprintf(newdirname,"%s/%s",dirname,dentp->d_name);
            count += DirCount(newdirname);;
        }

        if(dentp->d_type == DT_REG)
        {
            count++;
            printf("\t %s\n",dentp->d_name);
        }
    }
    closedir(dirp);
    return count;
}

errno 错误信息

全局变量 错误信息

dup与dup2

#include <unistd.h>

int dup(int oldfd);
int dup2(int oldfd, int newfd);

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

int dup3(int oldfd, int newfd, int flags);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

去留意

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值