一、获取文件的元数据(meta data)
通过read write可以对文件的内容进行读写。
但是今天我们要操作的是文件的元数据(文件的属性信息)
day08$ls -l hello
-rw-rw-r-- tarena tarena 8月 : hello
文件的类型
文件的权限
文件的硬链接数
属主
属组
文件的大小
时间 文件的链接
分为两种 硬链接和软链接 每个文件有且仅有一个自己的inode。
硬链接的两个文件有同一个inode。
如何为一个文件创建硬链接?
ln 源文件 链接文件 如何创建软链接文件?
ln -s 源文件 链接文件 如何使用程序获取文件的元数据?
使用stat()获取文件的元数据
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
功能:获取文件信息
参数:
path:指定了文件的名字。
buf:用来存储这个文件的元数据。
返回值:
成功
- 错误 errno被设置 struct stat 这个结构体中有哪些成员,需要了解
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection*/
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; /*blocksize for file system I/O */
blkcnt_t st_blocks; /*number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
举例说明 获取文件的元数据 代码参见 pstat.c 时间的问题。系统记录的是从1970年1月1日0::0开始的一个秒数。需要将这个长整型的数转换为字符串格式。
ctime()
#include <time.h>
char *ctime(const time_t *timep);
功能:将时间的整数值转换为字符串格式
参数:
timep:指定要转换的长整型的数。
返回值:
NULL 错误
非空 转换后的字符串的首地址 关于用户的问题,用户看到的是文件的名字,而系统管理中使用的是用户的id,数字。
在程序中输出的是数字,但是需要转为字符串格式,方便用户的使用。那如何根据用户的id,找到用户的名字? 使用到函数getpwuid()
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
功能:过去passwd文件中的一条记录
参数:
uid:指定用户的uid 返回值:
指向匹配uid的记录的首地址
NULL 没有找到匹配的记录或者错误的产生。
如果是错误产生 设置errno
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 */
}; 在linux系统中用户的信息存放在/etc/passwd文件中
root:x:::root:/root:/bin/bash
tarena:x:::tarena,,,:/home/tarena:/bin/bash
以:分隔的七列
第一列:用户的名字
第二列:是否有密码? x有密码
第三列:用户的id。 uid
第四列:用户的组id。 gid
第五列:用户的注释信息
第六列:用户的工作主目录
第七列:用户登陆成功启动的程序 需要将gid转换为组名
getgrgid()
#include <sys/types.h>
#include <grp.h> struct group *getgrgid(gid_t gid);
功能:获取组文件的一条记录
参数:
gid:指定用户组的id
返回值:
NULL 没有找到匹配的记录或者错误产生 如果错误产生errno被设置 返回一个地址 struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* group members */
}; 文件权限的处理
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_IRWXU mask for file owner permissions
S_IRUSR owner has read permission
S_IWUSR owner has write permission
S_IXUSR owner has execute permission st_mode& S_IRWXU 得到的是文件拥有者对文件的权限。
,,,
,,,
,,,
,,, 作业:编写代码实现ls -l filename的功能。
编译生成可执行文件pls。 二、文件夹的操作
linux操作系统下一切皆文件。文件夹也是文件的一种。
drwxrwxr-x tarena tarena 8月 : dir
对文件夹读写操作。
什么是文件夹的内容?
文件夹的内容,就是文件夹里的文件或文件夹。 rwx
r 读
w 写
x 通过
使用程序来访问文件夹的内容。
opendir() closedir() readdir()
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
功能:打开一个文件夹
参数:
name:指定了要打开的文件夹的名字
返回值:
NULL 错误 errno被设置
返回一个指针,指向文件夹流的指针。 DIR FILE closedir()
#include <sys/types.h>
#include <dirent.h> int closedir(DIR *dirp);
功能:关闭文件夹
参数:
dirp:opendir()的返回值
返回值:
- 错误 errno被设置
成功 readdir()
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
功能:从文件夹读取数据
参数:
dirp:opendir()的返回值。
返回值:
返回一个地址
如果到达了文件的末尾,NULL被返回,errno不变
如果错误产生,NULL被返回,errno被设置 struct dirent{
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
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[]; /* filename */
}; 举例说明 编写代码实现浏览指定文件夹下的所有文件
代码参见 dir_op.c position 作业的补充:
如果filename是普通文件,将普通文件的元数据输出。
如果filename是文件夹,将文件夹里的所有的文件的元数据输出 三、文件操作的杂项 getcwd()
access()
chdir() mkdir()
umask()
rmdir()
unlink()
link()
symlink()
rename()
remove()
chmod()
到此 ,文件和文件系统结束了。
进程管理 总结:
一、获取文件的元数据
链接文件
用户 属组
二、文件夹的操作
权限 文件夹的内容
三、文件操作的杂项

