2014-12-26 30 views
1

我有一個名爲「1」包含以下文件夾:如何在目錄的子目錄中創建包含文件名的文本文件,併爲每一行添加父目錄名稱?

folder "1" 
    folder "a1" 
     file "1.txt"  
     file "2.txt" 
    folder "b2" 
     file "3.txt" 
     file "4.txt" 
     file "5.txt" 
    folder "c3" 
     file "6.txt" 
     file "7.txt" 
     file "8.txt" 
     file "9.txt" 
    file "1.txt" – just skip it, don't take it into account! 
    file "2.txt" – just skip it, don't take it into account! 

我想收集所有子目錄內的文件名,並在前面加上它的父目錄的名稱後面加上":"字符的每一行。注意:目錄「1」可能包含文件,我需要跳過它們。他們的名字不包含在result.txt

所以,我想

的Result.txt

a1:1.txt  
    a1:2.txt 
    b2:3.txt 
    b2:4.txt 
    b2:5.txt 
    c3:6.txt 
    c3:7.txt 
    c3:8.txt 
    c3:9.txt 

這可能嗎?我發現的所有命令是find命令,但我無法想象如何將其應用於此問題...注意:我正在使用Cygwin或windows下的busybox。

回答

2

您可以使用find -exec

cd 1 
find . -mindepth 2 -name "*.txt" -exec bash -c 'f="${1#./}"; echo "${f/\//:}"' - '{}' \; 
a1:1.txt 
a1:2.txt 
b2:3.txt 
b2:4.txt 
b2:5.txt 
c3:6.txt 
c3:7.txt 
c3:8.txt 
c3:9.txt 

-mindepth 2將確保不要直接下1匹配目錄裏的文件。

+0

哇。你能解釋一下'bash -c'後面的東西嗎? – helloV

+0

'bash -c'只是執行一個包含1個或多個bash命令的命令行。 – anubhava

+0

我看不到......輸出文件的名稱(「result.txt」)在哪裏? –

相關問題