The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



Thread: For cycle
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 06-08-2007
aigles's Avatar
aigles aigles is offline
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
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