For loop failing cd command

Hi guys,

i've wrote the following loop;

for i in `ls`
 do
 cd $i/host
 cat "xxxx.txt" |grep "yyyy" >> zzzz.txt
 done

I have a set of folder with different name and i need to extract a value from a file contained in the host subfolder ( that is present in each folder).

When i run the script it returns the following error
"No such file or directory", but actually it exists...

You are changing into directories and never changing back out. folder2 may exist, but it probably doesn't inside folder1! So do cd ../../ at the bottom of the loop to back back out.

Try this:-

for i in `find . -type d`
do
   cd ${i}/host
   cat xxxx.txt |grep "yyyy" >> zzzz.txt
   cd -
done

Thanks guys!!

@ bipinajith

Find command is recursive so it's better to use command such as 'ls'.

There was a missing / after

cd $i/host

Now i'm trying to understand the reason why the output redirection is not working...

You can also use complete folder structure

cd /folder/${i}/host

then you no need to return to previous folder