简体   繁体   中英

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>

What I came up with, using sed is:

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

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 /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. I also use @ instead of / as the sed s separator character, to avoid interference from the </title> parts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM