日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

UNIX再学习 -- 文件和目录

發(fā)布時(shí)間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UNIX再学习 -- 文件和目录 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文件I/O部分?jǐn)鄶嗬m(xù)續(xù)寫(xiě)了三天,最后總結(jié)發(fā)現(xiàn)還有好多內(nèi)容是略過(guò)沒(méi)講的,我的內(nèi)心是崩潰的。UNIX環(huán)境高級(jí)編程這本書(shū),雖然我只看了四章我就發(fā)現(xiàn)了書(shū)里面的內(nèi)容講的太跳,如果是剛接觸UNIX或者沒(méi)有一點(diǎn)C語(yǔ)言基礎(chǔ)的會(huì)很難上手。這就造成了,前面講的會(huì)漏掉很多內(nèi)容。

一、函數(shù) stat、fstat、fstatat 和 lstat

詳細(xì)內(nèi)容可 man fstat?查看

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h>int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf);

1、參數(shù)解析

第一個(gè)參數(shù):文件路徑/文件描述符 第二個(gè)參數(shù):結(jié)構(gòu)體指針,結(jié)構(gòu)體變量的地址。結(jié)構(gòu)體基本形式如下: struct stat {dev_t st_dev; /* ID of device containing file */ino_t st_ino; /* inode number */mode_t st_mode; /* protection */nlink_t st_nlink; /* number of hard links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */dev_t st_rdev; /* device ID (if special file) */off_t st_size; /* total size, in bytes */blksize_t st_blksize; /* blocksize for file system I/O */blkcnt_t st_blocks; /* number of 512B blocks allocated */time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */time_t st_ctime; /* time of last status change */};其中時(shí)間函數(shù)部分,參看:C語(yǔ)言再學(xué)習(xí) -- 時(shí)間函數(shù)

2、返回值

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

3、函數(shù)功能

獲取指定文件的狀態(tài)信息

4、示例說(shuō)明

#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <time.h>int main (void) {struct stat st = {};int res = stat ("a.txt", &st);if (-1 == res)perror ("Fail to stat"), exit (1);printf ("st_mode = %o, st_size = %ld, st_mtime = %ld\n", st.st_mode, st.st_size, st.st_mtime);printf ("文件的權(quán)限是:%o\n", st.st_mode&0777);printf ("文件的大小是:%ld\n", st.st_size);if (S_ISREG (st.st_mode))printf ("是普通文件\n");if (S_ISDIR (st.st_mode))printf ("是目錄文件\n");printf ("最后一次修改時(shí)間是:%s\n", ctime (&st.st_mtime));struct tm* pt = localtime (&st.st_mtime);printf("最后一次修改時(shí)間是:%d-%d-%d %02d:%02d:%02d\n",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);return 0; } 輸出結(jié)果: st_mode = 100644, st_size = 0, st_mtime = 1490857501 文件的權(quán)限是:644 文件的大小是:0 是普通文件 最后一次修改時(shí)間是:Thu Mar 30 15:05:01 2017最后一次修改時(shí)間是:2017-3-30 15:05:01

5、總結(jié)

這部分只講函數(shù) stat,其實(shí)在 Linux 下 stat 命令?也可以實(shí)現(xiàn)這些功能了。?參看:stat 命令 # stat c_test/文件:"c_test/"大小:4096 塊:8 IO 塊:4096 目錄 設(shè)備:801h/2049d Inode:2102110 硬鏈接:2 權(quán)限:(0775/drwxrwxr-x) Uid:( 1000/ tarena) Gid:( 1000/ tarena) 最近訪問(wèn):2017-03-30 15:05:03.502154419 +0800 最近更改:2017-03-30 15:05:01.858151879 +0800 最近改動(dòng):2017-03-30 15:05:01.858151879 +0800 創(chuàng)建時(shí)間:-# stat a.txt 文件:"a.txt"大小:0 塊:0 IO 塊:4096 普通空文件 設(shè)備:801h/2049d Inode:2121684 硬鏈接:1 權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 最近訪問(wèn):2017-03-30 15:05:01.858151879 +0800 最近更改:2017-03-30 15:05:01.858151879 +0800 最近改動(dòng):2017-03-30 15:05:01.858151879 +0800 創(chuàng)建時(shí)間:-

二、文件類(lèi)型

UNIX 系統(tǒng)的大多數(shù)文件是普通文件或目錄,但是也有另外一些文件類(lèi)型。文件類(lèi)型包括如下幾種.

1、普通文件

在 UNIX/Linux 系統(tǒng)中通常所見(jiàn)到的文件,如用 C/C++ 語(yǔ)言編寫(xiě)的源代碼文件,編譯器、匯編器和連接器產(chǎn)生的匯編文件、目標(biāo)文件和可執(zhí)行文件,各種系統(tǒng)配置文件,shell 腳本文件,包括音頻、視頻等數(shù)字內(nèi)容的多媒體文件,乃至包括數(shù)據(jù)庫(kù)在內(nèi)的各種應(yīng)用程序所維護(hù)、管理和使用的數(shù)據(jù)文件等,都是普通文件。 一個(gè)文件包括兩部分?jǐn)?shù)據(jù),一部分是元數(shù)據(jù),如文件的類(lèi)型、權(quán)限、大小、用戶(hù)、組、各種時(shí)間戳等,存儲(chǔ)在 i 節(jié)點(diǎn)中,另一部分是內(nèi)容數(shù)據(jù),存儲(chǔ)在數(shù)據(jù)塊中

2、目錄文件

系統(tǒng)通過(guò) i 節(jié)點(diǎn)唯一地標(biāo)識(shí)一個(gè)文件的存在,但人們更愿意使用有意義的文件名來(lái)訪問(wèn)文件,目錄就是用來(lái)建立文件名和 i 節(jié)點(diǎn)之間的映射的。 目錄的本質(zhì)就是一個(gè)普通文件,與其它普通文件唯一的區(qū)別就是它僅僅存儲(chǔ)文件名和 i 節(jié)點(diǎn)號(hào)的映射,每一個(gè)這樣的映射,用目錄中的一個(gè)條目表示,謂之硬鏈接。
介紹相對(duì)路徑和絕對(duì)路徑: Lniux的文件系統(tǒng)中有一個(gè)大分組,它包含了文件系統(tǒng)中所有文件,這個(gè)大的分組用一個(gè)專(zhuān)門(mén)的目錄表示,這個(gè)目錄叫做根目錄,根目錄可以使用“/”表示
路徑可以用來(lái)表示文件或者文件夾所在的位置,路徑是從一個(gè)文件夾開(kāi)始走到另一個(gè)文件夾或者文件位置中間的這條路。把這條路經(jīng)過(guò)的所有文件夾名稱(chēng)按順序書(shū)寫(xiě)出來(lái)的結(jié)果就可以表示這條路。
路徑分為絕對(duì)路徑和相對(duì)路徑
絕對(duì)路徑:
起點(diǎn)必須是根目錄,如 /abc/def ?所有絕對(duì)路徑一定是以“/”作為開(kāi)頭的
相對(duì)路徑:可以把任何一個(gè)目錄作為起點(diǎn),如../../abc/def ?相對(duì)路徑編寫(xiě)時(shí)不應(yīng)該包含起點(diǎn)位置
相對(duì)目錄中“..”表示上層目錄
相對(duì)路徑中用“.”表示當(dāng)前
終端窗口里的當(dāng)前目錄是所有相對(duì)路徑的起點(diǎn),當(dāng)前目錄的位置是可以修改的。
pwd 命令:可以用來(lái)查看當(dāng)前目錄的位置
cd ?命令:可以用來(lái)修改當(dāng)前目錄位置 ??
ls ?命令:可以用來(lái)查看一個(gè)目錄的內(nèi)容

3、塊特殊文件

這種類(lèi)型的文件提供對(duì)設(shè)備(如磁盤(pán))帶緩沖的訪問(wèn),每次訪問(wèn)以固定長(zhǎng)度為單位進(jìn)行。

4、字符特殊文件

這種類(lèi)型的文件提供對(duì)設(shè)備不帶緩沖的訪問(wèn),每次訪問(wèn)長(zhǎng)度可變。系統(tǒng)中的所有設(shè)備要么是字符特殊文件,要么是塊特殊文件。

5、FIFO

這種類(lèi)型的文件用于進(jìn)程間通信,有時(shí)也稱(chēng)為命名管道。

6、套接字

這種類(lèi)型的文件用于進(jìn)程間的網(wǎng)絡(luò)通信。套接字也可用于一臺(tái)宿主機(jī)上進(jìn)程之間的非網(wǎng)絡(luò)通信。

7、符號(hào)鏈接

這種類(lèi)型的文件指向另一個(gè)文件。
再講到 stat 函數(shù)時(shí),其結(jié)構(gòu)中的 st_mode 有各類(lèi)文件類(lèi)型的信息 S_ISREG(m) is it a regular file (普通文件) S_ISDIR(m) directory (目錄文件) S_ISCHR(m) character device (字符特殊文件) S_ISBLK(m) block device (塊特殊文件) S_ISFIFO(m) FIFO (named pipe) (管道或 FIFO) S_ISLNK(m) symbolic link (符號(hào)鏈接)(Not in POSIX.1-1996.) S_ISSOCK(m) socket (套接字)(Not in POSIX.1-1996.)我們講權(quán)限的時(shí)候,也講過(guò)使用 ls -l 命令?也可以查看文件的類(lèi)型的: # ls -l 總用量 40 drwxr-xr-x 2 root root 4096 Mar 30 16:42 test -rw-r--r-- 1 root root 872 Mar 30 15:04 test.c其中第一個(gè)字符含義:
- ?普通文件 ?(例如:/etc/passwd)
d ?目錄 ?(例如:/etc)
s ?本地套接字 ?(例如:/dev/log)
c ?字符設(shè)備 ?(例如:/dev/tty)
b ?塊設(shè)備 ?(例如:/dev/sr0)
l ?符號(hào)鏈接 ?(例如:/dev/cdrom)
p ?有名管道/FIFO ?(例如:/var/lib/oprofile/opd_pipe)

相對(duì)于用函數(shù),我更喜歡用指令來(lái)獲取文件類(lèi)型 #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <stdio.h> #include <stdlib.h>int main(int argc, char *argv[]) {struct stat sb;if (argc != 2) {fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);exit(EXIT_FAILURE);}if (stat(argv[1], &sb) == -1) {perror("stat");exit(EXIT_FAILURE);}printf("File type: ");switch (sb.st_mode & S_IFMT) {case S_IFBLK: printf("block device\n"); break;case S_IFCHR: printf("character device\n"); break;case S_IFDIR: printf("directory\n"); break;case S_IFIFO: printf("FIFO/pipe\n"); break;case S_IFLNK: printf("symlink\n"); break;case S_IFREG: printf("regular file\n"); break;case S_IFSOCK: printf("socket\n"); break;default: printf("unknown?\n"); break;}printf("I-node number: %ld\n", (long) sb.st_ino);printf("Mode: %lo (octal)\n",(unsigned long) sb.st_mode);printf("Link count: %ld\n", (long) sb.st_nlink);printf("Ownership: UID=%ld GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);printf("Preferred I/O block size: %ld bytes\n",(long) sb.st_blksize);printf("File size: %lld bytes\n",(long long) sb.st_size);printf("Blocks allocated: %lld\n",(long long) sb.st_blocks);printf("Last status change: %s", ctime(&sb.st_ctime));printf("Last file access: %s", ctime(&sb.st_atime));printf("Last file modification: %s", ctime(&sb.st_mtime));exit(EXIT_SUCCESS); } 輸出結(jié)果: # ./a.out test.c File type: regular file I-node number: 2121694 Mode: 100644 (octal) Link count: 1 Ownership: UID=0 GID=0 Preferred I/O block size: 4096 bytes File size: 1642 bytes Blocks allocated: 8 Last status change: Fri Mar 31 09:15:48 2017 Last file access: Fri Mar 31 09:15:53 2017 Last file modification: Fri Mar 31 09:15:48 2017 # ls -l /dev/tty crw-rw-rw- 1 root tty 5, 0 Mar 29 16:00 /dev/tty # stat /dev/tty文件:"/dev/tty"大小:0 塊:0 IO 塊:4096 字符特殊文件 設(shè)備:5h/5d Inode:4961 硬鏈接:1 設(shè)備類(lèi)型:5,0 權(quán)限:(0666/crw-rw-rw-) Uid:( 0/ root) Gid:( 5/ tty) 最近訪問(wèn):2017-03-30 16:35:58.445938548 +0800 最近更改:2017-03-29 16:00:37.867616181 +0800 最近改動(dòng):2017-03-29 16:00:37.867616181 +0800 創(chuàng)建時(shí)間:-

