Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在shell脚本中每隔2分钟查找一个目录中是否有新文件?_Bash_Shell_Unix - Fatal编程技术网

Bash 如何在shell脚本中每隔2分钟查找一个目录中是否有新文件?

Bash 如何在shell脚本中每隔2分钟查找一个目录中是否有新文件?,bash,shell,unix,Bash,Shell,Unix,我有一个名为/home/user/local的目录。每隔两分钟左右,就会有一个新文件转储到此目录中。我需要每2分钟检查一次这个目录,看看是否有一个新的文件在那里。如果有新文件,我需要将它的列表放入一个变量中,以便以后使用。如何执行此shell脚本 inotifywait正是您想要的:设置一个cron作业,该作业运行一个脚本,该脚本将当前列表与存储在某个文件中的旧列表进行比较。#/usr/bin/env bash #! /usr/bin/env bash FILELIST=/tmp/fileli

我有一个名为
/home/user/local
的目录。每隔两分钟左右,就会有一个新文件转储到此目录中。我需要每2分钟检查一次这个目录,看看是否有一个新的文件在那里。如果有新文件,我需要将它的列表放入一个变量中,以便以后使用。如何执行此shell脚本

inotifywait
正是您想要的:

设置一个cron作业,该作业运行一个脚本,该脚本将当前列表与存储在某个文件中的旧列表进行比较。

#/usr/bin/env bash
#! /usr/bin/env bash

FILELIST=/tmp/filelist
MONITOR_DIR=/home/usr/local

[[ -f ${FILELIST} ]] || ls ${MONITOR_DIR} > ${FILELIST}

while : ; do
    cur_files=$(ls ${MONITOR_DIR})
    diff <(cat ${FILELIST}) <(echo $cur_files) || \
         { echo "Alert: ${MONITOR_DIR} changed" ;
           # Overwrite file list with the new one.
           echo $cur_files > ${FILELIST} ;
         }

    echo "Waiting for changes."
    sleep $(expr 60 \* 2)
done
FILELIST=/tmp/FILELIST 监视器\u DIR=/home/usr/local [[-f${FILELIST}]| | ls${MONITOR\u DIR}>${FILELIST} while:;做 cur\u files=$(ls${MONITOR\u DIR})
diff假设您的变量名为$listOF

创建脚本:

listOfFiles=`ls -t /home/user/local`
for i in $listOfFiles
do
echo "$listOF" | grep $i
if [[ "$?" -eq "0" ]]
then
listOF=$listOF" "$i
fi
done
并通过cmd:crontab-e将此脚本放入crontab 格式:

1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * * <path to your script>
1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59****

首先,我要向利菲表示衷心的感谢,我希望事情就这么简单。然而,对于我的设备来说,为循环编写临时文件对于我当前的设备来说是不可移植的,并且我需要对在监控目录中可能发生的各种事情执行操作,因此下面是我根据leafei的答案编写的脚本,并在本地进行了测试。注意:第一篇文章可能会关闭格式设置

#!/usr/bin/env bash
Var_monitor_dir="${1?No directory passed}"
Var_sleep_time="${2:-120}"
Var_diff_opts="--suppress-common-lines"
Func_parse_change(){
    _added_input="$(grep -E ">" <<<"${@}")"
    _removed_input="$(grep -E "<" <<<"${@}")"
    if [ "${#_added_input}" != "0" ]; then
            mapfile -t _arr_added <<<"${_added_input}"
            let _added_count=0
            until [ "${#_arr_removed}" = "${_added_count}" ]; do
                _current_path="${_arr_removed[${_added_count}]}"
                if [ -f "${_current_path}" ]; then
                    echo "# ${0##*/} detected added file: ${_current_path}"
                elif [ -d "${_current_path}" ]; then
                    echo "# ${0##*/} detected added directory ${_current_path}"
                elif [ -p "${_current_path}" ]; then
                    echo "# ${0##*/} detected added named pipe ${_current_path}"
                fi
                ## Ignore other added input and add to index counter
                let _added_count++
            done
            unset _added_count
    fi
    if [ "${#_removed_input}" != "0" ]; then
            mapfile -t _arr_removed <<<"${_added_input}"
            let _removal_count=0
            until [ "${#_arr_removed}" = "${_removal_count}" ]; do
                echo "# Removal detected: ${_arr_removed[${_removal_count}]}"
                let _removal_count++
            done
    fi
 }
 Func_watch_dir(){
    _current_listing=""
    while [ -d "${Var_monitor_dir}" ]; then
         _new_listing="$(ls "${Var_monitor_dir}")"
         _diff_listing="$(diff ${Var_diff_opts} <(echo "${_current_listing}") <(echo "${_new_listing}"))"
        if [ "${#_diff_listing}}" != "0" ]; then
            Func_parse_change "${_diff_listing}"
        fi
        _current_listing="${_new_listing}"
        sleep ${Var_sleep_time}
    done
}
if [ "${#@}" != "0" ]; then
     Func_watch_dir
     exit 0
else
     echo "${0##*/} needs a directory to monitor"
     exit 1
if
等待时间结束后,第一个终端应报告已出现文件和目录,如果您运行以下命令并再次等待,它也将显示删除

rmdir /tmp/test_dir
rm /tmp/test.file

希望这对您有所帮助。

我想您可以这样编写
crontab
条目:
0-59/2****
是的,您可以,但有时可以,有时不能:-)取决于发行版、版本等。是的,但不能使用Linux上安装的Windows共享CIFS。
rmdir /tmp/test_dir
rm /tmp/test.file