Linux目錄操作詳解
- 1. 獲取當前工作目錄
- 1.1 getcwd()
- 1.2 get_current_dir_name()
- 2. 切換工作目錄
- 2.1 chdir()
- 3. 創建和刪除目錄
- 3.1 mkdir()
- 3.2 rmdir()
- 4. 獲取目錄中的文件列表
- 4.1 opendir() 打開目錄
- 4.2 readdir() 讀取目錄內容
- 4.3 closedir() 關閉目錄
- 5. dirent 結構體
- 6. 示例代碼
- (1) 獲取當前工作目錄
- (2)切換工作目錄
- (3)創建和刪除目錄
- (4)遍歷目錄中的文件
- (5)結合所有操作的示例
本文將介紹常用的Linux目錄操作函數,包括獲取當前工作目錄、切換工作目錄、創建和刪除目錄等操作。
1. 獲取當前工作目錄
Linux提供了兩個常用函數來獲取當前工作目錄。
1.1 getcwd()
該函數用于獲取當前進程的工作目錄,并將路徑存儲在用戶提供的緩沖區中。
函數原型:
char *getcwd(char *buf, size_t size);
buf
:存儲當前工作目錄路徑的緩沖區。size
:緩沖區的大小。
如果成功,返回當前工作目錄的路徑;如果失敗,返回NULL
,并設置errno。
1.2 get_current_dir_name()
這是一個較為簡化的函數,不需要傳入緩沖區,它會動態分配內存來存儲當前目錄的路徑。
函數原型:
char *get_current_dir_name(void);
調用此函數后,返回的字符串指向當前工作目錄的路徑,使用完后需要調用free()
來釋放內存。
2. 切換工作目錄
切換工作目錄的操作在文件操作中經常會用到。Linux提供了一個函數chdir()
來實現該操作。
2.1 chdir()
該函數用于改變當前進程的工作目錄。
函數原型:
int chdir(const char *path);
path
:目標目錄的路徑。
返回值:
- 成功:返回0。
- 失敗:返回-1,
errno
會被設置為具體的錯誤碼(如目錄不存在或權限不足)。
3. 創建和刪除目錄
3.1 mkdir()
用于創建一個新目錄。需要提供目錄名和訪問權限。
函數原型:
int mkdir(const char *pathname, mode_t mode);
pathname
:新目錄的路徑。mode
:目錄權限,通常使用如0755
來設定權限。
返回值:
- 成功:返回0。
- 失敗:返回-1,
errno
會設置相應的錯誤。
3.2 rmdir()
用于刪除一個空目錄。刪除時,目錄必須為空。
函數原型:
int rmdir(const char *path);
path
:需要刪除的目錄路徑。
返回值:
- 成功:返回0。
- 失敗:返回-1,
errno
會設置具體的錯誤原因(如目錄非空)。
4. 獲取目錄中的文件列表
獲取目錄中的文件列表通常需要遍歷目錄。Linux提供了一些API來幫助我們打開、讀取和關閉目錄。
4.1 opendir() 打開目錄
函數原型:
DIR *opendir(const char *pathname);
pathname
:需要打開的目錄路徑。
返回值:
- 成功:返回
DIR*
,即目錄流。 - 失敗:返回
NULL
,errno
設置為錯誤碼。
4.2 readdir() 讀取目錄內容
函數原型:
struct dirent *readdir(DIR *dirp);
dirp
:通過opendir()
獲得的目錄流。
返回值:
- 成功:返回指向
dirent
結構體的指針,其中包含了當前目錄項的信息。 - 失敗:返回
NULL
。
4.3 closedir() 關閉目錄
函數原型:
int closedir(DIR *dirp);
dirp
:需要關閉的目錄流。
返回值:
- 成功:返回0。
- 失敗:返回-1。
5. dirent 結構體
在通過readdir()
函數獲取目錄項時,返回的是一個dirent
結構體,該結構體定義了目錄項的相關信息。
結構體定義:
struct dirent {long d_ino; // inode編號off_t d_off; // 偏移量unsigned short d_reclen; // 目錄項長度unsigned char d_type; // 文件類型char d_name[NAME_MAX+1]; // 文件名
};
d_name
:存儲目錄項的文件名,最大長度為255字符。d_type
:文件類型,可能的值包括普通文件(DT_REG
)、目錄(DT_DIR
)、符號鏈接(DT_LNK
)等。
6. 示例代碼
(1) 獲取當前工作目錄
以下示例展示了如何使用getcwd()
和get_current_dir_name()
獲取當前工作目錄:
#include <iostream>
#include <unistd.h>
#include <stdlib.h>using namespace std;int main() {// 使用getcwd()獲取當前工作目錄char path1[256];if (getcwd(path1, sizeof(path1)) != NULL) {cout << "Current working directory (getcwd): " << path1 << endl;} else {perror("getcwd failed");}// 使用get_current_dir_name()獲取當前工作目錄char *path2 = get_current_dir_name();if (path2 != NULL) {cout << "Current working directory (get_current_dir_name): " << path2 << endl;free(path2); // 注意釋放內存} else {perror("get_current_dir_name failed");}return 0;
}
(2)切換工作目錄
示例展示了如何使用chdir()
函數來切換工作目錄:
#include <iostream>
#include <unistd.h>
#include <stdlib.h>using namespace std;int main() {// 獲取當前工作目錄char currentDir[256];if (getcwd(currentDir, sizeof(currentDir)) != NULL) {cout << "Current working directory: " << currentDir << endl;} else {perror("getcwd failed");return 1;}// 切換到新的目錄if (chdir("/home") == 0) {cout << "Changed working directory to /home" << endl;} else {perror("chdir failed");return 1;}// 獲取切換后的工作目錄if (getcwd(currentDir, sizeof(currentDir)) != NULL) {cout << "New working directory: " << currentDir << endl;} else {perror("getcwd failed");}return 0;
}
(3)創建和刪除目錄
以下示例展示了如何使用mkdir()
創建目錄和rmdir()
刪除空目錄:
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>using namespace std;int main() {const char *dirName = "testDir";// 創建目錄if (mkdir(dirName, 0755) == 0) {cout << "Directory " << dirName << " created successfully." << endl;} else {perror("mkdir failed");return 1;}// 刪除目錄if (rmdir(dirName) == 0) {cout << "Directory " << dirName << " deleted successfully." << endl;} else {perror("rmdir failed");return 1;}return 0;
}
(4)遍歷目錄中的文件
以下示例展示了如何使用opendir()
, readdir()
, 和 closedir()
來遍歷目錄中的文件:
#include <iostream>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>using namespace std;int main() {const char *dirName = "/home";// 打開目錄DIR *dir = opendir(dirName);if (dir == NULL) {perror("opendir failed");return 1;}struct dirent *entry;// 遍歷目錄中的文件cout << "Files in directory " << dirName << ":" << endl;while ((entry = readdir(dir)) != NULL) {cout << "File: " << entry->d_name << endl;}// 關閉目錄closedir(dir);return 0;
}
(5)結合所有操作的示例
以下是一個較為完整的示例,結合了獲取當前目錄、切換目錄、創建目錄和遍歷目錄等操作:
#include <iostream>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>using namespace std;int main() {char currentDir[256];// 獲取當前工作目錄if (getcwd(currentDir, sizeof(currentDir)) != NULL) {cout << "Current working directory: " << currentDir << endl;} else {perror("getcwd failed");return 1;}// 創建目錄const char *dirName = "exampleDir";if (mkdir(dirName, 0755) == 0) {cout << "Directory " << dirName << " created successfully." << endl;} else {perror("mkdir failed");return 1;}// 切換到新創建的目錄if (chdir(dirName) == 0) {cout << "Changed to directory " << dirName << endl;} else {perror("chdir failed");return 1;}// 獲取切換后的工作目錄if (getcwd(currentDir, sizeof(currentDir)) != NULL) {cout << "New working directory: " << currentDir << endl;} else {perror("getcwd failed");}// 列出當前目錄中的文件DIR *dir = opendir(".");if (dir == NULL) {perror("opendir failed");return 1;}struct dirent *entry;cout << "Files in the current directory:" << endl;while ((entry = readdir(dir)) != NULL) {cout << "File: " << entry->d_name << endl;}// 關閉目錄closedir(dir);// 刪除目錄if (rmdir(dirName) == 0) {cout << "Directory " << dirName << " deleted successfully." << endl;} else {perror("rmdir failed");return 1;}return 0;
}