简体   繁体   English

将文本附加到特定文件夹下的所有文件

[英]Append a text to all files under specific folder

I need to append text to the bottom of the files into .htaccess files under /home directory for all of my client's website.我需要将文本附加到文件的底部到我所有客户网站的 /home 目录下的 .htaccess 文件中。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}`

I already tried to echo 'code' >> .htaccess and failed because it contains new line, I've also tried \\n or \\r without success and I'm using printf ;我已经尝试echo 'code' >> .htaccess并失败,因为它包含新行,我也尝试过\\n\\r没有成功,我正在使用printf I can add new line but it says我可以添加新行,但它说

bash: printf: `}': invalid format character

Update..更新..

Im successfully fix the invalid format character by following tripleee code but unfortunately it not append all of my .htaccess files我通过遵循三元组代码成功修复了无效格式字符,但不幸的是它没有附加我所有的 .htaccess 文件

This is the code这是代码

for file in /home/*/public_html/.htaccess; do
    printf '%s\n' '# Redirect to https' \
        'RewriteEngine On' \
        'RewriteCond %{HTTPS} off' \
        'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' >>".htaccess"
done

Im able to resolved the issue by using these code我能够通过使用这些代码解决问题

printf '%s\n' '# Redirect to https' \
        'RewriteEngine On' \
        'RewriteCond %{HTTPS} off' \
        'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}'  |
tee -a /home/*/public_html/.htaccess

Thanks to tripleee感谢三重奏

You need to double the % to produce a literal per-cent character in a printf format string.您需要将%加倍以在printf格式字符串中生成文字百分比字符。 Or, pass an explicit format string to printf and pass the individual lines as arguments.或者,将显式格式字符串传递给printf并将各个行作为参数传递。

printf '%s\n' 'RewriteEngine On' \
        'RewriteCond %{HTTPS} off' \
        'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' |
tee -a /home/*/public_html/.htaccess >/dev/null

There's no reason you couldn't use echo too, though it's slightly hard on the eyes.没有理由您也不能使用echo ,尽管它对眼睛来说有点困难。

echo 'RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' |
tee -a /home/*/public_html/.htaccess >/dev/null

You can get something similar with echo -e but it's less portable and less elegant than printf .你可以用echo -e得到类似的东西,但它不如printf便携和优雅。

You could also simply use a here document:您也可以简单地使用此处的文档:

tee -a /home/*/public_html/.htaccess<<-'____HERE' >/dev/null
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
____HERE

Putting a dash (minus) before the here-document separator allows you to use tabs (but not spaces!) for indentation;在此处文档分隔符之前放置一个破折号(减号)允许您使用制表符(但不能使用空格!)进行缩进; the leading tabs will be stripped from the text.前导标签将从文本中删除。 Putting quotes around the separator causes the shell to quote the document (ie no dollar signs or backticks in the document will be evaluated).在分隔符周围加上引号会导致 shell 引用文档(即不会评估文档中的美元符号或反引号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在特定文件夹下查找 Soalris 中的硬链接文件 - Finding files that are Hardlink in Soalris under specific folder 在文件夹的所有文件中追加新行 - Append new line in all files of the folder 如何在Linux的文件夹中的所有文件上附加和添加一些文本 - How can I append and prepend some text to all files in a folder in Linux 将文件夹中的所有.tgz 文件解压缩到特定文件夹 - unzipping all .tgz files in folder to a specific folder 如何使用根目录中存在的所有目录下特定文件夹中的Shell脚本列出所有文件? - How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory? Linux:如何删除文件夹中除具有特定名称的文件之外的所有文件? - Linux: how to remove all the files in a folder except ones with specific names? 如何从文件夹中的所有文件中删除带有特定字符串的行 - How to remove from all files in folder the lines with specific string 查找所有文件并将特定文件解压缩到本地文件夹 - Find all files and unzip specific file to local folder 将文件夹中的所有pdf转换为文本并处理这些txt文件 - transform all the pdfs in a folder to text and process these txt files 比较文件夹中的所有文件 - Compare all files in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM