watch command to notify on newly created files on linux
How to modify the below command to notify on creating a new file to /usr/local/mydir/ by linux user john ?
watch -d 'ls -l /usr/local/mydir/ | fgrep john'
ie I want to continually monitor and call an another script if any new file is created to /usr/local/mydir/.
As In production I cannot go for any tool instead of shell script. So can help me with shell scripting solution ?
Instead of constantly pulling a directory this way, maybe try to use the
inotify
subsystem, which is designed for this purpose. For user space tools, have a look at theinotify-tools
.inotify
suggested by SvenW is an excellent choice for this.Just to be different: Alternative, more complete way for this kind of stuff is to use kernel audit subsystem and
auditd
. Just installauditd
if it's not already installed and then useauditctl
, in your casewould do.
Then you can run a report and see a very detailed log of what happened and when:
This produces results similar to this:
If you insist on using just standard tools, a script like the following might help:
Replace the
echo
line with your desired action and remove theexit
line if you want to run this script continuously.Note: This will act on any modification of the directory listing, even if files just get updated, not added.