【问题标题】:check directory content every 2 second and copy its content if some file exists there in bash每 2 秒检查一次目录内容,如果 bash 中存在某个文件,则复制其内容
【发布时间】:2012-04-30 10:18:08
【问题描述】:

我想每 2 秒检查一次 linux bash 中的特定目录,如果存在任何文件,则在运行的守护进程劫持该文件之前将该文件复制到该目录(对该文件进行一些处理和修改并快速将处理后的文件移动到另一个目录)。

我试过了:

while true; do
  cp * /home/a/*;
  sleep 1;
done

但它不起作用:我想我的cp 语法一定是错误的。我尝试了/home/a/*/home/a/home/a/,但都没有奏效。

有什么方法可以在 bash shell 中做到这一点?

【问题讨论】:

    标签: linux bash shell daemon infinite-loop


    【解决方案1】:

    您快到了,但您需要考虑该目录中没有文件的情况。

    使用bash 中的正确选项,这相当容易:

    #! /bin/bash
    
    shopt -s nullglob # to get an empty list if no file is present
    
    while true ; do
        for i in * ; do
            echo "Intercepted $i"
            cp "$i" /home/a/
        done
        sleep 1
    done
    

    (请注意,这通常是非常不可靠的。您可以复制部分写入的文件、完全丢失文件等)

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 2019-08-20
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多