實現Linux tree命令

2021-08-26 03:23:59 字數 646 閱讀 2669

linux下有這樣乙個命令,可以把當前目錄下的所有檔案和子檔案以tree的方式顯示出來,看下效果

[crazybaby@localhost test]$ tree . |-- a |-- b |-- c `-- test2 |-- d |-- e `-- f 3 directories, 4 files [crazybaby@localhost test]$

自己用遞迴方式用c實現了下,效果如下:

[crazybaby@localhost test]$ ./a.out ./test a a.out b c +test2 f +d +e [crazybaby@localhost test]$

這裡+號表示directory.

下面是原始碼:

#include #include #include #include using namespace std; int showconsoledir(char* path, int cntfloor) char tname[10000]; memset(tname, 0, 10000); snprintf(tname,sizeof(tname),"%s/%s",path,dir_ent->d_name); if ((dir_child = opendir(tname))!=null) cout<<"+"

Linux tree命令 簡單實現

tree命令,將目錄檔案以樹的形式呈現 系統的tree命令效果 模擬實現tree命令 include include include include include void print tree const char dirname,int indent int depth indent whil...

Linux tree 命令用法

a 顯示所有檔案和目錄。a 使用asni繪圖字元顯示樹狀圖而非以ascii字元組合。c 在檔案和目錄清單加上色彩,便於區分各種型別。d 顯示目錄名稱而非內容。d 列出檔案或目錄的更改時間。f 在每個檔案或目錄之前,顯示完整的相對路徑名稱。f 在執行檔案,目錄,socket,符號連線,管道名稱名稱,各...

Linux Tree命令詳解

中文翻譯 a 顯示所有檔案和目錄。a 使用asni繪圖字元顯示樹狀圖而非以ascii字元組合。c 在檔案和目錄清單加上色彩,便於區分各種型別。d 顯示目錄名稱而非內容。d 列出檔案或目錄的更改時間。f 在每個檔案或目錄之前,顯示完整的相對路徑名稱。f 在執行檔案,目錄,socket,符號連線,管道名...