与时间有关的shell命令:
date // 显示当前日期-- 中国北京时间 date -u //显示当前日期时间 -- 世界标准时间 UTC date -R // 显示当前日期时间 – RFC格式 运行: $ time // 显示程序运行的时间 time ./a.out 可执行程序a.out 显示: real 0m10.081s 程序开始运行到结束的时间 user 0m0.000s 用户CPU时间 sys 0m0.004s 系统CPU时间 说明 用户cpu时间:就是执行用户指令所用的时间。 系统CPU时间: 就是该进程执行内核程序所经历的时间 $ cal // 显示日历 指令: cal month year 显示指定年月的日历: cal 4 20001.时间编程:
GUN/Linux 提供的时间获取API
数据类型定义及结构体描述
typedef long time_t; struct tm { int tm_sec; /* 秒:取值区间为[0,59] */ int tm_min; /* 分:取值区间为[0,59] */ int tm_hour; /* 时:取值区间为[0,23] */ int tm_mday; /* 一个月中的日期:取值区间为[1,31] */ int tm_mon; /* 月份(从一月开始, 0 代表一月) :取值区间为[0,11] */ int tm_year; /* 年份:其值等于实际年份加上 1900*/ int tm_wday; /* 星期:取值区间为[0,6],其中 0 代表星期天,1 代表星期一,以此类推 */ int tm_yday; /* 从每年的 1 月 1 日开始的天数:取值区间为[0,365],其中 0 代表 1 月 1 日,1 代表 1 月 2 日,以此类推*/ int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst 为正,不实 行夏令时的进候, tm_isdst 为 0;不了解情况时, tm_isdst()为负*/ };函数:
获取日历时间: time 函数原型 time_t time(time_t *t); 功能 返回日历时间 所属头文件 <time.h> 参数 time_t类型的指针变量,或者填充NULL 返回值 成功返回日历时间,失败返回-1
–
获取格林威治时间 gmtime 原型 struct tm *gmtime(const time_t *timep); 功能 将参数timep所指定的日历时间转换为标准时间 所属头文件 <time.h> 参数 timep待转化的日历时间 返回值 成功返回世界标准时间,以struct tm形式存储
代码演示:获取格林威治时间:
#include <stdio.h> #include <time.h> int main() { time_t mytime; struct tm *stm; mytime = time(NULL); stm = gmtime(&mytime); printf("%d年 %d月 %d日 %d时 %d分 %d秒\n",1900+stm->tm_year,1+stm->tm_mon,stm->tm_mday,stm->tm_hour,stm->tm_min,stm->tm_sec); return 0; }运行结果:
获取本地时间 localtime 原型 struct tm *localtime(const time_t *timep); 功能 将timep指向的日历时间转换为本地时间 所属头文件 <time.h> 参数 timep:指向待转化日历时间 返回值 返回以struct tm形式存储的本地时间,失败返回NULL
代码演示:获取本地时间:
#include <stdio.h> #include <time.h> int main() { time_t mytime; struct tm *stm; mytime = time(NULL); stm = localtime(&mytime); printf("%d年 %d月 %d日 %d时 %d分 %d秒\n",1900+stm->tm_year,1+stm->tm_mon,stm->tm_mday,stm->tm_hour,stm->tm_min,stm->tm_sec); return 0; }运行结果:
字符串形式显示时间 asctime 原型 char *asctime(const struct tm *tm); 功能 将struct tm格式的时间转化为字符串 所属头文件 <time.h> 参数 带转化的tm格式的时间 返回值 字符串显示的时间
代码演示:字符串形式显示时间:
#include <stdio.h> #include <time.h> int main() { time_t mytime; char *ch = NULL; struct tm *stm; mytime = time(NULL); stm = localtime(&mytime); printf("%d年 %d月 %d日 %d时 %d分 %d秒\n",1900+stm->tm_year,1+stm->tm_mon,stm->tm_mday,stm->tm_hour,stm->tm_min,stm->tm_sec); ch = asctime(stm); printf("时间:%s\n",ch); return 0; }运行结果:
日历时间转本地时间 ctime 原型 char *ctime(const time_t *timep); 功能 将日历时间转化为本地时间 所属头文件 #include <time.h> 参数 待转化为日历时间 返回值 返回一字符串表示目前当地的时间日期。
代码演示:日历时间转本地时间:
#include <stdio.h> #include <time.h> int main() { time_t mytime; char *ch = NULL; struct tm *stm; mytime = time(NULL); ch = ctime(&mytime); printf("时间:%s\n",ch); return 0; }运行结果:
2.文件属性:
大小 文件类型 创建时间 路径等等。。。
属性描述:
struct stat { dev_t st_dev; /*如果是设备,返回文件使用的设备号,否则为 0*/ ino_t st_ino; /* 索引节点号 */ mode_t st_mode; /* 文件类型 */ nlink_t st_nlink; /* 文件的硬连接数 */ uid_t st_uid; /* 所有者用户识别号*/ gid_t st_gid; /* 组识别号 */ dev_t st_rdev; /* 设备类型*/ off_t st_size; /* 文件大小,字节表示 */ blksize_t st_blksize; /* 系统每次按块Io操作时块的大小(一般是512或1024)*/ blkcnt_t st_blocks; /*块的索引号 */ time_t st_atime; /* 最后访问时间,如read*/ time_t st_mtime; /* 最后修改时间*/ time_t st_ctime; /* 创建时间 */ };获取属性:
stat 功能 提供文件名字,获取文件对应属性。 函数原型 int stat(const char *path,struct stat *buf)(第一个参数为传入的文件,第二个参数为属性)(文件不打开执行stat) 所属头文件 <sys/types.h>、<sys/stat.h>、<unistd.h> 参数 path:文件路径 buf:返回文件的文件信息 返回值 成功返回0,失败返回-1
参考代码:获取文件索引节点,创建时间的属性:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { struct stat buf; int ret; ret = stat(argv[1],&buf); if(ret < 0) { perror("stat"); return -1; } printf("索引节点%ld\n",buf.st_ino); printf("创建时间:%s\n",ctime(&buf.st_ctime)); return 0; }运行结果:
fstat 功能: 通过文件描述符获取文件对应的属性。 函数原型 int fstat(int fds,struct stat *buf)(第一个参数为传入的文件描述符,第二个参数为属性)(文件打开后才能执行fstat) 所属头文件 <sys/types.h>、<sys/stat.h>、<unistd.h> 参数 fds:文件描述符 buf:返回文件的信息, 返回值 成功返回0,失败返回-1
代码演示:通过文件描述符获取文件对应的属性:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <fcntl.h> int main(int argc,char *argv[]) { struct stat buf; int fd; int ret; fd = open(argv[1],O_RDWR); if(fd < 0) { perror("open"); return -1; } ret = fstat(fd,&buf); if(ret < 0) { perror("stat"); return -1; } printf("索引节点%ld\n",buf.st_ino); printf("创建时间:%s\n",ctime(&buf.st_ctime)); close(fd); return 0; }运行结果:
lstat 功能 连接文件描述名,获取文件属性。 函数原型 int lstat(const char *path,struct stat *buf) 所属头文件 <sys/types.h>、<sys/stat.h>、<unistd.h> 参数 path:文件路径 buf:返回文件的文件信息,针对符号链接,返回链接本身,而不是非目标文件 返回值 成功为0 失败为-1
代码演示:连接文件描述名,获取文件属性:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { struct stat buf; int ret; ret = lstat(argv[1],&buf); if(ret < 0) { perror("stat"); return -1; } printf("索引节点%ld\n",buf.st_ino); printf("创建时间:%s\n",ctime(&buf.st_ctime)); return 0; }运行结果:
注意: stat和lstat的作用完全相同都是取得参数file_name 所指的文件状态, 其差别在于, 当文件为符号连接时, lstat()会返回该链接本身的状态,而不是非目标文件(也就是返回的是连接文件的信息)。 stat 和 fstat的差别 一个传递带路径的文件名或者目录名 传递文件的描述符
相关指令 ls -ail 2408949 -rwxr-xr-x 1 root root 70 04-21 12:47 lsfile.sh 索引节点 文件种类和权限 硬链接个数 拥有者 所归属的组 文件或目录的大小 最后访问或修改时间 文件名或目录名 文件种类 当为[ d ]则是目录 当为[ - ]则是文件; 若是[ l ]则表示为链接文档(link file); 若是[ b ]则表示为装置文件里面的可供储存的接口设备(可随机存取装置); 若是[ c ]则表示为装置文件里面的串行端口设备,例如键盘、鼠标(一次性读取装置) 硬件连接数 1就是它本身 大于1就是有快捷方式 ln -s 创建 关联 ls命令实际上就是调用stat等系统调用的函数读取文件属性并显示出来判断文件类型:
代码演示:判断文件的类型:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { struct stat buf; int ret; ret = lstat(argv[1],&buf); if(ret < 0) { perror("stat"); return -1; } printf("索引节点%ld\n",buf.st_ino); printf("创建时间:%s\n",ctime(&buf.st_ctime)); if(S_ISLNK(buf.st_mode)) printf("该文件是 l\n");//连接文件 if(S_ISREG(buf.st_mode)) printf("该文件是 -\n");//普通文件 if(S_ISDIR(buf.st_mode)) printf("该文件是 d\n");//目录 if(S_ISCHR(buf.st_mode)) printf("该文件是 c\n");//字符设备 if(S_ISBLK(buf.st_mode)) printf("该文件是 b\n");//块设备 if(S_ISFIFO(buf.st_mode)) printf("该文件是 p\n");//命名管道 if(S_ISSOCK(buf.st_mode)) printf("该文件是 s\n");//套接字 return 0; }运行结果: 表示该文件为普通文件。
权限的核查:
access 功能: 可检测当前用户(运行这个程序的用户)对某文件是否有某权限 函数原型: int access(const char *pathname, int mode); 所属头文件: <unistd.h> 参数: pathname:文件或者是目录路径 mode R_OK:测试读权限 W_OK:测试写权限 X_OK:测试执行权限 F_OK:测试文件是否存在 返回值: 若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1 注意:access如果是在普通用户下,只检测当前用的权限,如果要是在root用下,那么他检测的是所有用户的权限
代码演示:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { if(argc<2) { printf("请输入正确的参数\n"); return -1; } if(access(argv[1],F_OK)) { printf("该文件不存在\n"); return -1; } else{ if(!(access(argv[1],W_OK))) printf("该文件存在可写权限\n"); if(!(access(argv[1],R_OK))) printf("该文件存在可读权限\n"); if(!(access(argv[1],X_OK))) printf("该文件存在可执行权限\n"); } return 0; }运行结果: 应用:
获取文件大小 获取文件创建时间 如何判断文件的属性 如何获取文件权限 编写ls xx -l命令的实现
3.目录操作:
基本函数:
mkdir
功能 创建目录 函数原型 int mkdir(const char *pathname, mode_t mode); 所属头文件 <sys/stat.h> <sys/types.h> 参数 pathname:文件路径 mode:直接使用数字即可(权限) 返回值 成功返回0,错误返回-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { if(argc<2) { printf("请输入正确的参数\n"); return -1; } if((mkdir(argv[1],0644)) < 0) { printf("创建目录失败\n"); return -1; } printf("创建目录成功\n"); return 0; }运行结果:
rmdir == rm
功能 删除目录 函数原型 int rmdir(const char *pathname); 所属头文件 <unistd.h> 参数 要删除目录路径,你也可以直接写argv[1] 返回值 成功返回0,错误返回-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc,char *argv[]) { if(argc<2) { printf("请输入正确的参数\n"); return -1; } if((mkdir(argv[1],0644)) < 0) { printf("创建目录失败\n"); return -1; } printf("创建目录成功\n"); rmdir(argv[1]); return 0; }运行结果: getcwd == pwd
功能 获取当前目录 函数原型 char *getcwd(char *buf, size_t size); char *get_current_dir_name(void); //char *getwd(char *buf);(最后一个不常用) 所属头文件 <unistd.h> 参数 buf:保存当前目录缓存区 size:buf最大为255字节 返回值 成功返回指向当前目录的指针,和部分值一样,错误返回NULL
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #define MAX 255 int main(int argc,char *argv[]) { char *ch = NULL; char buf[MAX]; if(argc<2) { printf("请输入正确的参数\n"); return -1; } if((mkdir(argv[1],0644)) < 0) { printf("创建目录失败\n"); return -1; } printf("创建目录成功\n"); ch = getcwd(buf,sizeof(buf)); if(ch == NULL) { printf("获取文件路径失败\n"); return -1; } printf("buf:%s\n",buf); return 0; }运行结果: chdir == cd
功能 修改当前目录,即切换目录,相当于 cd 命令 函数原型 int chdir(const char *path); 所属头文件 <unistd.h> 参数 path:文件路径 返回值 成功返回0,失败返回-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #define MAX 255 int main(int argc,char *argv[]) { char *ch = NULL; char buf[MAX]; ch = getcwd(buf,sizeof(buf)); if(ch == NULL) { printf("获取文件路径失败\n"); return -1; } printf("buf:%s\n",buf); chdir("/home/dazai/000"); ch = getcwd(buf,sizeof(buf)); if(ch == NULL) { printf("获取文件路径失败\n"); return -1; } printf("buf:%s\n",buf); return 0; }运行结果: chmod
功能 更改权限 原型 int chmod(const char *path, mode_t mode); 头文件 #include <sys/types.h> #include <sys/stat.h> 参数 path :文件路径 mode 权限 返回值 成功返回0失败-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #define MAX 255 int main(int argc,char *argv[]) { char *ch = NULL; char buf[MAX]; ch = getcwd(buf,sizeof(buf)); if(ch == NULL) { printf("获取文件路径失败\n"); return -1; } printf("buf:%s\n",buf); if((chmod(argv[1],0666)) < 0) { printf("更改权限失败\n"); return -1; } printf("更改权限成功\n"); return 0; }
运行结果: 读取目录:
struct dirent结构体 存储目录中的文件信息(文件名、扩展名等等)
#include <dirent.h> struct dirent { long d_ino; /* inode number 索引节点号 */ off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ unsigned short d_reclen; /* length of this d_name 文件名长 */ unsigned char d_type; /* the type of d_name 文件类型 */ char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */ }opendir
功能 打开目录 函数原型 DIR *opendir(const char *name); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 目录的路径 返回值 成功返回指向当前目录的指针,错误返回NULL
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #include <dirent.h> #define MAX 255 int main(int argc,char *argv[]) { DIR *dir = NULL; dir = opendir(argv[1]); if(dir == NULL) { printf("打开目录失败\n"); return -1; } printf("打开目录成功\n"); return 0; }运行结果: closedir
功能 关闭目录 函数原型 int closedir(DIR *dir); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 opendir返回的指针 返回值 成功返回0,错误返回-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #include <dirent.h> #define MAX 255 int main(int argc,char *argv[]) { DIR *dir = NULL; dir = opendir(argv[1]); if(dir == NULL) { printf("打开目录失败\n"); return -1; } printf("打开目录成功\n"); closedir(dir); return 0; }运行结果: readdir
功能 读取目录信息 函数原型 struct dirent *readdir(DIR *dir); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 打开目录后返回的文件指针 返回值 成功返回指向dirp的指针dirent,错误返回NULL
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #include <dirent.h> #define MAX 255 int main(int argc,char *argv[]) { DIR *dir = NULL; struct dirent *ren; dir = opendir(argv[1]); if(dir == NULL) { printf("打开目录失败\n"); return -1; } printf("打开目录成功\n"); while((ren = readdir(dir)) != NULL) { printf("文件名:%s\n",ren->d_name); } closedir(dir); return 0; }运行结果: rewinddir
功能 重新定位到目录文件的头部 函数原型 void rewinddir(DIR *dir); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 打开目录后返回的文件指针
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #include <dirent.h> #define MAX 255 int main(int argc,char *argv[]) { DIR *dir = NULL; struct dirent *ren; dir = opendir(argv[1]); if(dir == NULL) { printf("打开目录失败\n"); return -1; } printf("打开目录成功\n"); while((ren = readdir(dir)) != NULL) { printf("文件名:%s\n",ren->d_name); } printf("===============================\n"); rewinddir(dir); while((ren = readdir(dir)) != NULL) { printf("文件名:%s\n",ren->d_name); } closedir(dir); return 0; }运行结果: seekdir
功能 设置参数dir 目录流目前的读取位置, 在调用readdir()时便从此新位置开始读取. 参数offset 代表距离目录文件开头的偏移量。 函数原型 void seekdir(DIR *dir,off_t offset); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 打开目录后返回的文件指针 offset :代表距离目录文件开头的 偏移量 返回值 无 注意:seekdir的偏移位置的变量是long int
telldir
功能 取得目录流的读取位置 函数原型 off_t telldir(DIR *dir); 所属头文件 #include <sys/types.h> #include <dirent.h> 参数 打开目录后返回的文件指针 返回值 成功返回距离目录文件开头的偏移量返回值返回下个读取位置, 有错误发生时返回-1
演示代码:
#include <stdio.h> #include <time.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include <stdlib.h> #include <dirent.h> #define MAX 255 int main(int argc,char *argv[]) { DIR *dir = NULL; struct dirent *ren; int i = 0; off_t offset = 0, offset_2 = 0; dir = opendir(argv[1]); if(dir == NULL) { printf("打开目录失败\n"); return -1; } printf("打开目录成功\n"); while((ren = readdir(dir)) != NULL) { offset = telldir(dir); if(++i == 2) offset_2 = offset; printf("文件名:%s\n",ren->d_name); } printf("===============================\n"); seekdir(dir,offset_2); while((ren = readdir(dir)) != NULL) { printf("文件名:%s\n",ren->d_name); } closedir(dir); return 0; }运行结果:
参考结构流程图: 时间编程&文件属性&目录操作 注:需要使用xmind软件进行查看