3.檔案許可權管理
4.讀取目錄檔案
stat:display file or file system status樣例:
其中size是檔案大小,access是檔案最後一次被訪問(開啟,讀取,修改等)的時間,modify是檔案內容最後一次被修改的時間,change是檔案屬性最後一次被改變的時間。peco@ubuntu:~/linux_pro$ stat a.out
file: 'a.out'
size: 8912 blocks: 24 io block: 4096 regular file
device: 801h/2049d inode: 1983571 links: 1
access: (0755/-rwxr-xr-x) uid: ( 1000/ peco) gid: ( 1000/ aston)
access: 2019-04-08 15:34:43.295173788 +0800
modify: 2019-04-08 15:34:41.807173752 +0800
change: 2019-04-08 15:34:41.807173752 +0800
birth: -
stat, fstat, lstat: get file status
struct stat是核心定義的乙個結構體,在<sys/stat.h>中宣告,該結構體中的所有元素加起來就是檔案屬性資訊。#include
#include
#include
intstat
(const
char
*pathname,
struct stat *buf)
;int
fstat
(int fd,
struct stat *buf)
;int
lstat
(const
char
*pathname,
struct stat *buf)
;
struct stat:
三個api的區別: 乙個具有9個許可權位,3個一組。比如:struct stat
;
-rwxr-xr-x 1 peco aston 8912 apr 8 15:34 a.out
第一組三個(rwx)表示檔案的屬主(owner、user)對該檔案的可讀、可寫、可執行許可權;第2組3個位(r-x)表示檔案的屬主所在的組(group)對該檔案的許可權;第3組3個位(r-x)表示其他使用者(others)對該檔案的許可權。
access函式可以測試得到當前執行程式使用者在當前環境下對目標檔案是否具有某種操作許可權。
其中引數pathname 是檔案的路徑名,引數 mode指定access的作用,取值如下:access: check user's permissions for a file
#include
intaccess
(const
char
*pathname,
int mode)
;
mode
含義f_ok (0)
判斷對檔案是可執行許可權
x_ok (1)
判斷對檔案是否有寫許可權
w_ok(2)
判斷對檔案是否有寫許可權
r_ok (4)
判斷對檔案是否有讀許可權
後三種可以一起使用,如w_ok|r_ok。
access示例:
chmod是乙個用來修改檔案的各種許可權屬性的命令。chmod命令只有root使用者才有權利去執行修改,它內部是用linux的乙個叫chmod的api實現的。#include
#include
intmain
(int argc,
char
**ar**)
else
ret =
access
(file_path, r_ok);if
(ret <0)
else
ret =
access
(file_path, w_ok);if
(ret <0)
else
ret =
access
(file_path, x_ok);if
(ret <0)
else
return0;
}
opendir:chmod: change permissions of a file
#include
intchmod
(const
char
*pathname, mode_t mode)
;
readdir:#include
#include
dir *
opendir
(const
char
*name)
;
struct dirent:#include
struct dirent *
readdir
(dir *dirp)
;
struct dirent
;
linux檔案屬性
linux檔案屬性1 首先檢視一下 ls l 檢視檔案的檔案屬性 上面顯示檔案屬性一共7個常見的字段。各個欄位的含義 1.第乙個字段 檔案許可權 就是 或者r w x的組合。一共10位。左面開始數起,a 第一位 檔案型別 常規檔案 系統普通檔案。d directory 目錄檔案,目錄是特殊的檔案,目...
linux檔案屬性
linux檔案屬性3 目錄是乙個特別的檔案 目錄檔案 比如 drwxr r 就是乙個目錄的許可權 目錄具有r許可權 可以檢視目錄裡面的檔名,只是檢視檔名。是否能夠對檔案操作,那還要看檔案的許可權。目錄具有w許可權 在目錄下 增加檔案 刪除檔案 檔名重新命名 目錄具有x許可權 能夠進入該目錄 能夠使用...
linux檔案屬性
linux是多使用者多工環境,因此檔案的許可權管理十分重要。linux將檔案可訪問訪問的身份分為3種 owner group others,每種身份有三種許可權 read write execute。另乙個常識,root是萬能的,能做任何事情!這是在終端中輸入ls al命令後,列出的內容,從途中可以...