[Gllug] Finding files in directories recursively

Matt Blissett matt at blissett.me.uk
Sun Jun 29 20:20:06 UTC 2008


Jon Dye wrote:

> Hi,
> 
> I want to run a program (mp3gain) passing it all the files in a
> particular directory.  I then want to do this recursively, e.g.

If you can, using zsh makes this reasonably simple:

$ mkdir -p dir1 dir2 dir3/dir4
$ touch dir{1,2}/file{1,2,3} dir3/dir4/file{1,2,3}

$ for d in **/*(/); echo $d/*
dir1/file1 dir1/file2 dir1/file3
dir2/file1 dir2/file2 dir2/file3
dir3/dir4
dir3/dir4/file1 dir3/dir4/file2 dir3/dir4/file3

That might not be a problem, but if it is:

$ setopt nullglob
$ for d in **/*(/); f=( $d/*(.) ) && [[ $#f != 0 ]] && echo $f
dir1/file1 dir1/file2 dir1/file3
dir2/file1 dir2/file2 dir2/file3
dir3/dir4/file1 dir3/dir4/file2 dir3/dir4/file3

The ** glob thing is very useful in combination with the (). E.g.:
$ echo **/*(.)
dir1/file1 dir1/file2 dir1/file3 dir2/file1 dir2/file2 dir2/file3
dir3/dir4/file1 dir3/dir4/file2 dir3/dir4/file3
$ echo **/*(/)
dir1 dir2 dir3 dir3/dir4

**/*(om)   -- ordered by modification time
**/*(oL)   -- by size (length)

Lots more in zshexpn(1) :-)

Matt


-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list