Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

shell script update database with the file name....

Hello, I have a linux ubuntu server and at times there are new files added to a folder. I would like to have shell script that will check this folder periodically and then update the folder name to the database ( if the name does not exist in the db already). Can someone let me know how I can do this? Thanks for the help.

A
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Uhh, can you please tell us a little more about the business rules here?  Why are you asking for a shell script?  What role does that play in your application logic and workflow?

It's relatively easy to detect changes in a folder.  PHP has scandir().  You can store a copy of the output and compare the current scandir() output to the stored copy to see if anything changed.  You can go a little further by checking filemtime() values for the contents of the folder.
Avatar of aej1973
aej1973

ASKER

Hello Ray, thanks for getting back to me. My business logic is as follows:

1.There are times that a user will upload an audio file to my server. This upload is not done via any form but through a telephone.

2.Since the upload is not done via a form I need to check to see if any new audio file has been uploaded to the folder ( there is a specific folder that the audio file gets uploaded to).

3.If a new audio file has been uploaded, then I need to upload the name of the audio file to a mysql database. The audio file is in the format aaaa@bbbbb.net. I will need to split the string at the '@'sign and store the 'aaaa' portion as one field and the 'bbbbb' portion as another field.

Thanks again for the help.

Regards,
a
Makes sense.  Have you considered using a CRON job?  You can fire up the CRON job once per minute and it will take only a fraction of a second to check for new files.  That would seem to be more efficient than manually running something from the command line.
Avatar of aej1973

ASKER

True, that is what I am working on now.
Good.  You can usually set something like that up with cPanel or similar.  Are you planning on keeping all of the uploaded files in the same directory or moving them to a different permanent store?
Avatar of aej1973

ASKER

just keep them in the same directory.
OK, here is what I would do.  

When you use scandir() you get back an array.  You can use json_encode() to turn this array into a string.  You can use md5() to make a hash from the string.  Md5() is idempotent, meaning that it will always create the same hash from the same input string.  You can store this hash and the  JSON string in a data base table.  If the newly made hash is the same as the old hash, there have been no changes to the directory.  If there is a difference, you can compare the contents of the current scandir() to the old scandir() and detect the differences.  That should tell you what is new in the directory.

You might also want to consider the possibility that there could be a name collision in the directory.  Not sure if that could happen, but it's worth a moment's thought since it might cause data loss.
Shell command 'find' is well suited for this purpose.
Use -mtime options of find, something like 'find -mtime -10' to get list of files created/updated in last 10 minutes and process the output of the command to update db.

Setup cron to run this script every 10 minutes.
Maybe worth a test; I've never used iwatch, but the blog post about it sounds credible.
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial