特黄特色三级在线观看免费,看黄色片子免费,色综合久,欧美在线视频看看,高潮胡言乱语对白刺激国产,伊人网成人,中文字幕亚洲一碰就硬老熟妇

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 >

linux 文件和目錄操作的相關(guān)函數(shù)使用教程

時間: 加城1195 分享

  Linux繼承了Unix以網(wǎng)絡(luò)為核心的設(shè)計思想,是一個性能穩(wěn)定的多用戶網(wǎng)絡(luò)操作系統(tǒng)。其中最常用操作之一就是對文件和目錄操作了,以下是對linux中文件和目錄操作的相關(guān)函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下。

  方法步驟

  struct stat

  {

  mode_t st_mode; 文件類型,文件權(quán)限

  ino_t st_ino; i節(jié)點號

  dev_t st_dev;

  dev_t st_rdev; 設(shè)備文件序號

  nlink_t st_nlink; 鏈接

  uid_t st_uid;

  gid_t st_gid; 用戶ID

  off_t st_size; 文件大小,此字段只對普通文件、目錄文件和符號連接有意義。

  time_t st_atime; 最后存取時間

  time_t st_mtime; 文件內(nèi)容的最后修改時間

  time_t st_ctime; 文件狀態(tài)的最后修改時間

  long st_blksize;

  long st_blocks;

  };

  1,stat函數(shù)取得文件信息。

  #include

  #include

  int stat(const char *pathname, struct stat *buf);

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

  int lstat(const char *pathname, struct stat *buf);

  lstat函數(shù)類似于stat,但是當(dāng)命名的文件是一個符號連接時,lstat返回該符號連接的有關(guān)信息,而不是由該符號連接引用的文件的信息

  2,access函數(shù)判斷文件權(quán)限

  #include

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

  返回:若成功則為 0,若出錯則為- 1

  access函數(shù)的mode常數(shù),取自

  mode 說 明

  R_OK 測試讀許可權(quán)

  W_OK 測試寫許可權(quán)

  X_OK 測試執(zhí)行許可權(quán)

  F_OK 測試文件是否存在

  3,umask函數(shù)設(shè)置文件創(chuàng)建屏蔽字

  #include

  #include

  mode_t umask(mode_t task) ;

  返回:以前的文件方式創(chuàng)建屏蔽字

  4,chmod函數(shù)用于修改文件的權(quán)限

  #include

  #include

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

  int fchmod(int fd, mode_t mode);

  兩個函數(shù)返回:若成功則為 0,若出錯則為- 1

  5,chown函數(shù)可用于更改文件的用戶 ID和組ID。

  #include

  #include

  int chown(const char *pathname,uid_t owner,gid_t group);

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

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

  三個函數(shù)返回:若成功則為 0,若出錯則為- 1

  6,在文件末尾處截短文件可以調(diào)用函數(shù) truncate和ftruncate。將一個文件的長度截短為 0是一個特例,用O_TRUNC標(biāo)志可以做到這一點。

  #include

  #include

  int truncate(const char *pathname, off_t

  length) ;

  int ftruncate(int filedes, off_t length) ;

  兩個函數(shù)返回;若成功則為 0,若出錯則為- 1

  7,創(chuàng)建一個向現(xiàn)存文件連接的方法是使用link函數(shù),想當(dāng)于硬連接 ln。只有超級用戶進(jìn)程可以創(chuàng)建指向一個目錄的新連接。其理由是這樣做可能在文件系統(tǒng)中形成循環(huán),大多數(shù)處理文件系統(tǒng)的公用程序都不能處理這種情況

  #include

  int link(const char*oldpath, const char *newpath) ;

  返回:若成功則為 0,若出錯則為- 1

  為了刪除一個現(xiàn)存的目錄項,可以調(diào)用 unlink函數(shù)。

  #include

  int unlink(const char *pathname) ;

  返回:若成功則為 0,若出錯則為-1。此函數(shù)刪除目錄項,并將由 pathname所引用的文件的連接計數(shù)減 1。

  硬連接的一些限制: ( a )硬連接通常要求連接和文件位于同一文件系統(tǒng)中, ( b )只有超級用戶才能創(chuàng)建到目錄的硬連接。

  symlink函數(shù)創(chuàng)建一個符號連接。相當(dāng)于軟連接,ln -s

  #include

  int symlink(const char *oldpath, const char *sympath) ;

  返回:若成功則為 0,若出錯則為- 1

  因為open函數(shù)跟隨符號連接,所以需要有一種方法打開該連接本身,并讀該連接中的名字。

  readlink函數(shù)提供了這種功能。

  #include

  int readlink(const char *pathname, char *buf, int bufsize) ;

  返回:若成功則為讀的字節(jié)數(shù),若出錯則為- 1

  此函數(shù)組合了open, read和close的所有操作。

  8,用mkdir函數(shù)創(chuàng)建目錄,用 rmdir函數(shù)刪除目錄。

  #include

  #include

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

  返回:若成功則為 0,若出錯則為- 1

  #include

  int rmdir(const char *pathname) ;

  返回:若成功則為 0,若出錯則為 - 1

  9,remove函數(shù)解除對一個文件或目錄的連接。對于文件, remove的功能與unlink相同。對于目錄, remove的功能與rmdir相同。

  #include

  int remove(const char *pathname) ;

  返回:若成功則為 0,若出錯則為- 1

  文件或目錄用rename函數(shù)更名。

  #include

  int rename(const char *oldname, const char *newwname) ;

  返回:若成功則為 0,若出錯則為- 1

  10,一個文件的存取和修改時間可以用 utime函數(shù)更改。

  #include

  #include

  int utime (const char *name, const struct utimebuf *t);

  返回:若成功則為 0,若出錯則為- 1

  如果times是一個空指針,則存取時間和修改時間兩者都設(shè)置為當(dāng)前時間;

  如果times是非空指針,則存取時間和修改時間被設(shè)置為 times所指向的結(jié)構(gòu)中的值。此時,進(jìn)程的有效用戶ID必須等于該文件的所有者 ID,或者進(jìn)程必須是一個超級用戶進(jìn)程。對文件只具有寫許可權(quán)是不夠的

  此函數(shù)所使用的結(jié)構(gòu)是:

  struct utimbuf {

  time_t actime; /*access time*/

  time_t modtime; /*modification time*/

  }

  11,對文件目錄的操作函數(shù),opendir readdir rewinddir

  #include

  #include

  DIR *opendir(const char *pathname) ;

  返回:若成功則為指針,若出錯則為 NULL

  struct dirent *readdir(DIR *dr);

  返回:若成功則為指針,若在目錄尾或出錯則為 NULL

  void rewinddir(DIR *dr);

  重置讀取目錄的位置為開頭

  int close(DIR *dr); 返回:若成功則為 0,若出錯則為- 1

  定義在頭文件中的dirent結(jié)構(gòu)與實現(xiàn)有關(guān)。 此結(jié)構(gòu)至少包含下列兩個成員:

  struct dirent {

  ino_t d_ino;

  char d_name[NAME_MAX+1];

  }

  12,chdir,改變當(dāng)前目錄

  #include

  int chdir(const char *pathname);

  int pchdir(int fd);

  getcwd,得到當(dāng)前目錄的完整路徑.

  #include

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

  若失敗返回NULL, buf為存儲路徑的字符數(shù)組,size為長度

  補充:Linux基本命令

 ?、賚s 意為list 列出當(dāng)前文件夾中的文件

  -l 顯示文件的屬性 可用ll來表示

 ?、赼lias 別名 看看是否有別名的文件

 ?、踓d dir 跳躍目錄 -P選項 將路徑中的鏈接文件替換成鏈接指向的文件路徑

  ④pwd 查看當(dāng)前工作的文件夾名 使用-P的選項,會直接進(jìn)入到其中,相當(dāng)于cd

  相關(guān)閱讀:Linuxshell腳本不執(zhí)行問題實例分析

  shell腳本不執(zhí)行問題:某天研發(fā)某同事找我說幫他看看他寫的shell腳本,死活不執(zhí)行,報錯。我看了下,腳本很簡單,也沒有常規(guī)性的錯誤,報“:badinterpreter:Nosuchfileordirectory”錯??催@錯,我就問他是不是在windows下編寫的腳本,然后在上傳到linux服務(wù)器的……果然。原因:在DOS/windows里,文本文件的換行符為rn,而在*nix系統(tǒng)里則為n,所以DOS/Windows里編輯過的文本文件到了*nix里,每一行都多了個^M。解決:

  1)重新在linux下編寫腳本;

  2)vi:%s/r//g:%s/^M//g(^M輸入用Ctrl+v,Ctrl+m)附:sh-x腳本文件名,可以單步執(zhí)行并回顯結(jié)果,有助于排查復(fù)雜腳本問題。


linux 文件和目錄操作的相關(guān)函數(shù)相關(guān)文章:

1.linux下文件夾的創(chuàng)建、復(fù)制、剪切、重命名、清空和刪除命令

2.linux拷貝文件夾命令

3.linux中系統(tǒng)中根目錄文件夾中文詳解

4.關(guān)于Linux中10個你不知道的命令補齊技巧有哪些

5.linux中的man命令的詳細(xì)解釋

4016938