0

I have bunch of directories following this structure:

\directories\
 |---\directory1\
 |      |---file1.txt
 |      |---file2.txt
 |
 |---\directory2\
 |      |---file1.txt
 |      |---file3.txt
 |
 |---\directory3\
 |      |---file4.txt
 |      |---file2.txt

I want to merge the directories and files so it ends up like this:

\directories\
 |---\directory1\
 |      |---file1.txt
 |      |---file2.txt
 |
 |---\directory2\
 |      |---file1.txt
 |      |---file3.txt
 |
 |---\directory3\
 |      |---file4.txt
 |      |---file2.txt
 |
 |---\mergeddata\
 |      |---file1.txt (from dir1 and dir2)
 |      |---file2.txt (from dir1 and dir4)
 |      |---file3.txt (from dir2)
 |      |---file4.txt (from dir3)

I am awful with bash and been trying quite a few things but... not getting any good results.

Looking forward to some help!

4

1 回答 1

3

你没有说你希望文件如何合并,或者以什么顺序。我会猜“连接,目录1出现在目录2之前,目录2在目录3之前”?

以下脚本显示了一种简单的方法来执行此操作,而不依赖于花哨的替换:

cd directories
mkdir mergeddata
for I in directory1 directory2 directory3 ; do   # replace with your actual directory list
    for F in "$I"/* ; do
        B=$(basename "$F")
        cat "$F" >> "mergeddata/$B"
    done
done

编辑:我添加了一些引号,以防您的任何文件名以空格字符或其他不方便的空格结尾。

于 2013-08-06T13:57:09.550 回答