在每个文件夹Unix中合并文件(cat) [英] Merging files (cat) in each-folder Unix
本文介绍了在每个文件夹Unix中合并文件(cat)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的主文件夹中,我有多个子文件夹,并且每个子文件夹都包含多个文件.我想将这些文件合并到每个子文件夹中.
Inside my main folder, I have multiple sub-folders and each sub folder contains multiple files. I want to merge these files in every sub-folder.
所以我正在尝试做这样的事情:
So I am trying to do something like this:
cd ../master-folder
for file in $( find . -name "*.txt" );
do
cat "all the text files in this sub folder" > "name of the subfolder.txt"
rm "all the previous text files excluding the merged output obviously"
done
感谢帮助!谢谢.
推荐答案
如果文件的顺序无关紧要,我会这样做:
I would do it like this, if the order of the files doesn't matter :
for i in $(find -maxdepth 1 -mindepth 1 -type d)
do
find $i -name '*.txt' -type f -exec cat {} >> $i-list.txt \;
find $i -name '*.txt' -type f -exec rm {} \;
done
第一个发现是寻找子目录.
The first find looks for subdirectories.
第二个将所有子文件的内容附加到文件中
The second one appends all subfile's content to a file
第三个删除子文件.
如果存在递归子目录,则此方法不起作用.如果需要,请删除"-maxdepth 1"
This doesn't work if there are recursive subdirectories. If you want this, remove '-maxdepth 1'
这篇关于在每个文件夹Unix中合并文件(cat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文