简体   繁体   English

替换字符串并在目录中的所有.html文件中包含日期

[英]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.

相关问题 查找字符串并替换目录和子目录中的所有文件 - Find a string and replace in all files inside a directory and sub-directory 使用sed替换目录中所有文件中的字符串 - using sed to replace a string in all files of a directory 查找并替换特定目录中所有文件和文件夹中的字符串 - Find and replace string in all files and folders in a certain directory Grep在日期范围内创建的所有文件中 - Grep inside all files created within date range 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder 在 linux 中,查找并替换目录中所有出现的 L3 到 Employee 文件中的薪水 - In linux, find and replace the salary from L3 to Employee files in all occurrences within a directory 用下划线替换目录中所有文件中的空格 - Replace spaces in all files in a directory with underscores 搜索并替换linux目录的所有文件中的文本 - Search and replace text in all files of a linux directory 如何使用目录和子目录中的所有文件中的另一个字符串模式替换字符串模式 - How to Replace a string pattern with another string pattern inside all files in directories and subdirectories 对目录中的所有文件执行命令 - Execute a command on all the files within a directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM