Solved

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

Posted on 2014-02-14
11
352 Views
Last Modified: 2014-05-06
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
0
Comment
Question by:aej1973
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39861226
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.
0
 

Author Comment

by:aej1973
ID: 39861301
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39861354
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.
0
 

Author Comment

by:aej1973
ID: 39861361
True, that is what I am working on now.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39861435
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?
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:aej1973
ID: 39861451
just keep them in the same directory.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39861521
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.
0
 
LVL 2

Expert Comment

by:c_kedar
ID: 39864072
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.
0
 

Author Comment

by:aej1973
ID: 39864825
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39864968
Maybe worth a test; I've never used iwatch, but the blog post about it sounds credible.
0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39886543
iwatch / inotify is nice but not sufficient because if your script stops at some point, it will not pickup changes that happened in the meantime

it would be much easier if you moved the file to another directory after processing them

but if you need to keep them in the same dir, as far as i know, mtime works in days and not minutes. it seems more reasonable to use the -newer predicate and store where you stopped in a different file. there are many other possible options.

something like this should do (written directly in ee so may be typos)

DIR=/your/watch/dir
touch /var/run/upload_checks_
sleep 1

values=$(
if test -f /var/run/upload_checks
then find $DIR -newer /var/run/upload_checks
else find $DIR
fi | sed 's:.*/\([^/@]*\)@\([^.]\)\..*$:,("\1","\2"):' | tr -d "\n" | cut 2-
)

echo 'insert ignore into TABLE (cola,colb) VALUES '$values | mysql -u USER -p PASS -h HOST  \
&& mv /var/run/upload_checks_ /var/run/upload_checks

Open in new window


make sure your db has a uniq index on the combined 2 fields
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 1 35
comparing strings that contain line breaks 18 37
update field on focusout 15 24
PHP string issue 5 18
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now