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

If you want to move all files from one directory to another

mv -f /dir1/* /dir2
  1. Why are you asking almost exactly the same thing again?

  2. You would need a 'while' not a 'for'

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 :

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 :

ls -1 <directory>/* 2>/dev/null | \
while read file
   echo $file
   mv -f $file <another dir>
done

Jean-Pierre.