與時間有關的shell命令,時間編程,檔案屬性和目錄操作
與時間有關的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 2000
1.時間編程:
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軟體進行查看
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/165687.html
標籤:python
上一篇:基于模型(MBD)的樹莓派程式開發——使用Simulink控制樹莓派點亮led燈
下一篇:mdk5軟體和stm32包的安裝以及stm32程式“LED閃爍”除錯,用Proteus仿真一個51程式設計和仿真
