![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| grep'ing a file until a certain message appears | pallak7 | Shell Programming and Scripting | 3 | 04-23-2009 12:48 PM |
| delimiter appears in field | derekxu | Shell Programming and Scripting | 3 | 02-13-2009 04:17 AM |
| Copying one file at a time from one directory to another directory. | Nikhilindurkar | UNIX for Dummies Questions & Answers | 1 | 09-01-2008 10:25 AM |
| appears 4 or more time | nickg | UNIX for Dummies Questions & Answers | 0 | 10-19-2007 05:00 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Take action if a particular file appears in a directory
This is my task - pls help
Write a script that will run every 5 min and check if a particular file has appeared in a particular directory. Once it appears then rename the file and move it a bkp directory and run another script. Last edited by Yogesh Sawant; 06-10-2009 at 04:49 PM.. |
|
|||||
|
to run your script every 5 minutes, you need to schedule a cron job, see - cron and crontab the script would be something like: Code:
file_to_check="/tmp/newfile" backup_dir="/foo/" another_script="/foo/bar" if [ -e $file_to_check ] ; then # Check if file exists mv $file_to_check $file_to_check.backup # Rename the file mv $file_to_check.backup $backup_dir # And move it to a backup dir another_script # Run another script fi |
|
||||
|
THis is my file named mycron which I add it to the crontab entry later to run every 5 mins and this works. But I havnt done the last part - to run another script if everything succeeds.
Thanks for the reply Yogesh. But in ur code, what if the mv fails - ie, say the destination directory doesnt exist or something else ... only after all this succeeds I need to start another script. # # Taking action if a particular file appears in a dir # filename="/home/mrudula/myfile" count=`find "$filename" | wc -l` if [ "$count" -eq 1 ] then # Renaming filename new_filename="$filename".new mv $filename $new_filename echo "$filename" renamed to "$new_filename" # Moving to backup directory bkup_dir="$filename"-bckup mkdir "$bkup_dir" mv "$new_filename" "$bkup_dir/$(basename $new_filename)" echo "$new_filename" moved to "$bkup_dir" else echo "No file found - "$filename"" fi |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|