The UNIX Forums
"Join the Network of UNIX System Users"


 
bluemouse2
Newbie
Rank: 1



UID 72
Digest Posts 0
Credits 0
Posts 16
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:36 AM  Profile | P.M. 
for cycle



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


Top
vino
Newbie
Rank: 1



UID 16
Digest Posts 0
Credits 0
Posts 74
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:36 AM  Profile | P.M. 
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
Top
ekarey
Newbie
Rank: 1



UID 133
Digest Posts 0
Credits 0
Posts 16
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:36 AM  Profile | P.M. 
1. why are you asking almost exactly the same thing again?
Top
arxidakas
Newbie
Rank: 1



UID 59
Digest Posts 0
Credits 0
Posts 42
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:36 AM  Profile | P.M. 
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
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:41 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005341 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP