整天不停地搜索文件?批处理文件?

时间:2014-02-19 10:39:19

标签: node.js bash

我想知道如何实现这一目标?我想不断循环遍历一个目录,如果有文件存在,删除它?

我可以在bash脚本中执行此操作,还是需要使用nodejs之类的东西?

谢谢!

2 个答案:

答案 0 :(得分:1)

这是您要搜索的内容:

首先:

1)您告诉脚本搜索某些文件,如果存在,请删除它们

您可以输入下面的脚本文件:

#!/bin/bash
LIST_FILE="
/path/to/file1
/path/to/file2
/path/to/file3
/path/to/file4
"
for file in $LIST_FILE
do
  if [ -a $file ];then
     rm -rf $file
     echo $file is removed
  fi
done

2)删除find命令

创建的所有文件
find /path/to/files >> /path/to/LIST

然后运行脚本并通过说./script.sh

来调用它
#!/bin/bash
LIST_FILE=/path/to/LIST
for file in $LIST_FILE
do
  if [ -a $file ];then
     rm -rf $file
     echo $file is removed
  fi
done

答案 1 :(得分:1)

你可以在寻找文件时找到一个循环的脚本:

#!/bin/bash

SLEEP_SECS=5
TARGET_FILE="filename"

while [ 1 ]; do
  find /path/to/file -type f -name "$TARGET_FILE" -exec rm {} \;
  sleep $SLEEP_SECS
done

TARGET_FILE可以更改为通配符,例如“* .JPG”。这将遍历/path/to/file下的所有目录以查找文件。在此示例中,脚本将每5秒运行一次。