三、文件訪問(wèn)權(quán)限

當(dāng)提及 文件 時(shí),指的是前面所提到的任何類(lèi)型的文件。所有文件類(lèi)型(目錄、字符特別文件等)都是有訪問(wèn)權(quán)限的st_mode 值也包含了對(duì)文件的訪問(wèn)權(quán)限位: The following flags are defined for the st_mode field:S_IFMT 0170000 bit mask for the file type bit fieldsS_IFSOCK 0140000 socketS_IFLNK 0120000 symbolic linkS_IFREG 0100000 regular fileS_IFBLK 0060000 block deviceS_IFDIR 0040000 directoryS_IFCHR 0020000 character deviceS_IFIFO 0010000 FIFOS_ISUID 0004000 set UID bitS_ISGID 0002000 set-group-ID bit (see below)S_ISVTX 0001000 sticky bit (see below)S_IRWXU 00700 mask for file owner permissions S_IRUSR 00400 owner has read permissionS_IWUSR 00200 owner has write permissionS_IXUSR 00100 owner has execute permissionS_IRWXG 00070 mask for group permissionsS_IRGRP 00040 group has read permissionS_IWGRP 00020 group has write permissionS_IXGRP 00010 group has execute permissionS_IRWXO 00007 mask for permissions for others (not in group)S_IROTH 00004 others have read permissionS_IWOTH 00002 others have write permissionS_IXOTH 00001 others have execute permissionchmod 命令用于修改這 9 個(gè)權(quán)限位。該命令允許我們用 u 表示用戶(hù)(所有者),用 g 表示組,用 o 表示其他。 對(duì)于一個(gè)文件的讀權(quán)限決定了我們是否能夠打開(kāi)現(xiàn)有文件進(jìn)行讀操作。這與 open 函數(shù)的 O_RDONLY 和 O_RDWR 標(biāo)志有關(guān)。 對(duì)于一個(gè)文件的寫(xiě)權(quán)限決定了我們是否能夠打開(kāi)現(xiàn)有文件進(jìn)行寫(xiě)操作。這與 open 函數(shù)的 O_WRONLY 和 O)RDWR 標(biāo)志有關(guān)。 為了在 open 函數(shù)中對(duì)一個(gè)文件指定 O_TRUNC 標(biāo)志,必須對(duì)該文件具有寫(xiě)權(quán)限(經(jīng)測(cè)試不限于寫(xiě)權(quán)限) 為了在一個(gè)目錄中創(chuàng)建一個(gè)新文件,必須對(duì)該目錄具有寫(xiě)權(quán)限和執(zhí)行權(quán)限 為了刪除一個(gè)現(xiàn)有文件,必須對(duì)包含該文件的目錄具有寫(xiě)權(quán)限和執(zhí)行權(quán)限。對(duì)該文件本身則不需要讀、寫(xiě)權(quán)限。 如果用 7 個(gè) exec 函數(shù)中的任何一個(gè)執(zhí)行某個(gè)文件,都必須對(duì)該文件具有執(zhí)行權(quán)限。該文件還必須是一個(gè)普通文件

四、函數(shù) access?

#include <unistd.h> int access(const char *pathname, int mode);

1、參數(shù)解析

第一個(gè)參數(shù):文件的路徑和文件名 第二個(gè)參數(shù):操作模式 F_OK:判斷文件是否存在 R_OK:判斷文件是否可讀 W_OK:判斷文件是否可寫(xiě) X_OK:判斷文件是否可執(zhí)行
查看 /usr/include/unistd.h 可發(fā)現(xiàn)這幾個(gè)模式可用數(shù)字表示
/* Values for the second argument to access.These may be OR'd together. */ #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ #define X_OK 1 /* Test for execute permission. */ #define F_OK 0 /* Test for existence. */

2、函數(shù)功能:

確定文件或文件夾的訪問(wèn)權(quán)限。即,檢查某個(gè)文件的存取方式,比如說(shuō)是只讀方式、只寫(xiě)方式等。

3、返回值

如果指定的存取方式有效,則函數(shù)返回 0,否則函數(shù)返回 -1。

4、示例說(shuō)明

#include <stdio.h> #include <unistd.h>int main (void) {if (0 == access ("a.txt", F_OK))printf ("文件存在\n");if (0 == access ("a.txt", 4))printf ("文件可讀\n");if (0 == access ("a.txt", W_OK))printf ("文件可寫(xiě)\n");if (0 == access ("a.txt", 1))printf ("文件可執(zhí)行\(zhòng)n");return 0; } 輸出結(jié)果: 文件存在 文件可讀 文件可寫(xiě)

五、函數(shù) umask

#include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask);

1、函數(shù)功能

主要用于設(shè)置創(chuàng)建文件時(shí)需要屏蔽的權(quán)限,返回之前屏蔽的權(quán)限 ?umask() 會(huì)將系統(tǒng)umask值設(shè)成參數(shù) mask&0777 后的值,然后將先前的umask值返回。在使用 open() 建立新文件時(shí),該參數(shù) mode 并非真正建立文件的權(quán)限,而是 (mode& ~umask) 的權(quán)限值.
系統(tǒng)默認(rèn)的屏蔽權(quán)限為 (0022): //創(chuàng)建文件 hh 查看權(quán)限: # ls -l hh -rw-r--r-- 1 root root 0 Mar 31 10:34 hh查看: # umask 0022

2、示例說(shuō)明

#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h>int main (void) {//使用umask函數(shù)設(shè)置屏蔽的權(quán)限mode_t old = umask (0055);//設(shè)置新的屏蔽字,返回舊的系統(tǒng)默認(rèn)// 當(dāng)前系統(tǒng)默認(rèn)屏蔽 0055printf ("old = %o\n", old);int fd = open ("a.txt", O_RDWR | O_CREAT, 0777);if (-1 == fd)perror ("fail to open"),exit (1);//恢復(fù)系統(tǒng)默認(rèn)的屏蔽字//對(duì)已經(jīng)創(chuàng)建過(guò)的文件權(quán)限沒(méi)有影響umask (old);close (fd);return 0; } 輸出結(jié)果: old = 22查看 a.txt 權(quán)限,說(shuō)明實(shí)際權(quán)限為 0722 (0777&~0055)屏蔽了0055 # ls -la a.txt -rwx-w--w- 1 root root 0 Mar 31 10:25 a.txt 示例說(shuō)明:在建立文件時(shí)指定文件權(quán)限為0777,通常umask值默認(rèn)為 0022,將 umask值新設(shè)為 0055,返回老的默認(rèn)值 22;則該文件的真正權(quán)限則為0777&~055=0722,也就是rw-w--w--

