Umur Can Gürelli
Umur Can Gürelli

Reputation: 23

How to run a command recursively for files with specific extensions in bash?

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

Here is the command

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

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

Upvotes: 2

Views: 1565

Answers (1)

Attie
Attie

Reputation: 6979

Try:

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

Explanation:

  • 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).

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).

Upvotes: 5

Related Questions