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 if a new file/files have landed in there. And if there are new files, I need to put a list of it into a variable to use later on. How do I do this shell script?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
a quick & dirty way. :) It'll monitor the directory for changes, not only when there's new file dumped in, but also if some file is missing/deleted.
File list is stored in variable
$cur_files
.Set up a cron job that runs a script that takes the current listing and compares it to an older listing stored in a file somewhere.
Lets say your variable is called $listOF
Create a script:
And put this script into the crontab by cmd: crontab -e in format:
inotifywait
is exactly what you are looking for: http://linux.die.net/man/1/inotifywaitFirst 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...
Assuming the above is saved as
monitor_directory.sh
and the directory to be monitored is/tmp
then opening one terminal and inputtingmonitor_directory "/tmp"
(ormonitor_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 updateAfter 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.
Hope this helps some.