简体   繁体   English

如何使用Bash为每个子目录创建文件名

[英]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为每个子目录创建文件名?

Why 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 nullglobshopt -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

How to fix it 如何修复

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.

相关问题 使用bash循环浏览目录中的文件,然后根据文件名创建子目录 - Using bash to cycle through files in directory then create sub-directory based on file name 如何使用linux bash在每个子目录中创建相同的目录名 - how to create the same directory name inside every sub-directory with linux bash 如何使用bash为另一个文件夹中的每个目录创建一个新文件? - How to make a new file for each directory in another folder using bash? 枚举bash目录中的每个子目录 - enumerate each sub directory in a directory in bash 使用 bash 遍历目录,并为每个文件名一一做一些事情 - Loop through directory using bash and do something for each file name one by one bash在目录中创建文件,仅部分目录名是已知的 - bash create file in directory, only part of directory name is known 如何通过bash获取一个bash脚本的目录名和文件名? - How to get the Directory name and file name of a bash script by bash? Flattern Sub Dirs并使用bash在目录名前添加文件名 - Flattern sub dirs & prepend file name with dir name using bash 如何在bash中自动完成文件或目录名称? - How to autocomplete a file or directory name in bash? 使用文件中的数据创建目录名称 - create a directory name using the data in a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM