The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-10-2009
mrudula009 mrudula009 is offline
Registered User
  
 

Join Date: May 2009
Posts: 22
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..
  #2 (permalink)  
Old 06-10-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Well, it is your task, so what have you tried?
  #3 (permalink)  
Old 06-10-2009
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
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

  #4 (permalink)  
Old 06-10-2009
mrudula009 mrudula009 is offline
Registered User
  
 

Join Date: May 2009
Posts: 22
Quote:
Originally Posted by KevinADC View Post
Well, it is your task, so what have you tried?
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0