追加文字的Linux的多个文件结束 [英] Append a text to the end of multiple files in Linux
问题描述
我需要将以下code到最后追加到目录中的所有PHP文件和子目录:
I need to append the following code to the end to all the php files in a directory and its sub directory:
</div>
<div id="preloader" style="display:none;position: absolute;top: 90px;margin-left: 265px;">
<img src="ajax-loader.gif"/>
</div>
我曾尝试与
echo "my text" >> *.php
但终端显示错误:
But terminal showed the error:
bash : *.php: ambiguous redirect
我应该怎么做。有数百个文件添加此code段,这将是一个真正艰难的时间,在每个文件中添加它。先谢谢了。
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.
推荐答案
您不指定外壳,你可以试试的foreach
命令。 tcsh下(我敢肯定,一个非常类似的版本可用对于bash),你可以这样说交互:
You don't specify the shell, you could try the foreach
command. Under tcsh (and I'm sure a very similar version is available for bash) you can say something like interactively:
foreach i (*.php)
foreach> echo "my text" >> $i
foreach> end
$ I
每次将在每个文件名通过循环。
$i
will take on the name of each file each time through the loop.
像往常一样,对大量文件执行操作时,它可能是测试他们在样本文件的小目录,以确保它能正常工作是个好主意。
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 .. bash
in error message (I'll tag your question with it). The equivalent loop would be
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
而不是 *。PHP
这篇关于追加文字的Linux的多个文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!