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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
On CEP Maturity and the Gartner Hype Cycle iBot Complex Event Processing RSS News 0 06-01-2008 08:30 PM
wildcard in a if cycle elionba82 Shell Programming and Scripting 2 04-18-2008 05:25 AM
CEP vs the hype cycle iBot Complex Event Processing RSS News 0 02-05-2008 02:20 PM
Stopping a command during the cycle chapmana UNIX for Dummies Questions & Answers 7 11-25-2006 01:08 PM
shell cycle mape Shell Programming and Scripting 2 07-14-2006 12:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-08-2007
Registered User
 

Join Date: Dec 2006
Posts: 3
Stumble this Post!
For cycle

Hello,

I have files in a dir.

I what to create a FOR cycle that will do this

FOR <condition> do
file=`ls <directory> | tail -1`
echo $file
mv -f $file <another dir>
done

What I want to now is what should I put in the <condition>. The condition I want is that the FOR will execute until there is no more files in the directory

Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-08-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Stumble this Post!
Quote:
Originally Posted by nagomes
Hello,

I have files in a dir.

I what to create a FOR cycle that will do this

FOR <condition> do
file=`ls <directory> | tail -1`
echo $file
mv -f $file <another dir>
done

What I want to now is what should I put in the <condition>. The condition I want is that the FOR will execute until there is no more files in the directory

Thanks
If you want to move all files from one directory to another
Code:
mv -f /dir1/* /dir2
Reply With Quote
  #3 (permalink)  
Old 06-08-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
1. Why are you asking almost exactly the same thing again?

2. You would need a 'while' not a 'for'
Reply With Quote
  #4 (permalink)  
Old 06-08-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
Code:
for file in <directory>/*
do
   echo $file
   mv -f $file <another dir>
done
The is a problem with this for loop : if the directory is empty body of the loop is executed nevertheless with the variable file set to '<directory>/*'

Another solution with the for loop :
Code:
for file in `ls -1 <directory>/* 2>/dev/null'
do
   echo $file
   mv -f $file <another dir>
done
You can also use a while loop :
Code:
ls -1 <directory>/* 2>/dev/null | \
while read file
   echo $file
   mv -f $file <another dir>
done
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0