Linux:如何在层次结构中查找具有特定扩展名的文件并在找到时执行命令?

标签 linux find

我有一个目录层次结构,其名称不遵循某种模式。例如

parent

  • bcgegec

    • hfiwehfiuwe
      • huiwwuifegeufg
      • whegwgefyfeg
    • 健康
    • chidchuehugfe
  • dedewdewf

    • 学习
    • gtgetgtg

等等。

在某些此类目录中,有一个扩展名为“gr”的文件。我需要找到每个这样的文件,cd 到它的目录并执行以 .gr 文件作为参数的“gnuplot”命令。我尝试了以下嵌套两个查找命令,但内部命令的 {} 无法按我的需要工作。外部查找应该遍历每个目录,内部查找应该查找 .gr 文件是否存在。

find $parentDir -type d -exec sh -c '(cd {} && find . -maxdepth 1 -name *.gr -exec /usr/bin/gnuplot {} \;)' \;

最佳答案

也许这就是您正在寻找的:

find . -type f -name "*.gr" -execdir /usr/bin/gnuplot {} \;

通读 man find 以获得其他有用的信息。

关于Linux:如何在层次结构中查找具有特定扩展名的文件并在找到时执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28152775/

相关文章:

Python - find() 函数的成本

linux - 从输出中删除 chkconfig header

linux - Rsync 与 .dd.xz 文件

应用程序中是否可以存在相同库(具有相同名称)的两个不同版本?

regex - Shell - 如何处理 find -regex?

bash - 如何在 bash/shell 脚本中查找不以 ".log"结尾的文件

javascript - react : "Error t.filter is not a function" or "Error: Uncaught TypeError: t.find is not a function" --> Trying to update object in array

linux - 使用 While 和 IF 进行验证

python - pynfs : error: gssapi/gssapi. h: 没有那个文件或目录

c# - 为什么 List<Object> 的 find 和 findindex 痛苦缓慢?