Linux目錄遍歷實現 -- 列出當前目錄下所有檔案
遞迴遍歷目錄,列出目錄中的檔案,程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_PATH 512 //最大檔案長度定義為512
/*對目錄中所有檔案執行print_file_info操作*/
void dirwalk(char *dir, void (*func)(char *))
{
char name[MAX_PATH];
struct dirent *dp;
DIR *dfd;
if((dfd = opendir(dir)) == NULL){
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
while((dp = readdir(dfd)) != NULL){ //讀目錄記錄項
if(strcmp(dp->d_name, ".") == 0 || strcmp (dp -> d_name, "..") == 0){
continue; //跳過當前目錄以及父目錄
}
if(strlen(dir) + strlen(dp -> d_name) + 2 > sizeof(name)){
fprintf(stderr, "dirwalk : name %s %s too long\n", dir, dp->d_name);
}else{
sprintf(name, "%s/%s", dir, dp->d_name);
(*func)(name);
}
}
}
/*列印檔案資訊*/
void print_file_info(char *name)
{
struct stat stbuf;
if(stat(name, &stbuf) == -1){
fprintf(stderr, "file size: open %s failed\n", name);
return;
}
if((stbuf.st_mode & S_IFMT) == S_IFDIR){
dirwalk(name, print_file_info); //如果是目錄遍歷下一級目錄
}else{
printf("%8ld %s\n", stbuf.st_size, name);//不是目錄,列印檔案size及name
}
}
int main(int argc, char *argv[])
{
printf("file size file name\n");
if(argc == 1){
print_file_info(".");//未加引數執行時,從當前目錄開始遍歷
}else{
while(--argc>0){
print_file_info(*++argv);
}
}
return 0;
}
程式執行及執行結果:
yu@ubuntu:~/Linux/206/dirop$ gcc -o dir dir.c
yu@ubuntu:~/Linux/206/dirop$ ./dir
file size file name
7820 ./dir
1351 ./dir.c
yu@ubuntu:~/Linux/206/dirop$ ./dir /home/yu/Linux/206
file size file name
7560 /home/yu/Linux/206/copy
0 /home/yu/Linux/206/src
7820 /home/yu/Linux/206/dir
487 /home/yu/Linux/206/copy.c
7516 /home/yu/Linux/206/lseek
7516 /home/yu/Linux/206/llll/lseek
1025 /home/yu/Linux/206/llll/x
348 /home/yu/Linux/206/llll/lseek.c
0 /home/yu/Linux/206/x
24 /home/yu/Linux/206/dest
348 /home/yu/Linux/206/lseek.c
7820 /home/yu/Linux/206/dirop/dir
1351 /home/yu/Linux/206/dirop/dir.c
相關推薦
Linux目錄遍歷實現 -- 列出當前目錄下所有檔案
遞迴遍歷目錄,列出目錄中的檔案,程式碼: #include <stdio.h> #include <stdlib.h> #include <string.h>
列出當前路徑下所有檔案
os.sep可以取代作業系統特定的路徑分隔符。windows下為 “\\” os.name字串指示你正在使用的平臺。比如對於Windows,它是'nt',而對於Linux/Unix使用者,它是'posix'。 os.getcwd()函式得到當前工作目錄,即當前Python指令碼工作的目錄路徑。
Linux c : 遍歷並刪除指定目錄下的所有檔案
系統:ubuntu17.10 遍歷並刪除指定目錄下的所有檔案 #include <dirent.h> #include <stdio.h> #include <string.h> int main() { &
Java 列出資料夾下所有檔案,符合條件的檔案複製到指定目錄
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class TestFile { public static void main(String[] a
c#遞迴遍歷指定資料夾內的所有檔案(包含子資料夾)
c#程式碼: public class DirectoryAllFiles { static List<FileInformation> FileList = n
python列出資料夾下所有檔案的四個方法
#方法1:使用os.listdir import os for filename in os.listdir(r'c:\windows'): print filename #方法2:使用glob模組,可以設定檔案過濾 import glob for filename in glob.glob(r'
linux c 遍歷目錄及文件
cup close logs sed mode 文件 void struct readdir #include <dirent.h>void recovery_backend() { DIR * pdir ; struct dirent *
Java實現遍歷N級樹形目錄結構
最近挺忙,一直在做專案,然後有個樹形目錄結構需要返回給前端,這裡給大家說一下實現的思路。 具體達到的效果類似: 一級目錄A: 二級目錄A: 三級目錄: 四級目錄: 檔案.txt
c++ 實現跨平臺的目錄遍歷
#ifdef _WIN32 #include <io.h> #else #include <unistd.h> #include <stdio.h> #include <dirent.h> #include <sys/s
linux C 遍歷目錄及其子目錄 opendir -> readdir -> closedir
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <dirent.h> 5 #include <sys/stat.h>
【Linux】遍歷某一目錄,判斷檔案與資料夾,main引數
LINUX下遍歷目錄的核心標頭檔案是#include <dirent.h>,方法一般是這樣的:開啟目錄->讀取->關閉目錄。相關函式有:DIR *opendir(const char *dirname);struct dirent *readdir(
linux C 遍歷目錄及其子目錄
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h&g
linux遍歷資料夾(目錄樹)nftw
http://www.cnblogs.com/harlanc/p/6991041.html #define _XOPEN_SOURCE 500 #include <ftw.h> #include <stdio.h> #include <stdl
文件目錄遍歷
pytho byte 返回 walk 遍歷文件 string Coding true 如果 os.walk() os.walk(top,topdown=True,onerror=None) top:需要遍歷的頂級目錄路徑 topdown:默認值“True”,首先返回
【滲透測試學習平臺】 web for pentester -3.目錄遍歷
線上 ini blog etc col tdi jos turn bsp Example 1 http://192.168.106.154/dirtrav/example1.php?file=../../../../../../../etc/passwd Example
這是一個定時腳本,主要功能是遍歷該文件夾下的所有文件並存儲到數組,對數據中的文件進行操作,一個一個移動到指定的目錄下,並刪除原有文件
for 定時 數據 gpo 刪除 目錄 復制文件 sleep 進行 1 #!/bin/bash 2 echo "這是一個定時腳本,主要功能是遍歷該文件夾下的所有文件並存儲到數組,對數據中的文件進行操作,一個一個移動到指定的目錄下,並刪除原有文件。" 3 j=0 4
通過proc 目錄遍歷進程
linux#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <dir
漏洞及修復——Apache/IIS目錄遍歷
php 右擊 面板 apache clas lin symlink ons options 一、Apache目錄遍歷漏洞的修復 1、在服務器端,打開Apache配置文件C:\Program Files\phpStudy\Apache\conf\httpd.conf
目錄遍歷
目錄遍歷利用服務器相關(存在安全漏洞的)應用服務,來惡意的獲取服務器上本不可訪問的文件訪問權限目錄遍歷
最新Zip壓縮文件漏洞,黑客可以觸發目錄遍歷攻擊
AS 文件覆蓋 使用 用戶添加 owas 代碼 項目 一個 人員 近日,國內某安全公司研究人員透露了一個關鍵漏洞的詳細信息,該漏洞影響了許多生態系統中的數千個項目,黑客可以利用這些漏洞在目標系統上實現代碼執行。 黑客是如何通過Zip壓縮文件入侵攻擊?被稱為“Zip