3、Linux 下的umask 指令

參看:umask 命令

(1)選項(xiàng)

-p:輸出的權(quán)限掩碼可直接作為指令來(lái)執(zhí)行; -S:以符號(hào)方式輸出權(quán)限掩碼。

(2)示例

設(shè)置新屏蔽權(quán)限: # umask -p 0055# umask -S 0055 u=rwx,g=w,o=w查看: # umask 0055

六、函數(shù) chmod 和 fchmod

#include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode);

1、參數(shù)解析

第一個(gè)參數(shù):文件路徑/文件描述符第二個(gè)參數(shù):權(quán)限模式查看 man fchmodThe new file permissions are specified in mode, which is a bit mask created by ORing together zero or more of the following:S_ISUID (04000) set-user-ID (set process effective user ID on execve(2))S_ISGID (02000) set-group-ID (set process effective group ID on execve(2); mandatory locking, as described in fcntl(2); take a newfile's group from parent directory, as described in chown(2) and mkdir(2))S_ISVTX (01000) sticky bit (restricted deletion flag, as described in unlink(2))S_IRUSR (00400) read by ownerS_IWUSR (00200) write by ownerS_IXUSR (00100) execute/search by owner ("search" applies for directories, and means that entries within the directory can beaccessed)S_IRGRP (00040) read by groupS_IWGRP (00020) write by groupS_IXGRP (00010) execute/search by groupS_IROTH (00004) read by othersS_IWOTH (00002) write by othersS_IXOTH (00001) execute/search by others

2、返回值

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

3、函數(shù)功能

修改現(xiàn)有文件的訪問(wèn)權(quán)限。
chmod 函數(shù)在指定的文件上進(jìn)行操作,而 fchmod 函數(shù)則對(duì)已打開(kāi)的文件進(jìn)行操作
為了改變一個(gè)文件的權(quán)限位,進(jìn)程的有效用戶(hù) ID 必須等于文件的所有者 ID,或者該進(jìn)程必須具有超級(jí)用戶(hù)權(quán)限

4、示例說(shuō)明

//示例一 chmod #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h>int main (void) {struct stat st;int res = stat ("a.txt", &st);if (-1 == res)perror ("fail to stat"), exit (1);printf ("權(quán)限是:%o\n", st.st_mode&0777);res = chmod ("a.txt", 0600);if (-1 == res)perror ("fail to chmod"), exit (1);res = stat ("a.txt", &st);if (-1 == res)perror ("fail to stat"), exit (1);printf ("權(quán)限是:%o\n", st.st_mode&0777);return 0; } 輸出結(jié)果: 權(quán)限是:644 權(quán)限是:600//示例二 fchmod #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h>int main (void) {int fd = open ("a.txt", O_RDONLY | O_CREAT, 0644);struct stat st;int res = stat ("a.txt", &st);if (-1 == res)perror ("fail to stat"), exit (1);printf ("權(quán)限是:%o\n", st.st_mode&0777);res = fchmod (fd, 0600);if (-1 == res)perror ("fail to chmod"), exit (1);res = stat ("a.txt", &st);if (-1 == res)perror ("fail to stat"), exit (1);printf ("權(quán)限是:%o\n", st.st_mode&0777);return 0; } 輸出結(jié)果: 權(quán)限是:644 權(quán)限是:600

5、示例總結(jié)

示例一:chmod 函數(shù)在指定的文件上進(jìn)行操作。示例二:fchmod 函數(shù)則對(duì)已打開(kāi)的文件進(jìn)行操作

6、Linux 下 chmod 命令

參看:C語(yǔ)言再學(xué)習(xí) -- 修改linux文件權(quán)限

(1)修改文件 a.txt 權(quán)限為 0644

chmod 644 a.txt 查看: # ls -l a.txt -rw-r--r-- 1 tarena root 0 Mar 31 16:16 a.txt

七、函數(shù) chown、fchown、lchown

#include <unistd.h> int chown(const char *path, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *path, uid_t owner, gid_t group);

1、參數(shù)解析

第一個(gè)參數(shù):文件路徑/文件描述符 第二個(gè)參數(shù):用戶(hù) ID 第三個(gè)參數(shù):組 ID

2、函數(shù)功能

可用于更改文件的用戶(hù) ID 和組 ID。
除了所引用的文件是符號(hào)鏈接以外,這三個(gè)函數(shù)的操作相似。在符號(hào)鏈接的情況下,lchown更改符號(hào)鏈接本身的所有者,而不是該符號(hào)鏈接所指向的文件
如若兩個(gè)參數(shù) owne r或 group 中的任意一個(gè)是 -1,則對(duì)應(yīng)的 ID 不變。
基于 BSD 的系統(tǒng)一直規(guī)定只有超級(jí)用戶(hù)才能更改一個(gè)文件的所有者。這樣做的原因是防止用戶(hù)改變其文件的所有者從而擺脫磁盤(pán)空間限額對(duì)他們的限制。系統(tǒng) V 則允許任一用戶(hù)更改他們所擁有的文件的所有者。

3、返回值

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

4、示例說(shuō)明

//示例一 chown #include <stdio.h> #include <unistd.h> int main (void) { //chown ("a.txt", 0, -1); chown ("a.txt", 0, 0); return 0; } 修改前: # ls -l a.txt 總用量 20 -rw------- 1 tarena tarena 0 Mar 31 14:24 a.txt chown ("a.txt", 0, 0); 修改后: # ls -l a.txt 總用量 20 -rw------- 1 root root 0 Mar 31 14:24 a.txt chown ("a.txt", 0, -1); 修改后: # ls -l a.txt 總用量 20 -rw------- 1 root tarena 0 Mar 31 14:24 a.txt //示例二 fchown #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h>int main (void) {int fd = open ("a.txt", O_RDONLY | O_CREAT, 0644);if (-1 == fd)perror ("fail to open"), exit (1); fchown (fd , 1000, 1000);return 0; } 修改前: # ls -la 總用量 12 -rw-r--r-- 1 root root 0 Mar 31 15:34 a.txt修改后: # ls -la 總用量 20 -rw-r--r-- 1 tarena tarena 0 Mar 31 15:34 a.txt//示例三 lchown #include <stdio.h> #include <unistd.h> #include <stdlib.h>int main (void) {int res = lchown ("/dev/cdrom1", 1000, 1000);if (-1 == res)perror ("fail to lchown"), exit (1);return 0; } 修改前: # ls -l cdrom1 sr0 lrwxrwxrwx 1 root root 3 Mar 29 16:00 cdrom1 -> sr0 brw-rw---- 1 root cdrom 11, 0 Mar 29 16:00 sr0修改后: # ls -l cdrom1 sr0 lrwxrwxrwx 1 tarena tarena 3 Mar 29 16:00 cdrom1 -> sr0 brw-rw---- 1 root cdrom 11, 0 Mar 29 16:00 sr0

5、示例總結(jié)

示例一:我們查看 cat /etc/passwd 可以看到所有用戶(hù)對(duì)應(yīng)的 用戶(hù) ID 和 組 ID。 其中超級(jí)用戶(hù) root 的用戶(hù) ID 為 0,組 ID 也為 0。而我所用的普通用戶(hù) tarena 用戶(hù) ID 為 1000,組 ID 為 1000 如若兩個(gè)參數(shù) owne r或 group 中的任意一個(gè)是 -1,則對(duì)應(yīng)的 ID 不變
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash tarena:x:1000:1000:tarena,,,:/home/tarena:/bin/bash 示例二:fchown,打開(kāi)時(shí)文件描述符。 示例三:這里有個(gè)名詞 符號(hào)鏈接,上面我們講文件類(lèi)型是有講到。比如?/dev/cdrom1就是符號(hào)鏈接它所指向的文件為sr0通過(guò)示例三可以驗(yàn)證,lchown 是改變符號(hào)鏈接本身的所有者,而不是該符號(hào)鏈接所指向的文件。 # stat /dev/cdrom1 文件:"/dev/cdrom1" -> "sr0"大小:3 塊:0 IO 塊:4096 符號(hào)鏈接 設(shè)備:5h/5d Inode:7459 硬鏈接:1 權(quán)限:(0777/lrwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root) 最近訪問(wèn):2017-03-31 15:59:53.712010169 +0800 最近更改:2017-03-29 16:00:36.411616237 +0800 最近改動(dòng):2017-03-31 15:59:47.936011311 +0800 創(chuàng)建時(shí)間:-

6、linux 下 chown 和 chgrp 命令

參看:C語(yǔ)言再學(xué)習(xí) -- 修改linux文件權(quán)限

(1)將test目錄所在的組由root修改為zslf組

sudo chgrp -R zslf test 查看: root@zslf-:/mnt# ls -la test/ 總用量 8 drwxr-xr-x 2 root zslf 4096 11月 24 17:20 . drwxr-xr-x 5 root root 4096 11月 24 17:20 ..

(2)將test目錄下的所有文件和子目錄的屬主由root改為zslf

sudo chown -R zslf.zslf test 查看: root@zslf-virtual-machine:/mnt# ls -la test/ 總用量 8 drwxr-xr-x 2 zslf root 4096 11月 24 17:20 . drwxr-xr-x 5 root root 4096 11月 24 17:20 ..

(3)將test目錄下的所有文件和子目錄的屬主由root改為zslf,屬組有root改為zslf。

sudo chown -R zslf.zslf test 查看: root@zslf-virtual-machine:/mnt# ls -la test/ 總用量 8 drwxr-xr-x 2 zslf zslf 4096 11月 24 17:33 . drwxr-xr-x 5 root root 4096 11月 24 17:20 .. -rw-r--r-- 1 zslf zslf 0 11月 24 17:33 hh

八、文件長(zhǎng)度(大小)

stat 結(jié)構(gòu)成員 st_size 表示以字節(jié)為單位的文件的長(zhǎng)度。此字段只對(duì)普通文件、目錄文件和符號(hào)鏈接有意義。 對(duì)于普通文件,其文件長(zhǎng)度可以是 0,在開(kāi)始讀這種文件時(shí),將得到文件結(jié)束(EOF)指示 參看:C語(yǔ)言再學(xué)習(xí) -- EOF、feof函數(shù)、ferror函數(shù)??對(duì)于目錄,文件長(zhǎng)度通常是一個(gè)數(shù)(如 16 或 512)的整倍數(shù)。例如,下面的例子中,目錄文件長(zhǎng)度為 4096,普通文件 a.txt 文件長(zhǎng)度可以是 0.# ls -la 總用量 20 drwxrwxr-x 2 tarena tarena 4096 Mar 31 16:16 . drwxrwxr-x 4 tarena tarena 4096 Mar 28 15:38 .. -rwxr-xr-x 1 root root 7233 Mar 31 16:16 a.out -rw-r--r-- 1 tarena root 0 Mar 31 16:16 a.txt -rw-r--r-- 1 root root 184 Mar 31 16:15 test.c 對(duì)于符號(hào)鏈接,文件長(zhǎng)度是在文件名中的實(shí)際字節(jié)數(shù)。注意,因?yàn)榉?hào)鏈接文件長(zhǎng)度總是由 st_size 指示,所以它并不包含通常 C 語(yǔ)言用作名字結(jié)尾的 null 字節(jié))。例如,在下面的例子中,符號(hào)鏈接 cdrom1 的文件長(zhǎng)度為 3 # ls -l /dev/cdrom1 lrwxrwxrwx 1 root root 3 Apr 5 09:19 /dev/cdrom1 -> sr0

九、文件空洞

文件空洞,是由所設(shè)置的偏移量超過(guò)文件尾端,并寫(xiě)入了某些數(shù)據(jù)后造成的。位于文件中但沒(méi)有被寫(xiě)過(guò)的字節(jié)都被設(shè)為 0,文件空洞不占用磁盤(pán)空間,但被計(jì)算在文件大小之內(nèi)。
作為一個(gè)例子,考慮下列情況: # ls -l a.out -rwxr-xr-x 1 root root 7233 Mar 31 16:16 a.out# du -h a.out 8.0K a.out 文件 a.out 長(zhǎng)度為 7.2K,可 du 命令報(bào)告該文件所使用的磁盤(pán)空間總量為 8.0K。很明顯,此文件中有很多空洞. du 命令,參看:du 命令

十、文件截?cái)?/span>

