Loop through files in a directory | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Loop through files in a directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 06-24-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 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 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.
    #4  
Old 06-24-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 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.
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 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 04: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 06:25 AM
loop through files in directory rajesh_tns Shell Programming and Scripting 3 04-29-2009 02:07 PM
Loop certain code to all files within directory cgkmal Shell Programming and Scripting 3 01-20-2009 12:48 AM
loop through the directory for files and sort by date and process the first file dsdev_123 AIX 1 01-30-2008 05:31 PM



All times are GMT -4. The time now is 03:28 PM.