[nzlug] bash help - for loop listing dirs

Simon Green simon at simongreen.net
Thu Feb 22 08:50:29 NZDT 2007


On 22/02/07, Howard <howard-nzlug at fox.co.nz> wrote:
> Hi, I'm after some bash script help please...
>
> I have a for loop which I thought would need to be thus:
>
> for i in `ls -1`
> do
> cd $i
> blah...
>
> I need to recursively list dirs, then do some stuff.  Some dirs have

You may find the 'find' command more helpful.

For example "find /bin -type d" would list all directories under the
/bin directory (including /bin itself). Used with the (-print0 and
xargs -0) or -exec option, you can then do something on each of these
directories, eg.

find /bin -type d -exec ls -la {} \;
find /bin -type d -print0 | xargs -0 ls -la

HTH

  -- simon




More information about the NZLUG mailing list