bash 在 Linux 中将文本附加到多个文件的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10865947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:11:07  来源:igfitidea点击:

Append a text to the end of multiple files in Linux

filebashunix

提问by Harikrishnan

I need to append the following code to the end to all the php files in a directory and its sub directory:

我需要将以下代码附加到目录及其子目录中的所有 php 文件的末尾:

</div>
<div id="preloader" style="display:none;position: absolute;top: 90px;margin-left: 265px;">
<img src="ajax-loader.gif"/>
</div>

I have tried with

我试过

echo "my text" >> *.php

But terminal showed the error:

但终端显示错误:

bash : *.php: ambiguous redirect

What should I do. there are hundreds of files to add this code segment and it would be a real tough time adding it in each file. Thanks in advance.

我该怎么办。有数百个文件要添加此代码段,将它添加到每个文件中确实很困难。提前致谢。

采纳答案by Levon

You don't specify the shell, you could try the foreachcommand. Under tcsh (and I'm sure a very similar version is available for bash) you can say something like interactively:

你没有指定shell,你可以试试这个foreach命令。在 tcsh 下(我确信有一个非常相似的版本可用于 bash)你可以交互地说出类似的话:

foreach i (*.php)
foreach> echo "my text" >> $i
foreach> end

$iwill take on the name of each file each time through the loop.

$i每次循环都会采用每个文件的名称。

As always, when doing operations on a large number of files, it's probably a good idea to test them in a small directory with sample files to make sure it works as expected.

与往常一样,在对大量文件进行操作时,最好在包含示例文件的小目录中测试它们以确保其按预期工作。

Oops .. bashin error message (I'll tag your question with it). The equivalent loop would be

哎呀..bash在错误消息中(我会用它标记你的问题)。等效循环将是

for i in *.php
do 
   echo "my text" >> $i
done

If you want to cover multiple directories below the one where you are you can specify

如果你想在你所在的目录下面覆盖多个目录,你可以指定

*/*.php

rather than *.php

而不是 *.php

回答by Dantastic

I usually use tee because I think it looks a little cleaner and it generally fits on one line.

我通常使用 T 恤,因为我觉得它看起来更干净一点,而且通常适合在一条线上。

echo "my text" | tee -a *.php

回答by ghoti

BashFAQ/056does a decent job of explaining why what you tried doesn't work. Have a look.

BashFAQ/056很好地解释了为什么您尝试的方法不起作用。看一看。

Since you're using bash (according to your error), the forcommand is your friend.

由于您使用的是 bash(根据您的错误),因此该for命令是您的朋友。

for filename in *.php; do
  echo "text" >> "$filename"
done

If you'd like to pull "text" from a file, you could instead do this:

如果您想从文件中提取“文本”,您可以这样做:

for filename in *.php; do
  cat /path/to/sourcefile >> "$filename"
done

Now ... you mighthave files in subdirectories. If so, you could use the findcommand to find and process them:

现在...您可能在子目录中有文件。如果是这样,您可以使用该find命令来查找和处理它们:

find . -name "*.php" -type f -exec sh -c "cat /path/to/sourcefile >> {}" \;

The findcommand identifies what files using conditions like -nameand -type, then the -execcommand runs basically the same thing I showed you in the previous "for" loop. The final \;indicates to findthat this is the end of arguments to the -execoption.

find命令使用-name和 等条件标识哪些文件-type,然后该-exec命令运行的内容与我在之前的“for”循环中向您展示的内容基本相同。final\;表示find这是-exec选项参数的结尾。

You can man findfor lots more details about this.

您可以man find了解更多有关此的详细信息。

The findcommand is portable and is generally recommended for this kind of activity especially if you want your solution to be portable to other systems. But since you're currently using bash, you may also be able to handle subdirectories using bash's globstaroption:

find命令是可移植的,通常建议用于此类活动,特别是如果您希望您的解决方案可移植到其他系统。但是由于您当前正在使用bash,您也可以使用 bash 的globstar选项处理子目录:

shopt -s globstar
for filename in **/*.php; do
  cat /path/to/sourcefile >> "$filename"
done

You can man bashand search for "globstar" for more details about this. This option requires bash version 4 or higher.

您可以man bash搜索“globstar”以获取更多详细信息。此选项需要 bash 版本 4 或更高版本。

NOTE: You may have other problems with what you're doing. PHP scripts don't needto end with a ?>, so you might be adding HTML that the script will try to interpret as PHP code.

注意:您在做的事情可能还有其他问题。PHP 脚本不需要以 结尾?>,因此您可能会添加脚本将尝试解释为 PHP 代码的 HTML。

回答by Duc Pham

You can use sedcombined with find. Assume your project tree is

您可以sed结合使用find。假设您的项目树是

    /MyProject/ 
    /MyProject/Page1/file.php
    /MyProject/Page2/file.php
    etc.        
  1. Save the code you want to append on /MyProject/. Call it append.txt
  2. From /MyProject/run:

    find . -name "*.php" -print | xargs sed -i '$r append.txt'
    

    Explain:

    • finddoes as it is, it looks for all .php, including subdirectories
    • xargswill pass (i.e. run) sedfor all .php that have just been found
    • sedwill do the appending. '$r append.txt' means go to the end of the file ($) and write (paste) whatever is in append.txt there. Don't forget -iotherwise it will just print out the appended file and not save it.
  1. 保存要附加的代码/MyProject/。称它为append.txt
  2. /MyProject/运行:

    find . -name "*.php" -print | xargs sed -i '$r append.txt'
    

    解释:

    • find照原样,它会查找所有 .php,包括子目录
    • xargs将通过(即运行)sed所有刚刚找到的 .php
    • sed会做追加。'$r append.txt' 意味着转到文件的末尾($)并写入(粘贴)append.txt 中的任何内容。不要忘记,-i否则它只会打印出附加的文件而不保存它。

Source: http://www.grymtheitroade.com/unix/Sed.html#uh-37

来源:http: //www.grymtheitroade.com/unix/Sed.html#uh-37

回答by Elftheitroados

You can do (Work even if there's space in your file path) :

您可以这样做(即使文件路径中有空间也可以工作):

#!/bin/bash

# Create a tempory file named /tmp/end_of_my_php.txt
cat << EOF > /tmp/end_of_my_php.txt
</div>
<div id="preloader" style="display:none;position: absolute;top: 90px;margin-left: 265px;">
<img src="ajax-loader.gif"/>
</div>
EOF

find . -type f -name "*.php" | while read the_file
do
    echo "Processing $the_file"
    #cp "$the_file" "${the_file}.bak" # Uncomment if you want to save a backup of your file
    cat /tmp/end_of_my_php.txt >> "$the_file"
done

echo
echo done

PS: You must run the script from the directory you want to browse

PS:必须从要浏览的目录运行脚本

回答by Khaled AbuShqear

Inspired from @Dantastic answer :

灵感来自@Dantastic 答案:

echo "my text" | tee -a file1.txt | tee -a file2.txt