10

I am trying to create a listener on a specific directory that kicks off a Linux command whenever a file shows up in this path. For example, whenever a file shows up in a directory like C:/home/ I would like to read a string of text from that file and then kick off another command. I was thinking of using a loop but that seems inefficient.

Ray
  • 150
  • 2
  • 10
  • 3
    Linux doesn't have 'C:' as a drive. I am confused. – squiguy Mar 19 '13 at 20:19
  • If you don't have to know immediately, you can sleep for a period of time, instead of polling it constantly. You can still use a loop, but do it once a minute (or perhaps longer). – Randy Howard Mar 19 '13 at 20:20
  • @squiguy you're right, my bad. I meant /home/ or something like that. Brain fart from switching between OSs too much. – Ray May 03 '17 at 03:55

1 Answers1

15

To get notified about events like file creation, opening, modifying etc. look into inotify. A good way to use it from bash is with the inotifywait command - here is its man page. It will block until an event you care about happens. For example:

inotifywait -e create /path/to/watch
echo "ding!"

will ding when a file or directory gets created in that path. See the man page for more details.

Jacob Parker
  • 2,546
  • 18
  • 31