0

i have to write a shell script which continuously runs without exiting and it should check every hour if any new files have been created in the current directory in which it is running. The directory to start with will be a command line argument to the script.

can someone help me with this? which commands should I use?

developer_beginning
  • 373
  • 3
  • 7
  • 17
  • definitely sleep... and ls, wc or grep if you need to print new files. or ls and date, or find – perreal Jan 07 '17 at 05:53
  • @perreal, no, *not* sleep. Using sleep means you need to poll, which is highly inefficient compared to letting the OS wake you up if and only if there's a change. – Charles Duffy Jan 07 '17 at 05:56
  • ...so, frankly, the best answer is **not** to write your shell script to stay resident like that at all, but to configure [incron](http://inotify.aiken.cz/?section=incron&page=about&lang=en) to launch your script if and only if there's a change in the directory that's monitored. – Charles Duffy Jan 07 '17 at 05:58
  • The second best answer is to have your script use [`inotifywait`](https://linux.die.net/man/1/inotifywait) to be launched if and only if an event you care about happens. – Charles Duffy Jan 07 '17 at 05:58
  • @CharlesDuffy absolutely, if something like inotify is possible that is the way to go, but if you have to write a shell script that... and if you need to report every hour it is a slightly different problem – perreal Jan 07 '17 at 05:59
  • ...of course, incron and inotifywait are Linux-specific tools, but other platforms have their own -- on MacOS, for instance, launchd has its own support for triggering a script on a filesystem change. – Charles Duffy Jan 07 '17 at 06:04
  • @perreal, ...also, [programmatic use of `ls` is highly error-prone](http://mywiki.wooledge.org/ParsingLs). If one has GNU `find`, then its `-printf` functionality can be used to extract epoch time for each file, and there's also support for comparing each file's time to that given on the command line, so *that's* a sane option. A `stat` implementation with arbitrary format-string support likewise lends itself to a robust (but nonportable, given lack of specification of such feature in POSIX) implementation. – Charles Duffy Jan 07 '17 at 06:04

0 Answers0