Take action if a particular file appears in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take action if a particular file appears in a directory
# 1  
Old 06-10-2009
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  
Old 06-10-2009
Well, it is your task, so what have you tried?
# 3  
Old 06-10-2009
to run your script every 5 minutes, you need to schedule a cron job, see - https://www.unix.com/answers-frequent...n-crontab.html

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  
Old 06-10-2009
Quote:
Originally Posted by KevinADC
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare file name and take action

Have some files in /tmp/dir abc.zip 123.zip 345.zip and if name matches to 345.zip then take action My code.... am i doing something wrong ? Please advise. #!/bin/bash set -x cd /tmp/dir for i in *.* do if ] then (4 Replies)
Discussion started by: abhaydas
4 Replies

2. Shell Programming and Scripting

Read all lines after a string appears in the file.

Hi All, I want to read all lines after a perticular string {SET UP VALUES}apprears in the file. SET UP values contains direcory, number of days and file type. Step1: Read all lines below SET UP VALUES string. Step2: If set up values are not present in each record then read from default... (4 Replies)
Discussion started by: Nagaraja Akkiva
4 Replies

3. Shell Programming and Scripting

Shell program to check if the same text appears twice in an XML file

Hi All, I am very new to this forum and beginner to shell scripting. I need a shell script to: Search for a text in XML file à if the same text appears twice in an XML file à output file name Script should loop thru every xml file of a given folder. Please help me writing this script. ... (1 Reply)
Discussion started by: amardeep001
1 Replies

4. UNIX for Dummies Questions & Answers

how to find a word in a file that appears next to a given keyword

Hi Experts, I have a file which contains some text. i need to print the word next to a given keyword. Please help. Ex: test.txt ===================== NEXT HOST ===================== AEADBAS001 access-list 1 permit xxxxxxxxxxxxxx ip access-list extended BLA_Outgoing_Filter... (6 Replies)
Discussion started by: mwrg
6 Replies

5. Shell Programming and Scripting

Take action only if a file is X hours (or seconds) old

shell: #!/bin/ash I searched and found a few relevant posts (here and here - both by porter, on the same day (?)) however both are just a do while loop, I need to check a file date and compare it to the current time. I would like it to say if file 'test' is more than 12 hours old than "right... (3 Replies)
Discussion started by: phdeez
3 Replies

6. Shell Programming and Scripting

Opening file and executing an action

I want the script to read the directory I am running the script from and print the contents of any file that has GX in it's title. This is the code needed. But how do I combine it? #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){... (4 Replies)
Discussion started by: DemonixX
4 Replies

7. Shell Programming and Scripting

grep'ing a file until a certain message appears

Hello, I'm writing a script that will automate the launch of some services on my AIX machine. However, some services are dependent on the successful startup of others. When I start these services manually, I usually just check a log file until I see a message that confirms a successful... (3 Replies)
Discussion started by: pallak7
3 Replies

8. Shell Programming and Scripting

Perform action file name written to the pipe

Hello, I have a script that monitors files uploaded via ftp. After a successful upload, the file name is written to the pipe. There is another program that reads this pipe and allows automatically run any program or script ( say test.sh ) to process the newly uploaded file. cat test.sh... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

9. Shell Programming and Scripting

Clean file in single action

What one finds challenging another finds simple... (HPUX B.11.11) I have a text file named something like 12345.dst that could look like this: DOG CAT NONE TEST CAT What I want to end up with is 12345.dst looking like this: CAT DOG TEST removing "NONE" should it be there and... (1 Reply)
Discussion started by: djp
1 Replies

10. Shell Programming and Scripting

Compare 2 different file version and prompt for action

Hi, I need some assistance with comparing 2 diff application. The format of the first file is in TAR "filename.1.2.3.tar" . The second file is a running application for which i need to find the version and then prompt the user for action as below :- Once compared, It needs to prompt the user,... (0 Replies)
Discussion started by: systemali
0 Replies
Login or Register to Ask a Question