Unix 目录问题

Unix Directory Issue

我有多个文件夹,例如,

Folder_1
     sub_folder_1----->1.txt,2.txt,3.txt
     sub_folder_2----->2.txt,5,txt,6.txt
     sub_folder_3----->1.txt,2.txt,3.txt
     sub_folder_4----->1.txt,2.txt,3.txt
     sub_folder_5----->1.txt,2.txt,3.txt


Folder_2
     sub_folder_1----->1.txt,2.txt,3.txt
     sub_folder_2----->1.txt,2.txt,3.txt
     sub_folder_3----->1.txt,2.txt,3.txt
     sub_folder_4----->1.txt,2.txt,3.txt
     sub_folder_5----->1.txt,2.txt,3.txt

..........

每个 sub_folders_* 里面有很多 files(.txt),我需要 cat 并将所有 sub_folders 中的所有文件合并到 one unique file 中。

我尝试使用 find /home/Arun/Labs/Master -maxdepth 2 -type f -exec cat {} \;

但是没能实现

请问有什么建议吗?

如果文件夹中没有复杂的结构,您可以简单地使用带有 2 个 for 循环的 shell 脚本来实现此目的

假设:结构与题中给出的一模一样,只有一级目录,所有数据都在.txt文件中

#read_recursive.sh
for foldr in *
    do
      cd foldr
      for subf in *
      do
        cat *.txt
      done
      cd ..
    done

$ sh read_recursive.sh | uniq > final_output.txt

这个 findxargs 应该处理:

find /home/Arun/Labs/Master -maxdepth 2 -name '*.txt' -print0 | xargs -0 cat > /tmp/output.txt
cat Folder_*/*/*.txt > new_file.txt

如果您不知道您的目录树有多深:

shopt -s globstar
cat **/*.txt > new_file.txt

来自man bash

globstar: If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.