學習筆記 Linux平台的檔案,目錄及操作

2021-05-25 05:16:37 字數 2882 閱讀 3992

**

下面介紹關於檔案和目錄操作的函式

1.檔案型別

stat(),fstat()和lstat()函式

fstat功能和stat類似,只是不以檔案的路徑稱作為標識,而是用檔案描述符標識目標檔案,獲得該檔案的有關資訊。lstat形式和功能都與stat類似,但是當目標檔案是符號鏈結檔案時,lstat返回該符號連線的有關資訊。

格式:#inlcude

#include

int stat(const chat *name, struct stat *buf);

int fstat(int filedes, struct stat *buf);

int lstat(const char *file, stuct stat *buf);

與此命名檔案有關的資訊結構放在buf中。成功則函式返回0,否則返回-1。

2.檔案許可權

(1)access

用來檢查指定檔案的屬性,可以檢查檔案愛你是否可讀,可寫,可執行。

格式:#include

int access(const char *file, int mode);

成功返回0,失敗返回-1 。

(3)umask

為 程序設定建立檔案的許可掩碼。

格式:#include

#include

mode_t umask(mode_t mode);

返回以前文的檔案方式建立遮蔽字。無錯誤返回。

(4)chmod和 fchmod

改變檔案的操作許可權

格式:#include

#include

chmod[option]mode[,mode...] file;

int chmod(const char  *path, mode_t mode);

int fchmode(int fileds, mode_t mode);

(5)chown,fchown和lchown

更改檔案的使用者id和組id。

格式:#include

#include

int chown(condt char *name, uid_t owner, gid_t group);

int fchown(int filedes, uid_t owner, gid_t group);

int lchown(const char *nam, uid_t owner, gid_t group);

3.檔案操作

(1)link,unlink和 remove

link函式建立指向檔案的新目錄項。

unlink函式刪除目錄項或檔案(僅作用於非目錄檔案)。

remove函式和unlink功能相同,只是remove可以刪除目錄檔案

格式:#include

int link(const char *old, const char *new);

int unlink(const char *old);

#include

int remove(const char *file);

成功返回0,失敗返回-1 。

(2)rename

此函式可以實現對乙個檔案或目錄的更名。

格式:#include

int rename(const char *old, const char *new);

(3)symlink

建立乙個符號連線。

#include

int symlink(const char *realpath, const char *charpath);

(4)realink

開啟符號連線檔案本身以讀取連線中的檔名。

格式:#include

int reslink(const char *file, const char *buf, int bufsize);

4.檔案時間

utime和utimes

格式:#include

#inlcude

int utime(const char *filename, const struct utimebuf *time);

int utimes(char *filename, struct timeval *tvp);

5.目錄操作

(1)mkdir

建立乙個新目錄。

格式:#include

#include

int mkdir(const char *dname, mode_t mode);

(2)rmdir

刪除乙個目錄。

格式:#include

int rmdir(const char *dname);

(3)chdir和fchdir

這兩個函式的任務是搜尋相對路徑名的起點並可以更改當前工作目錄。它們的區別是chdir用路徑名來標識目標目錄,而fchdir用檔案描述符來標識目標目錄。

格式:#include

int chdir(const char *path);

int fchdir(int filedes);

(4)gecwd

獲取當前工作目錄的絕對路徑名。

格式:#include

char *getcwd(char *buf, size_t bufsize);

6.特殊裝置檔案

sync和fsync

清空檔案系統緩衝區

格式:#include

void sync(void);

int fsync(int filedes);

學習筆記 Linux平台的檔案,目錄及操作

下面介紹關於檔案和目錄操作的函式 1.檔案型別 stat fstat 和lstat 函式 fstat功能和stat類似,只是不以檔案的路徑稱作為標識,而是用檔案描述符標識目標檔案,獲得該檔案的有關資訊。lstat形式和功能都與stat類似,但是當目標檔案是符號鏈結檔案時,lstat返回該符號連線的有...

寫得蠻好的linux學習筆記 linux目錄架構

根目錄 bin 常用的命令 binary file 的目錄 boot 存放系統啟動時必須讀取的檔案,包括核心 kernel 在內 boot grub menu.lst grub設定 boot vmlinuz 核心 boot initrd 核心解壓縮所需 ram disk dev 系統周邊裝置 etc...

Linux學習筆記 檔案

檔案將使用者分為三類 user 表示檔案的所有者,只能有乙個,一般為建立的使用者,不過也可以轉交所有權 group 表示檔案的所處組,只能有乙個,定義了所處組的使用者之後,可以給組分配相應許可權 others 剩下的其他使用者即為其他人 上圖為linux檔案詳細資訊 1.10個字元,第乙個表示 二進...