Monitor files in a specific directory in C Programming Language in UNIX


Is there a way we can monitor files in a specific directory in UNIX using C Programming Language?

What I want is, when a file (file1.txt) in a directory (example A directory) is moved to different directory (B directory) my C program will log it in a file. The program will add a line on a log file like:
"file1.txt is moved to B directory" ...

Is this possible in C? How? Or do you have any other recommendations?

Thanks a lot.

Who is Participating?
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Take a look at the inotify API

"The inotify API provides a mechanism for monitoring file system events.
Inotify can be used to monitor individual files, or to monitor directo-
ries.   When  a  directory is monitored, inotify will return events for
the directory itself, and for files inside the directory."

NB. The inotify API replaces the older dnotify.
janjerellAuthor Commented:
Thanks so much evilrix... I'll get back to you soon as I figured this out...
janjerellAuthor Commented:
Thanks evilrix.. I have it working right now... SOrry for the Grade.. It should be A.. I don't know how to change.. anyway, thanks again...
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

evilrixSenior Software Engineer (Avast)Commented:
>> Thanks evilrix..
Most welcome.

>> SOrry for the Grade.. It should be A.
If you'd like to change the grade you can "Request Attention" by clicking the link at the top right of the page and posting a request in Community Support.
evilrixSenior Software Engineer (Avast)Commented:
Many thanks indeed.
janjerellAuthor Commented:
thanks mod
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.