[英]Using bash to cycle through files in directory then create sub-directory based on file name
[英]How to create a file name for each sub directory using Bash
我有這樣的文件夾結構:
.
+-- dir
| +-- subdir1
| +-- subdir2
| +-- subdir3
| +-- subdir4
我想使用Bash在每個subdir
創建一個index.html
文件:
.
+-- dir
| +-- subdir1
| +-- index.html
| +-- subdir2
| +-- index.html
| +-- subdir3
| +-- index.html
| +-- subdir4
| +-- index.html
我可以使用以下命令列出所有子目錄:
.
$ ls dir/*/
並創建一個帶有touch
的文件,然后我認為正確的命令將是:
.
$ touch dir/*/index.html
結果: touch: cannot touch 'dir/*/index.html': No such file or directory
如何使用Bash為每個子目錄創建文件名?
touch dir/*/index.html
不起作用 Glob 僅擴展到實際存在的東西 ,或者擴展到glob表達式本身(如果不存在匹配項且shell選項全部為默認配置)。 (可以使用shopt -s nullglob
或shopt -s failglob
配置此方案以導致不生成參數或在bash中產生錯誤情況)。
因此,如果您具有dir/subdir1/index.html
,但其中dir/subdir2/
中沒有index.html
,則dir/*/index.html
將擴展為僅包含dir/subdir1/index.html
,但不包含任何引用完全到subdir2
。
瀏覽存在的內容(目錄),然后處理不屬於您自己的內容:
for d in dir/*/; do touch -- "$d/index.html"; done
在其中一個目錄的名稱以短划線開頭的特殊情況下,需要使用--
:您不希望將-dirname
視為touch
的選項; --
表示所有將來的論點都被視為位置論,從而避免了這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.