![](/img/trans.png)
[英]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.