我有成千上万的目录,他们都有这种格式;
/var/www/vhosts/[USERNAME].company.com/conf/
我有一个名为x.txt的文件,它的内容应该有
[USERNAME] and some static text...
所以当我做dir / var / www / vhosts / * / conf /时,我得到了所有需要复制的文件,但是,我不知道如何抓住[USERNAME]并将其放入该文件,我需要复制。
所有的build议欢迎。 我只能在这个环境中使用shell脚本。
谢谢,
cd / var / www / vhosts && for d in * /; 做
用户= $ {d %%。*}
echo“$ user blah blah”>“$ {d} /conf/x.txt”
DONE
…应该让你几乎你想要的东西。
丹尼斯和迈克确定你在引用$ {dir}。 如果有空格的目录,这可能会导致一些问题。
echo "$user and some static text..." > "${dir}/conf/x.txt"
为了便于携带,我会使用“$ {d %%。*}”来查找用户的名字。
这是另一种方法:
cd /var/www/vhosts && find -maxdepth 1 -mindepth 1 -type d -print0 | while read -d '' -r dir do user=$(basename "$dir" .company.com) echo "$user and some static text..." > "${dir}/conf/x.txt" done