inode
1.文件有创建者、文件的创建日期、文件的大小等信息就称作文件元信息这种储存文件元信息的区域就叫做inode,中文译名为"索引节点"。
每一个文件都有对应的inode,除了文件名以外的所有文件信息,都存在inode之中
2.inode包含文件的元信息,具体来说有以下内容:
* 文件的字节数
* 文件拥有者的User ID
* 文件的Group ID
* 文件的读、写、执行权限
* 文件的时间戳,共有三个:ctime指inode上一次变动的时间,mtime指文件内容上一次变动的时间,atime指文件上一次打开的时间。
* 链接数,即有多少文件名指向这个inode
* 文件数据block的位置
3.实际操作
#可以用stat命令,查看某个文件的inode信息:
stat 123.txt
#通过inode节点删除文件(当遇到文件名无法删除文件时)
find ./ -inum 687654| xargs rm -f
#查看每个硬盘分区的inode总数和已经使用的数量,可以使用df命令
df -i
对文件进行拷贝
例如:
建立一个文件123.txt,将其中的文件内容拷贝到1.txt文件中
echo “shenlan” >123.txt //输入文件内容为shenlan
建立一个文件file1.c写入程序如下:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
const char* srcpath = "123.txt";
int fdin;
int fdout;
char buffer[1024];
fdin = open(srcpath,O_RDONLY,S_IRUSR);
if(-1 == fdin)
{
perror("open");
return -1;
}
fdout = open("123test.txt",O_CREAT|O_RDWR,0764);
if(-1 == fdout)
{
perror("open");
return -1;
}
//记录每次读取到的字节数
int nSize = 0;
//拷贝文件
while((nSize = read(fdin,buffer,1024))>0)
{
//写文件
write(fdout,buffer,nSize);
}
//关闭文件描述符
close(fdin);
close(fdout);
printf("copy completed\n");
return 0;
}
执行命令gcc file1.c -o file1 及./file1 并查看文件