将一个文件分类到另一个目录中的同名文件

Cat one file onto same named file in another directory

我正在尝试将 *.txt 的内容放到另一个目录中的另一个同名文件中。例如。 ../../*.txt

我试过:

find . -type f -name "*.txt" -exec cat {} \; >> ../../*.txt

还有一些变体,但最终会出现模糊的重定向错误或什么也没有。

我在这里错过了什么?

对于 1 个文件:

cat ./file1.txt >> ../../file1.txt

您的问题建议使用 1 个文件,但您的 find 命令建议使用多个 *.txt

类型的文件

要执行 *.txt 类型的多个文件并根据您的 find 命令尝试:

find . -name "*.txt" -print0 | while read -d $'[=11=]' filename
do
  cat ./$filename >> ../../$filename
done

* 不进行一对一映射。它将被扩展 bash 以表示 ../../ 目录中的所有 txt 文件。这是导致错误的原因,因为您现在正尝试重定向到多个文件。

使用 for 循环比查找更容易,因为您需要引用文件名两次。

for file in *.txt
do
    if [ -f ./$file ] ; then 
        cat ./$file >> ../../$file
    fi
done

cat the contents of *.txt onto another file with the same name

本质上,您是在此处复制文件。是吗?

所以下面的东西应该适合你。

find . -type f -iname "*.txt" -exec cp bash -c 'cp "" ../../""' _ {} \;