#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length);

1、參數(shù)解析

第一個(gè)參數(shù):文件路徑/文件描述符 第二個(gè)參數(shù):文件長(zhǎng)度

2、函數(shù)功能:修改文件長(zhǎng)度

這兩個(gè)函數(shù)將一個(gè)現(xiàn)有文件長(zhǎng)度截?cái)酁?length。如果該文件以前的長(zhǎng)度大于 length,則超過(guò) length 以外的數(shù)據(jù)就不再能訪問(wèn)。如果以前的長(zhǎng)度小于 length,文件長(zhǎng)度增加,在以前的文件尾端和新的文件尾端之間的數(shù)據(jù)將讀作 0 (也就是可能再文件中創(chuàng)建了一個(gè)空洞)。

3、返回值

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

4、示例說(shuō)明

#include <stdio.h> //示例一 truncate 函數(shù) #include <unistd.h> #include <stdlib.h>int main (void) {int res = truncate ("a.txt", 50);if (-1 == res)perror ("fail to truncate"), exit (1);return 0; }a.txt 為空時(shí): 執(zhí)行前: -rw-r--r-- 1 tarena root 0 Mar 31 16:16 a.txt 執(zhí)行后: -rw-r--r-- 1 tarena root 50 Apr 5 13:14 a.txt長(zhǎng)度截?cái)酁?0,后面的數(shù)據(jù)填充為 0。a.txt 不為空時(shí): 執(zhí)行前: -rw-r--r-- 1 tarena root 296 Apr 5 13:17 a.txt 執(zhí)行后: -rw-r--r-- 1 tarena root 50 Apr 5 13:14 a.txt長(zhǎng)度截?cái)酁?0, 后面的數(shù)據(jù)不再能訪問(wèn)。 //示例二 ftruncate 函數(shù) #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h>int main (void) {int fd = open ("a.txt", O_RDWR);if (-1 == fd)perror ("fail to open"), exit (1);int res = ftruncate (fd, 50);if (-1 == res)perror ("fail to truncate"), exit (1);return 0; }a.txt 為空時(shí): 執(zhí)行前: -rw-r--r-- 1 tarena root 0 Mar 31 16:16 a.txt 執(zhí)行后: -rw-r--r-- 1 tarena root 50 Apr 5 13:14 a.txt長(zhǎng)度截?cái)酁?0,后面的數(shù)據(jù)填充為 0。a.txt 不為空時(shí): 執(zhí)行前: -rw-r--r-- 1 tarena root 296 Apr 5 13:17 a.txt 執(zhí)行后: -rw-r--r-- 1 tarena root 50 Apr 5 13:14 a.txt長(zhǎng)度截?cái)酁?0, 后面的數(shù)據(jù)不再能訪問(wèn)。

5、與 O_TRUNC?

打開(kāi)文件時(shí),可以使用 O_TRUNC 來(lái)將文件的長(zhǎng)度截?cái)酁?0.

十一、函數(shù) rename 和 renameat

#include <stdio.h> int rename(const char *oldname, const char *newname); int renameat(int oldfd,const char *oldname, int newfd, const char *newname);

1、函數(shù)功能

用于文件或目錄的重命名根據(jù) oldname 是指文件、目錄還是符號(hào)鏈接,有幾種情況需要加以說(shuō)明。我們必須說(shuō)明如果 newname 已經(jīng)存在時(shí)將會(huì)發(fā)生什么 (1)如果 oldname 指的是一個(gè)文件而不是目錄,那么為該文件或符號(hào)鏈接重命名。在這種情況下,如果 newname 已存在,則它不能引用一個(gè)目錄。如果 newname 已存在,而且不是一個(gè)目錄,則先將目錄項(xiàng)刪除然后將oldname 重命名為 newname。對(duì)包含 oldname 的目錄以及包含 newname 的目錄,調(diào)用進(jìn)程必須具有寫(xiě)權(quán)限,因?yàn)閷⒏倪@兩個(gè)目錄。
(2)如若oldname指的是一個(gè)目錄,那么為該目錄重命名。如果 newname 已存在,則它必須引用一個(gè)目錄,而且該目錄應(yīng)當(dāng)是空目錄(空目錄指只有.和..項(xiàng))。如果 newname 存在(而且是一個(gè)新目錄),則先將其刪除,然后將oldname 重命名為 newname。另外,當(dāng)為一個(gè)目錄重命名時(shí),newname 不能包含 oldname 作為其路徑名字的路徑前綴。例如,不能將 /usr/foo 重命名為 /usr/foo/testdir,因?yàn)榕f名字 (/usr/foo) 是新名字的路徑前綴,因而不能將其刪除。
(3)如若 oldname 或 newname 引用符號(hào)鏈接,則處理的是符號(hào)鏈接本身,而不是它所引用的文件。
(4)不能對(duì) . 和 .. 重命名。
更確切的說(shuō),.和..都不能出現(xiàn)在 oldname 和 newname 的最后部分。
(5)作為一個(gè)特例,如果 oldname 和 newname 引用同一文件,則函數(shù)不做任何更改而成功返回。
如若 newname 已經(jīng)存在,則調(diào)用進(jìn)程對(duì)它需要有寫(xiě)權(quán)限。
另外,調(diào)用進(jìn)程將刪除 oldname 目錄項(xiàng),并可能要?jiǎng)?chuàng)建 newname 目錄項(xiàng),所以它需要對(duì)包含的 oldname 及包含 newname 的目錄具有寫(xiě)和執(zhí)行權(quán)限。
除了當(dāng) oldname 或 newname 指向相對(duì)路徑名時(shí),其他情況下 renameat 函數(shù)與 rename 函數(shù)功能相同。如果 oldname 參數(shù)指定了相對(duì)路徑,就相對(duì)于 oldfd 參數(shù)引用的目錄來(lái)計(jì)算 oldname。類(lèi)似的,如果 oldname 參數(shù)指定了相對(duì)路徑,就相對(duì)于newfd引用的目錄來(lái)計(jì)算 newname。oldfd 或 newfd 參數(shù)都能設(shè)置成 AT_FDCWD,此時(shí)相對(duì)于當(dāng)前目錄來(lái)計(jì)算相應(yīng)的路徑名。

2、返回值

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

3、示例說(shuō)明

#include <stdio.h> #include <stdlib.h>int main (void) {rename ("a.txt", "b.txt");return 0; }

4、Linux 下的 mv 命令

