Bash 将一个文件放到另一个目录中的同一个命名文件上

Bash 将一个文件放到另一个目录中的同一个命名文件上,bash,unix,find,cat,Bash,Unix,Find,Cat,我正在尝试将*.txt的内容cat到另一个目录中具有相同名称的另一个文件中。例如,。/../*.txt 我试过: find . -type f -name "*.txt" -exec cat {} \; >> ../../*.txt 和它的一些变化,但最终与一个模糊的重定向错误或什么都没有 我在这里遗漏了什么?对于1个文件: cat ./file1.txt >> ../../file1.txt 您的问题建议使用1个文件,但您的find命令建议使用许多*.txt 要执行

我正在尝试将*.txt的内容cat到另一个目录中具有相同名称的另一个文件中。例如,
。/../*.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 $'\0' filename
do
  cat ./$filename >> ../../$filename
done
对于1文件:

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

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

find . -name "*.txt" -print0 | while read -d $'\0' filename
do
  cat ./$filename >> ../../$filename
done

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

使用for循环比使用find更容易做到这一点,因为您需要两次引用文件名

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

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

使用for循环比使用find更容易做到这一点,因为您需要两次引用文件名

for file in *.txt
do
    if [ -f ./$file ] ; then 
        cat ./$file >> ../../$file
    fi
done
将*.txt的内容放到另一个同名文件中

基本上你是在这里复制文件。你不是吗

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

find . -type f -iname "*.txt" -exec cp bash -c 'cp "$1" ../../"$1"' _ {} \;
将*.txt的内容放到另一个同名文件中

基本上你是在这里复制文件。你不是吗

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

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