[CentOS] how to do repetetive command in shell

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

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)

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

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

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