參看:mv 命令 mv命令用來(lái)對(duì)文件或目錄重新命名,或者將文件從一個(gè)目錄移到另一個(gè)目錄中。

(1)選項(xiàng)

--backup=<備份模式>:若需覆蓋文件,則覆蓋前先行備份; -b:當(dāng)文件存在時(shí),覆蓋前,為其創(chuàng)建一個(gè)備份; -f:若目標(biāo)文件或目錄與現(xiàn)有的文件或目錄重復(fù),則直接覆蓋現(xiàn)有的文件或目錄; -i:交互式操作,覆蓋前先行詢(xún)問(wèn)用戶(hù),如果源文件與目標(biāo)文件或目標(biāo)目錄中的文件同名,則詢(xún)問(wèn)用戶(hù)是否覆蓋目標(biāo)文件。用戶(hù)輸入”y”,表示將覆蓋目標(biāo)文件;輸入”n”,表示取消對(duì)源文件的移動(dòng)。這樣可以避免誤將文件覆蓋。 --strip-trailing-slashes:刪除源文件中的斜杠“/”; -S<后綴>:為備份文件指定后綴,而不使用默認(rèn)的后綴; --target-directory=<目錄>:指定源文件要移動(dòng)到目標(biāo)目錄; -u:當(dāng)源文件比目標(biāo)文件新或者目標(biāo)文件不存在時(shí),才執(zhí)行移動(dòng)操作。

(2)示例

將文件ex3改名為new1 mv ex3 new1

十二、函數(shù) mkdir、mkdirat和 rmdir

用 mkdir 和mkdirat 函數(shù)創(chuàng)建目錄,用 rmdir 函數(shù)刪除目錄。#include <sys/stat.h> int mkdir (const char *pathname, mode_t mode); int mkdirat(int dirfd, const char *pathname, mode_t mode);

1、函數(shù)功能

這兩個(gè)函數(shù)創(chuàng)建一個(gè)新的空目錄。其中 . 和 .. 目錄項(xiàng)是自動(dòng)創(chuàng)建的。所指定的文件訪問(wèn)權(quán)限 mode 由進(jìn)程的文件模式創(chuàng)建屏蔽字修改。

2、返回值

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

3、mode 方式

S_IRWXU 00700權(quán)限,代表該文件所有者擁有讀,寫(xiě)和執(zhí)行操作的權(quán)限 S_IRUSR(S_IREAD) 00400權(quán)限,代表該文件所有者擁有可讀的權(quán)限 S_IWUSR(S_IWRITE) 00200權(quán)限,代表該文件所有者擁有可寫(xiě)的權(quán)限 S_IXUSR(S_IEXEC) 00100權(quán)限,代表該文件所有者擁有執(zhí)行的權(quán)限 S_IRWXG 00070權(quán)限,代表該文件用戶(hù)組擁有讀,寫(xiě)和執(zhí)行操作的權(quán)限 S_IRGRP 00040權(quán)限,代表該文件用戶(hù)組擁有可讀的權(quán)限 S_IWGRP 00020權(quán)限,代表該文件用戶(hù)組擁有可寫(xiě)的權(quán)限 S_IXGRP 00010權(quán)限,代表該文件用戶(hù)組擁有執(zhí)行的權(quán)限 S_IRWXO 00007權(quán)限,代表其他用戶(hù)擁有讀,寫(xiě)和執(zhí)行操作的權(quán)限 S_IROTH 00004權(quán)限,代表其他用戶(hù)擁有可讀的權(quán)限 S_IWOTH 00002權(quán)限,代表其他用戶(hù)擁有可寫(xiě)的權(quán)限 S_IXOTH 00001權(quán)限,代表其他用戶(hù)擁有執(zhí)行的權(quán)限

4、示例說(shuō)明

一般,新建目錄的權(quán)限為 0755#include <stdio.h> #include <stdlib.h>int main (void) {mkdir ("test", 0755);return 0; }

5、Linux 下的 mkdir 命令

參看:mkdir 命令

(1)選項(xiàng)?

-Z:設(shè)置安全上下文,當(dāng)使用SELinux時(shí)有效; -m <目標(biāo)屬性>或--mode<目標(biāo)屬性>建立目錄的同時(shí)設(shè)置目錄的權(quán)限; -p或--parents 若所要建立目錄的上層目錄目前尚未建立,則會(huì)一并建立上層目錄; --version 顯示版本信息。

(2)示例

在目錄/usr/meng下建立子目錄test,并且只有文件主有讀、寫(xiě)和執(zhí)行權(quán)限,其他人無(wú)權(quán)訪問(wèn) mkdir -m 700 /usr/meng/test在當(dāng)前目錄中建立bin和bin下的os_1目錄,權(quán)限設(shè)置為文件主可讀、寫(xiě)、執(zhí)行,同組用戶(hù)可讀和執(zhí)行,其他用戶(hù)無(wú)權(quán)訪問(wèn) mkdir -p-m 750 bin/os_1

6、函數(shù) rmdir

用 rmdir 函數(shù)可以刪除一個(gè)空目錄。空目錄是只可包含 . 和 .. 這兩項(xiàng)的目錄。#include <unistd.h> int rmdir( const char *pathname ); 返回值:若成功則返回0,若出錯(cuò)則返回-1如果調(diào)用此函數(shù)使目錄的鏈接計(jì)數(shù)成為0,并且也沒(méi)有其他進(jìn)程打開(kāi)此目錄,則釋放由此目錄占用的空間。如果在鏈接計(jì)數(shù)達(dá)到0時(shí),有一個(gè)或幾個(gè)進(jìn)程打開(kāi)了此目錄,則在此函數(shù)返回前刪除最后一個(gè)鏈接及.和..項(xiàng)。另外,在此目錄中不能再創(chuàng)建新文件。但是在最后一個(gè)進(jìn)程關(guān)閉它之前并不釋放此目錄。(即使另一個(gè)進(jìn)程打開(kāi)該目錄,它們?cè)诖四夸浵乱膊荒軋?zhí)行其他操作。這樣處理的原因是,為了使rmdir函數(shù)成功執(zhí)行,該目錄必須是空的。)

(1)示例

#include <stdio.h> #include <stdlib.h>int main (void) {rmdir ("test");return 0; }

7、Linux 下的 rmdir 命令

參看:rmdir 命令??刪除目錄必須是 空目錄

(1)選項(xiàng)

