如何在 bash 的每个子目录中创建相同的目录?
我一直在尝试在“example”的所有子目录中创建目录“a”。
我尝试的是以下内容:
mkdir example/*/a
但它不将 '*' 作为通配符,返回 mkdir: 无法创建 «example/*/a» 目录: 它不存在文件或目录
请注意,我有太多示例中的子目录,所以我无法通过创建列表来做到这一点。
我不想用 for 循环来做到这一点,因为我正在学习 bash 并且不允许我使用循环。也许,没有办法避免 for 循环,在这种情况下,请告诉我:)。
谢谢。
I've been trying to create the directory "a" inside all the subdirectories of "example".
What I tried is the following:
mkdir example/*/a
But it doesn't take the '*' as a wildcard, returning mkdir: can not create «example/*/a» directory: It doesn't exist file or directory
Note that I have too much subdirectories in example, so I can't do it by creating a list.
I don't want to do it with for loops, because I'm learning bash and I'm not allowed to use loops. Probably, there is no way to avoid a for loop, in that case, please tell me :).
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
dirs=( example/*/ )
创建一个包含example
的所有子目录的数组 ((example/dir1/ example/dir2/ ...)
)。"${dirs[@]/%/a}"
扩展为包含dirs
的所有元素的列表,并在每个元素后附加一个a
其中((example/dir1/a example/dir2/a ...)
)。如果您有非常大量的子目录,上面代码中的
mkdir
可能会失败,并出现“参数列表太长”错误。请参阅rm、cp、mv 命令的参数列表太长错误。避免该问题的一种方法是将要创建的以 NUL 结尾的目录列表传递给 xargs -0:
如果需要,这将运行多个 mkdir 命令,每个都有一个不超过限制的参数列表。
但是,如果目录数量那么,您可能会遇到性能问题,因为 Bash (不必要但不可避免)对
example/*/
生成的列表进行排序。在这种情况下,最好使用find
。一种方法是:find
通过-exec ... +
内置了对xargs
行为的支持。然而,在这种情况下使用起来有点麻烦。我能很快想到的最好办法是:因为它在运行之前将
/a
添加到由find
传递给bash
的所有参数中mkdir
在它们上,它可能会遇到“参数列表太长”错误。我将其留在这里,因为它说明了一种有时有用的技术,但我建议不要使用它。Try:
dirs=( example/*/ )
creates an array of all the subdirectories ofexample
((example/dir1/ example/dir2/ ...)
)."${dirs[@]/%/a}"
expands to a list containing all the elements ofdirs
with ana
appended to each of them ((example/dir1/a example/dir2/a ...)
).If you have a very large number of subdirectories the
mkdir
in the code above may fail with an "Argument list too long" error. See Argument list too long error for rm, cp, mv commands.One way to avoid the problem is to pass a NUL-terminated list of the directories to be created to
xargs -0
:That will, if necessary, run multiple
mkdir
commands, each with an argument list that does not exceed the limit.However, if the number of directories is that large you may run into performance problems due to Bash (unnecessarily, but unavoidably) sorting the list produced by
example/*/
). In that case it would be better to usefind
. One way to do it is:find
has built-in support forxargs
-like behaviour with-exec ... +
. However, it's a bit fiddly to use in this case. The best I can come up with in a hurry is:Since it adds
/a
to all of the arguments passed tobash
byfind
before runningmkdir
on them, it may encounter an "Argument list too long" error anyway. I'll leave it here because it illustrates a sometimes-useful technique, but I recommend against using it.您可以使用
find
:You can use
find
:只是:
Just:
我认为只有当您已经知道
example
下的子目录时,才能避免循环,例如:否则,请使用 for 循环来救援:
注意:添加
-v
到mkdir
如果您希望它显示创建的目录。I think you can only avoid a loop if you already know the subdirectories under
example
, e.g.:Otherwise, use a for loop to the rescue:
Note: add a
-v
tomkdir
if you want it to show you the directories that get created.