从 shell 创建数千个文件

从 shell 创建数千个文件

我有数千个目录,它们都具有这种格式;

/var/www/vhosts/[USERNAME].company.com/conf/

我有一个名为 x.txt 的文件,它的内容应该有

[USERNAME] and some static text...

因此,当我执行 dir /var/www/vhosts/*/conf/ 时,我获得了需要复制文件的所有目录,但是,我不知道如何获取 [USERNAME] 并将其放入我需要复制的文件中。

欢迎提出所有建议。我只能在这个环境中使用 shell 脚本。

谢谢,

答案1

cd /var/www/vhosts && for d in */; 执行
   用户=${d%%.*}
   回显“$user blah blah”>“${d}/conf/x.txt”
完毕

... 应该可以帮您得到您想要的东西。

答案2

Dennis 和 Mike 确保你引用了 ${dir}。如果任何目录有空格,这可能会导致一些问题。

echo "$user and some static text..." > "${dir}/conf/x.txt"

为了便于移植,我会使用“${d%%.*}”来查找用户的姓名。

答案3

这是另一种方法:

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

相关内容