Getting one file per directory

Getting one file per directory

am 23.10.2007 18:49:15 von mathieu.malaterre

Hello,

I am trying to traverse a directory and only print a single file per
sub-directory found. So far I have something like:

for i in `find /usr -type d`; do find $i -maxdepth 1 -type f | tail
-1 ; done > /tmp/files.txt

Well it works as long as the directory does not contains a space in
the path. Is there a way to work around that ? Any other solution is
fine (all I need is a single file per directory).

Thanks
-Mathieu

Re: Getting one file per directory

am 23.10.2007 19:25:22 von Stephane CHAZELAS

2007-10-23, 16:49(-00), mathieu:
> Hello,
>
> I am trying to traverse a directory and only print a single file per
> sub-directory found. So far I have something like:
>
> for i in `find /usr -type d`; do find $i -maxdepth 1 -type f | tail
> -1 ; done > /tmp/files.txt
>
> Well it works as long as the directory does not contains a space in
> the path. Is there a way to work around that ? Any other solution is
> fine (all I need is a single file per directory).
[...]


With GNU find, you can do:

find /usr -type d -exec find {} -maxdepth 1 -type f -print -quit \;

With zsh

for dir (/usr/**/*(D/oN)) {
file=($dir/*(.NDoN[1]))
(($#file)) && print -r -- $file
}

(none tested)

--
Stéphane