-p或--parents:刪除指定目錄后,若該目錄的上層目錄已變成空目錄,則將其一并刪除; --ignore-fail-on-non-empty:此選項(xiàng)使rmdir命令忽略由于刪除非空目錄時(shí)導(dǎo)致的錯(cuò)誤信息; -v或-verboes:顯示命令的詳細(xì)執(zhí)行過(guò)程; --help:顯示命令的幫助信息; --version:顯示命令的版本信息。

(2)示例

刪除子目錄os_1和其父目錄bin rmdir -p bin/os_1

8、Linux 下的 rm 命令

正如上面的刪除目錄命令 rmdir 必須是空目錄。非空目錄則可以使用 rm 命令

(1)選項(xiàng)

-d:直接把欲刪除的目錄的硬連接數(shù)據(jù)刪除成0,刪除該目錄; -f:強(qiáng)制刪除文件或目錄; -i:刪除已有文件或目錄之前先詢(xún)問(wèn)用戶(hù); -r或-R:遞歸處理,將指定目錄下的所有文件與子目錄一并處理; --preserve-root:不對(duì)根目錄進(jìn)行遞歸操作; -v:顯示指令的詳細(xì)執(zhí)行過(guò)程。

(2)示例

刪除 test 目錄下除隱含文件外的所有文件和子目錄 # rm -r test

(3)說(shuō)明

不過(guò)使用 rm 是很危險(xiǎn)的,刪除的東西是無(wú)法恢復(fù)。有時(shí)養(yǎng)成好的習(xí)慣使用 -i 選項(xiàng)刪除前先詢(xún)問(wèn)用戶(hù)一下也不錯(cuò)。

十三、讀目錄

1、opendir 函數(shù)

#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name);

(1)函數(shù)功能

主要用于打開(kāi)參數(shù)指定的目錄,并且返回該目錄所在的地址。

(2)返回值

成功返回指針,失敗返回NULL

2、readdir 函數(shù)

#include <dirent.h> struct dirent *readdir(DIR *dirp);

(1)函數(shù)功能

主要用于讀取參數(shù)指定的目錄中內(nèi)容,返回參數(shù)指針

(2)結(jié)構(gòu)體參數(shù)

struct dirent {ino_t d_ino; /* inode number */ //i節(jié)點(diǎn)的編號(hào)off_t d_off; /* offset to the next dirent */ //距離下一個(gè)子項(xiàng)的偏移量unsigned short d_reclen; /* length of this record */ //記錄的長(zhǎng)度unsigned char d_type; /* type of file; not supported //文件的類(lèi)型by all file system types */ char d_name[256]; /* filename */ //文件名};

3、closedir 函數(shù)

#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);

(1)函數(shù)功能

主要用于關(guān)閉參數(shù)指定的目錄

(2)示例說(shuō)明

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h>int main (void) {DIR *dir = opendir ("code");if (NULL == dir)perror ("fail to opendir"), exit (1);struct dirent *ent;while (ent = readdir (dir))printf ("文件名為:%s\n", ent->d_name);closedir (dir);return 0; } 輸出結(jié)果: 文件名為:test 文件名為:. 文件名為:..

十四、函數(shù) chdir、fchdir 和 getcwd

?1、chdir、fchdir

這兩個(gè)函數(shù)中,分別用 pathname 或打開(kāi)文件描述符來(lái)指定新的當(dāng)前工作目錄#include <unistd.h> int chdir(const char *path); int fchdir(int fd);

(1)函數(shù)功能

更改當(dāng)前工作目錄。工作目錄只是進(jìn)程的一個(gè)屬性,所以它只影響進(jìn)程本身。

(2)返回值

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

(3)示例說(shuō)明

//示例一 chdir #include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (void) {if (-1 == chdir ("/tmp"))perror ("fail to chdir"), exit (1);return 0; }//示例二 fchdir #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h>int main (void) {int fd = open ("/tmp", O_RDONLY); if (-1 == fchdir (fd))perror ("fail to fchdir"), exit (1);char buf[100];if (getcwd(buf, 100) == NULL)perror ("fail to getcwd"), exit (1);printf("cwd = %s\n", buf);return 0; } 輸出結(jié)果: cwd = /tmp

(4)示例總結(jié)

執(zhí)行示例,使用 pwd 命令查看當(dāng)前目錄可以發(fā)現(xiàn),并沒(méi)有發(fā)生改變。這是因?yàn)槊總€(gè)程序運(yùn)行在獨(dú)立的進(jìn)程中,shell 的當(dāng)前工作目錄并不會(huì)隨著程序調(diào)用 chdir 而改變。由此可見(jiàn),為了改變 shell 進(jìn)程自己的工作目錄,shell 應(yīng)當(dāng)直接調(diào)用 chdir 函數(shù),為此, cd 命令內(nèi)建在 shell 中。

2、函數(shù) getcwd

#include <unistd.h> char *getcwd(char *buf, size_t size);

(1)函數(shù)功能

獲取當(dāng)前工作目錄的絕對(duì)路徑

(2)返回值

成功返回 buf,失敗返回 NULL

(3)示例說(shuō)明

#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (void) {if (chdir("/usr/bin/") < 0)perror ("chdir failed"), exit (1);char buf[100];if (getcwd(buf, 100) == NULL)perror ("fail to getcwd"), exit (1);printf("cwd = %s\n", buf);return0; } 輸出結(jié)果: cwd = /usr/bin

(4)Linux 下的 pwd 命令

pwd 命令以絕對(duì)路徑的方式顯示用戶(hù)當(dāng)前工作目錄。命令將當(dāng)前目錄的全路徑名稱(chēng)(從根目錄)寫(xiě)入標(biāo)準(zhǔn)輸出。全部目錄使用 / 分隔。第一個(gè) / 表示根目錄,最后一個(gè)目錄是當(dāng)前目錄。執(zhí)行 pwd 命令可立刻得知您目前所在的工作目錄的絕對(duì)路徑名稱(chēng)。

十五、未講部分:

設(shè)置用戶(hù) ID和設(shè)置組 ID新文件和目錄的所有權(quán)粘著位文件系統(tǒng)函數(shù) link符號(hào)鏈接創(chuàng)建和讀取符號(hào)鏈接文件的時(shí)間函數(shù) futimens設(shè)備特殊文件

總結(jié)

以上是生活随笔為你收集整理的UNIX再学习 -- 文件和目录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。