從 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%%.*}
   echo "$user blah blah" > "${d}/conf/x.txt"
完畢

……應該可以讓你得到你想要的東西。

答案2

丹尼斯和麥克確保您引用的是 ${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

相關內容