【问题标题】:Linux Find and executeLinux 查找并执行
【发布时间】:2013-09-04 13:31:41
【问题描述】:

我需要编写一个 Linux 脚本,它执行以下操作: 从名为 ftpuser 的目录中查找所有 .ear 和 .war 文件,甚至包括将出现在那里的新文件,然后执行一个生成一些报告的命令。当命令完成后,这些文件需要移动到另一个目录。

下面我想我已经找到了命令的启动方式。我的问题是有谁知道如何在目录中找到新条目然后执行命令以便我可以得到报告?

find /directory -type f -name "*.ear" -or -type f -name "*.war" 

【问题讨论】:

  • 您所说的“……即使是即将出现的新的……”是什么意思? find 只能找到当前目录中的文件,没有机会看到未来会带来什么。

标签: bash find


【解决方案1】:

您似乎希望脚本无限期地运行。循环您find 的文件以执行所需的操作:

while : ; do
  for file in $(find /directory -type f -name "*.[ew]ar"); do
    some_command "${file}"           # Execute some command for the file
    mv "${file}" /target/directory/  # Move the file to some directory
  done
  sleep 60s                          # Maybe sleep for a while before searching again!
done

【讨论】:

  • 非常感谢 devnull!但我仍然有一些问题。如果可以的话,它部分有效,请查看这篇文章。 link
【解决方案2】:

这也可能有帮助:Monitor Directory for Changes

如果它不是时间紧迫的,但您不愿意在每次重新启动后手动启动脚本(如 devnull 建议的)或其他什么,我建议使用 cron-job。

你可以设置一个工作

crontab -e

并像这样附加一行:

* * * * * /usr/bin/find /some/path/ftpuser/ -type f -name "*.[ew]ar" -exec sh -c '/path/to/your_command $1 && mv $1 /target_dir/' _ {} \;

这会每分钟运行一次搜索。您可以更改时间间隔,请参阅https://en.wikipedia.org/wiki/Cron 了解概览。 && 导致仅当 your_command 成功时才执行移动。您可以通过手动运行来检查,然后

echo $?

0 表示真或成功。欲了解更多信息,请参阅http://tldp.org/LDP/abs/html/exit-status.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2021-12-11
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
相关资源
最近更新 更多