Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello
Can anyone tell me if there is any Linux Equivalent for Windows NT API fuinctions which help us to identify when a
file in a directory is added,Edited or Deleted.I need this sort of interface very badly.
I don't know what glibc offers but I know KDE have an excellent technology called KDirWatchers which fulfills your need. If you want to use the KDE framework(which will solve this problem) I suggest you drop in on #kde or #kde-users on OPN or join the kde mailinglist(send an mail with "subscribe" as subject to kde-devel-request@mail.kde.org).
You should also check out http://developer.kde.org where all you need to get familiar with KDirWatch is documented.
To check if a file is added or removed you can simply dump the file listing to a file. When then checking if a file is added or removed you can then just compare your dump-file with the actual file listing.
A simple way to do a file listing is running by the following at shell (I don't know how much you know, so I write some extra):
------------
ls > your_dump_file
------------
If you're writing your program in C/C++ you may do the command above by calling:
------------
#include <stdlib.h>
....
system("ls > your_dump_file");
------------
When you're then going to check the last time the file was modified you then call this function (you'll actually have to compare every file to check if it's been modified or not):
------------
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int stat(const char *path, struct stat *buf);
------------
Where "path" is the file you'd like to get information about and "buf" is a pointer a "struct stat"-type where it will save all the info about the file.
The struct "stat" contains a lot of other information about files. If you're interrested in them all try "man 2 stat". Anyway, the "struct stat" has a member called "st_mtime" which is of the type "time_t" (numbers of seconds since 1970). That's the variable you'll have compare with your dump-file.
(I forgot to tell you above that you'll also have to dump the last modification times for each file :-) )
So, now you have something to do :-)
If you don't like the shell-solution of dumping a file and instead would like me to send the solution on how to list a directory inside a C-program I can do that :-)
Me judging Saravanan's post; it looks like he/her wants to know *when*, to get notified when the something changes. One solution is to construct a loop which performs what you suggested and then compares the data so see if something had changed. But that is really ugly.
Please try not to double post....there have been solutions offered in both this thread and this one but you need to tell us why they won't work so we can find the right solution.
Dear Cyth
U assumption was right, i want to nnow whenever a file is being added into a directory.Whenver a new file is added i will check for certain contents in the file and if the conditions are satisfied i will move the file to anothere directory.
Dear Ztyx
I am actually calling the shell program from a PHP script.
system will do,stat is fine it will only give some information about the file/directory,but how do i compare dump_file with the directory to find out if a new file is added
This is an php-example on how to check if a file i added. The file "dump_file.txt" is the file you dumped the list of files last time last time. That file is being used to compare with the new file list.
To use this code below you must first use chdir() to change to the directory where you're going to compare the files...
------------------------------
PHP Code:
function isStringInFile($file, $string) { $fp = fopen($file, "r");
$found_a_file = false;
while ($teststring = fgets($fp, 4096)) { if ($teststring == $string) $found_a_file = true; {
fclose($fp);
return $found_a_file; }
// Here goes the actual code // First dump the files which exists at this moment system("ls > /tmp/a_temp_file.txt");
$fp2 = fopen("/tmp/a_temp_file.txt", "r");
while ($file_name = fgets($fp2, 4096)) { if (isStringInFile("dump_file.txt", $file_name) == false) { echo("The file $file_name has been added recently"); } }
fclose($fp2); unlink("/tmp/a_temp_file.txt");
// After the comparation you just dump the directory to file again system("ls > dump_file.txt");
-----------------------------------
This doesn't have code for the modification of files. If you'd like to check which files that have been deleted you just run my code above except that you compare the new old file dump with new one instead. Got it?
I hope this helps or perhaps this isn't the solution you're looking for...
Dear Jens Rantil
i got ur idea of trapping a new file nbeing added it is only the lack of ,y knowledge in Linux that bought me down.Ur logic is good, as of know i will carry on with this logic to find out if a file is modifed we will get the size of the file and compare it,meanwhile if i find some other optimal solution i will inform u also.I have decided to learn the linux commands in and out.
Regards
Saravanan
Well scanning the contents of the complete directory every x seconds to see if there are any changes is a kind of expensive operation. The modified time of a directory is automatically changes when something in the directory changes (Ex. file is added, file is removed, file is modified). Once the timestamp is changed then you can scan the directory contents to find out exactly which file is modified, added or deleted.
In c you could check the directory timestamp with a simple stat command. I don't know how you would do that with perl but I'm sure it wouldn't be to hard to do.
Dear Mik
I am to going to check the directory every xx seconds i will be checking the directory every x days and check if any new file has been added.I will tell u my exact requirement.
I need to check if a user has received a mail if he has i need to read he mail and do some manipulations,i think the solution provided by Rantil serves the purpose anyway i have started imoplementing his logic,i
Just so you know. There might be better solutions although due my lack of knowledge about any such library in linux I'd write my program in the way described above.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.