多个文件的Gnu / LinuxBash批量命令

问题描述

我正在寻找一种解决方案,可以同时运行多个文件的命令。

仅用于1个文件的示例:

dot -Tpng dummy.dot.modulename -o dummy.dot.modulename.png

现在所有“ .dot ”(伪指令)的示例

dot -Tpng $(find . -name "*.dot*") -o $(find . -name "*.dot*").png

解决方法

在* .dot文件遍历模式上循环

#!/usr/bin/env bash

for dotfile in *.dot
do
  # Create the png file name by removing the trailing .dot
  # and replacing it with .png
  pngfile="${dotfile%.dot}.png"
  dot -Tpng "$dotfile" -o "$pngfile"
done

而不是循环遍历文件名的扩展。您可以让find执行如下命令/脚本:

find . -name '*.dot' \
  -execdir sh 'for f; do dot -Tpng "$f" -o "${f%.dot}.png" ;done' {} \;