如何在目录的子目录中创建包含文件名的文本文件,并在每行之前添加父目录名?

时间:2014-12-26 07:15:11

标签: shell unix command-line awk

我有一个名为" 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!

我想收集所有子目录中的文件名,并在每行前面加上其父目录名,后跟":" char。注意:目录" 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命令,但我无法想象如何将其应用于此问题...注意:我在Windows下使用Cygwin或busybox。

1 个答案:

答案 0 :(得分: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目录下的文件。