How to find if there are new files in a directory every 2 minutes in shell script?

后端 未结 5 1820
别跟我提以往
别跟我提以往 2021-02-03 14:11

I have a directory called /home/user/local. Every two minutes or so, a new file is dumped into this directory. I need to check this directory every 2 minutes to see

5条回答
  •  一整个雨季
    2021-02-03 15:02

    First off a big thank you to leafei, I'd hoped that it was that easy. However for my devices writing temporary files for loops wasn't portable enough for my current project and I required performing actions for various things that might happen within a monitored directory, so here's the script I wrote based off leafei's answer and tested locally. Note formatting maybe off on first post...

    #!/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
    

    Assuming the above is saved as monitor_directory.sh and the directory to be monitored is /tmp then opening one terminal and inputting monitor_directory "/tmp" (or monitor_directory.sh "/tmp" "10" to have faster updates between tests) will start the loop that checks for changes. Then opening a second terminal try inputting the following and waiting for the first terminal to update

    mkdir -p /tmp/test_dir
    touch /tmp/test.file
    

    After the wait time is up the first terminal should report that a file and a directory has appeared, if you run the following and wait again it'll show removals too.

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

    Hope this helps some.

提交回复
热议问题