[英]Replace string and include date inside all .html files within a directory
I am looking for bash code to replace all <title>XXX</title>
within all .html files to <title>XXX - YYY $Date</title>
我正在寻找bash代码将所有.html文件中的所有
<title>XXX</title>
替换为<title>XXX - YYY $Date</title>
What I came up with, using sed is: 我想出的是,使用sed是:
sed -i "s/</title>/ - YYY 'date'/g" /home/mirror/*
This however, doesn't implement the .html requirement and I can't get it to work, I'm either getting 然而,这并没有实现.html要求,我无法让它工作,我要么得到
sed: no imput files
or 要么
-bash: s/</title>/ - YYY 'date'/g: No such file or directory
The structure is: 结构是:
/home/mirror/
├── images
├── archive
│ └── index.html
│ └── 2.html
├── index.html
├── 2.html
├── style.css
└── scripts.js
How can I do this? 我怎样才能做到这一点?
You need find
to recurse through the directories: 你需要
find
通过目录递归:
find /home/mirror -name '*.html' -exec sed -i "s@</title>@ - YYY $Date</title>@g" {} +
Note I replaced "'date'" with "$Date" as that's what you said you wanted to add. 注意我用“$ Date”替换了“'date'”,因为这就是你想要添加的内容。 I also use
@
instead of /
as the sed s
separator character, to avoid interference from the </title>
parts. 我还使用
@
而不是/
作为sed s
分隔符,以避免来自</title>
部分的干扰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.