如何为bash中具有特定扩展名的文件递归运行命令? [英] How to run a command recursively for files with specific extensions in bash?

查看:42
本文介绍了如何为bash中具有特定扩展名的文件递归运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从根文件夹递归运行命令,这将影响该根文件夹下具有特定扩展名的所有文件.

I want to run a command from root folder recursively which will affect all files under that root folder with a specific extension.

这是命令

blender b03.blend --background --python myScript.py

我想对每个带有.blend扩展名的文件运行b03.blend文件.

Instead of b03.blend file I want to run this command for every file with .blend extension.

推荐答案

尝试:

find . -name '*.blend' -exec blender {} --background --python myScript.py \;

说明:

  • 查找 *.blend 匹配的任何文件-使用单引号很重要,这样您的外壳程序就不会为当前目录中的任何文件扩展此文件
  • 然后对于找到的每个文件,执行 blender {} --background --python myScript.py . {} 替换为文件名,例如 some/sub/tree/file.blend .
  • \; 将终止 -exec 语句. \ 用于对; 进行转义,以便外壳程序不吸收它,并将行拆分为多个命令.
  • 所有 blender 实例将从初始工作目录运行,因此 blender 必须能够正确处理非"here"文件,并具有同等访问权限不是 *.blend 文件的邻居的 myScript.py .
  • find any file that matches *.blend - use of single quotes is important so that your shell doesn't expand this for any files in the current directory
  • then for each file found, execute blender {} --background --python myScript.py. The {} is replaced with the file's name, for example some/sub/tree/file.blend.
  • \; will terminate the -exec statement. The \ is used to escape the ;, so that the shell doesn't absorb it and split the line into multiple commands.
  • All instances of blender will be run from the initial working directory, so blender must be able to correctly handle a file that is not 'here', and equally access the myScript.py that is not a neighbour of the *.blend file(s).

注意:我对 blend 不熟悉,但是如果它进入后台并需要一些时间来处理(由于-background),那么您可能不堪重负……您可能更愿意一次处理一个文件,或者将作业分解为几个可以串行运行的独立作业(每个项目都可以完成)...要获得加分,请查看类似 make 的东西,该东西可以轻松地在限制条件下运行并发操作(例如: make -j 8 最多可以运行 8 >一次执行命令.

NOTE: I'm not familiar with blend, but if it will go into the background and take some time to process (due to --background), then you may overwhelm your machine... You might prefer to process one file at a time, or perhaps break the job into a handful of independent jobs that can run serially (each item to completion)... for bonus points look at something like make that can easily run concurrent operations while working with restrictions (e.g: make -j 8 will run at most 8 commands at once).

这篇关于如何为bash中具有特定扩展名的文件递归运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