Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Loop through files in a directory

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-23-2005
rladda rladda is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 7 July 2005, 12:22 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Loop through files in a directory

Hi,
I want to write bash script that will keep on looking for files in a directory and if any file exists, it processes them. I want it to be a background process, which keeps looking for files in a directory.
Is there any way to do that in bash script?
I can loop through all the files like this:
for i in `ls -1 \dirname\*`
do
....
done
I can make it an endless loop llike this while [ true ]
do
done
Thanks for all you help.
R
Sponsored Links
    #2  
Old Unix and Linux 06-24-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Why not run the process in the background ?

rladda.sh


Code:
#! /bin/sh

while true
do
for file in `ls /path/to/dir`
do
[[ -f $file ]] && echo "$file exists"
done
sleep 60
done

Check the /path/to/dir for any file every 60 seconds.

Run the script as

/path/to/rladda.sh &

Vino
The Following User Says Thank You to vino For This Useful Post:
figaro (03-11-2011)
Sponsored Links
    #3  
Old Unix and Linux 06-24-2005
ssk ssk is offline
Registered User
 
Join Date: May 2005
Last Activity: 3 January 2008, 7:24 AM EST
Posts: 25
Thanks: 0
Thanked 2 Times in 1 Post
Hammer & Screwdriver why not cron

You can run the script in cron also to check for the file and process the same if it exists. Just a suggestion. Linux
    #4  
Old Unix and Linux 06-24-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Quote:
Originally Posted by ssk
You can run the script in cron also to check for the file and process the same if it exists. Just a suggestion. Linux
True. cron can be used. And I think it would be a better suggestion than the sleep.

The & makes the script remain alive throughout. If done as a cron job, then once the job is done, the script exits until it is called again.

My 2 cents,
Vino
Sponsored Links
    #5  
Old Unix and Linux 06-24-2005
alisevA3 alisevA3 is offline
Registered User
 
Join Date: May 2002
Last Activity: 15 March 2011, 9:42 AM EDT
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
works well. thank you

Alice
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy all files in 1 directory to another usinge for-in loop Trinimini UNIX Desktop for Dummies Questions & Answers 1 03-10-2011 03:59 AM
Find the latest directory and loop through the files and pick the error messages sudhir_83k Shell Programming and Scripting 15 10-22-2010 05:25 AM
loop through files in directory rajesh_tns Shell Programming and Scripting 3 04-29-2009 01:07 PM
Loop certain code to all files within directory cgkmal Shell Programming and Scripting 3 01-19-2009 11:48 PM
loop through the directory for files and sort by date and process the first file dsdev_123 AIX 1 01-30-2008 04:31 PM



All times are GMT -4. The time now is 04:26 AM.