[CentOS] how to do repetetive command in shell

Paul Heinlein heinlein at madboa.com
Thu Oct 21 20:26:37 UTC 2010


On Thu, 21 Oct 2010, John Kennedy wrote:

> Damn...I will get this right...Need more sleep...

> for i in `ls -d /opt/* | cut -d/ -f3` do
> cp /opt/${i}/test/ /backup/${i}
> done

Using ls in a for loop is rarely necessary.

   for i in /opt/*; do
     cp -a ${i}/test /backup/$(basename $i)
   done

More simply, just cd to /opt before globbing filenames:

   cd /opt
   for d in *; do
     cp -a ${i}/test /backup/${i}
   done

-- 
Paul Heinlein <> heinlein at madboa.com <> http://www.madboa.com/



More information about the CentOS mailing list