[英]How to create a file name for each sub directory using Bash
I have a folder structure like this: 我有这样的文件夹结构:
.
+-- dir
| +-- subdir1
| +-- subdir2
| +-- subdir3
| +-- subdir4
And I want to create an index.html
file inside each subdir
using Bash: 我想使用Bash在每个
subdir
创建一个index.html
文件:
.
+-- dir
| +-- subdir1
| +-- index.html
| +-- subdir2
| +-- index.html
| +-- subdir3
| +-- index.html
| +-- subdir4
| +-- index.html
I can list all of sub directories by using: 我可以使用以下命令列出所有子目录:
.
$ ls dir/*/
And create a file with touch
, then I thought that the correct command would be: 并创建一个带有
touch
的文件,然后我认为正确的命令将是:
.
$ touch dir/*/index.html
The result: touch: cannot touch 'dir/*/index.html': No such file or directory
结果:
touch: cannot touch 'dir/*/index.html': No such file or directory
How to create a file name for each sub directory using Bash? 如何使用Bash为每个子目录创建文件名?
touch dir/*/index.html
doesn't work touch dir/*/index.html
不起作用 Globs expand only to things that actually exist , or to the glob expression itself if no matches exist and shell options are all at default configuration. Glob 仅扩展到实际存在的东西 ,或者扩展到glob表达式本身(如果不存在匹配项且shell选项全部为默认配置)。 (It's possible to configure this scenario to result in no arguments being generated or in an error condition in bash, using
shopt -s nullglob
or shopt -s failglob
). (可以使用
shopt -s nullglob
或shopt -s failglob
配置此方案以导致不生成参数或在bash中产生错误情况)。
Thus, if you have dir/subdir1/index.html
, but dir/subdir2/
with no index.html
in it, dir/*/index.html
will expand to only contain dir/subdir1/index.html
, but not any reference at all to subdir2
. 因此,如果您具有
dir/subdir1/index.html
,但其中dir/subdir2/
中没有index.html
,则dir/*/index.html
将扩展为仅包含dir/subdir1/index.html
,但不包含任何引用完全到subdir2
。
Glob to the things that exist -- the directories -- and tack on the things that don't yourself: 浏览存在的内容(目录),然后处理不属于您自己的内容:
for d in dir/*/; do touch -- "$d/index.html"; done
The --
is needed for the corner case where one of your directories has a name starting with a dash: You don't want -dirname
to be treated as an option to touch
; 在其中一个目录的名称以短划线开头的特殊情况下,需要使用
--
:您不希望将-dirname
视为touch
的选项; --
indicates that all future arguments are treated as positional, preventing this circumstance. --
表示所有将来的论点都被视为位置论,从而避免了这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.