文件元数据、文件夹操作(day08)的更多相关文章

  1. Python对文件和文件夹的高级操作模块shutil

    shutil模块提供了许多关于文件和文件夹的高级操作. 特别提供了支持文件复制和删除的功能. # 将文件对象fsrc的内容复制到文件类对象fdst.length(可选参数)是缓冲区大小 shutil. ...

  2. [No000083]文件与文件夹操作

    #region Folder option 文件夹操作 /// <summary> /// 指定目录是否存在 /// </summary> /// <param name ...

  3. PHP 文件夹操作「复制、删除、查看大小」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...

  4. C# 文件和文件夹操作

    一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...

  5. Windows Store App 用户库文件夹操作

    上面介绍了与用户库文件有关的操作,包括创建.读写等,下面将介绍与用户库文件夹相关的操作. 与文件操作一样,想要对用户库文件夹进行操作,需要首先获取用户库的相应位置,获取的方法上面已经介绍过了,这里不再 ...

  6. Winform 基础知识 之文件夹操作

    using System.IO; /// <summary> /// 删除文件夹下所有文件 /// </summary> /// <param name="di ...

  7. .Net文件*夹*操作

    一.文件夹操作 Directory类,DirectoryInfo类.使用using System.IO命名空间 (一)创建文件夹 方法一: private string path = @"F ...

  8. Python操作文件、文件夹、字符串

    Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...

  9. 【CITE】C#目录、文件、文件夹操作

    1.   在一个目录下创建一个文件夹 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); ...

随机推荐

  1. cocos2d-x 3.0游戏实例学习笔记 《跑酷》第七步--物理碰撞检測(1)

    说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...

  2. DOCKER_HOST have a weird tcp

    [piqiu@benjaminpro ~]$boot2docker start Waiting for VM and Docker daemon to start... ............... ...

  3. linux更改gitlab存储位置

    更改仓库存储位置默认时GitLab的仓库存储位置在“/var/opt/gitlab/git-data/repositories”,在实际生产环境中显然我们不会存储在这个位置,一般都会划分一个独立的分区 ...

  4. web 前端学习笔记

    <!DOCTYPE HTML> <head> <style type="text/css"> body {  background: #ff00 ...

  5. hdu3652B-number(数位dp)

    B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. [Apple开发者帐户帮助]四、管理密钥(1)创建私钥以访问服务

    私钥允许您访问和验证与某些应用服务(如APN,MusicKit和DeviceCheck)的通信.您将在对该服务的请求中使用JSON Web令牌(JWT)中的私钥. 所需角色:帐户持有人或管理员. 在“ ...

  7. LocalDateTime查找最近的五分钟点

    /** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...

  8. Integer应该用==还是equals

    问题引出:“Integer应该用==还是equals” 讨论这个问题之前我们先放一段代码 public static void main(String[] args) { Integer a1 = 2 ...

  9. Wireshark 、HTTPWatch、Fiddler的介绍

    一.Wireshark  <TCP/IP协议监听> Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包 ...

  10. MySQL实现递归查询

    DROP FUNCTION IF EXISTS queryChildrenCaseInfo;CREATE FUNCTION queryChildrenCaseInfo(cId INT)RETURNS ...