List of Directories Containing XML Docs

Ovid on 2009-01-02T09:48:58

As part of my holiday refactoring, I need to generate a list of directories containing XML docs. Surely there's something more natural than this?

find . -name '*.xml' | perl -nle '/(.*)\// && print $1' | uniq

I assumed there would be an option for find, but I didn't see it. (Well, gfind, that is. Solaris' find command is a pile o' junk, like so many other things on Solaris.)


I don't get it...

melo on 2009-01-02T11:45:06

If you are using perl in that pipe, why not just use perl?

perl -MFile::Find -e 'find(sub { return unless /[.]xml$/; print "$File::Find::dir\n"}, @ARGV)' .

Best regards,

-printf

Smylers on 2009-01-02T12:16:39

Gnu Find has a -printf action which you can use to emit just the names of the directories, like this:

find -name '*.xml' -printf '%h\n' | uniq

I don't think it's possible to avoid the uniq though; -prune sounds promising, but isn't quite right to be useful here. So for a directory with lots of XML files in it, once it's found the first one and told you the directory you have to sit there and wait while it tells you again for all the others.

Re:-printf

Ovid on 2009-01-02T13:00:13

Thanks! That's exactly what I was looking for.

Or the Unix equivalent:

Simon on 2009-01-02T15:10:17

find . -name '*.xml' -exec dirname \{\} \; | uniq

Re:Or the Unix equivalent:

Aristotle on 2009-01-02T16:00:54

Ovid is running this on a pretty big tree, so -exec isn’t a very good idea. Is this a Unix without xargs?

Re:Or the Unix equivalent:

youam on 2009-01-03T17:17:54

dirname can't handle more than a single path name, so you'd need to run xargs with -n1, which would be even one more process than find -exec

Re:Or the Unix equivalent:

Aristotle on 2009-01-04T05:46:56

Hm, good point. I guess sed would have to do in that case:

find . -name '*.xml' | sed 's/[^/]*\.xml$//; t; d' | uniq

Re:Or the Unix equivalent:

Aristotle on 2009-01-04T05:47:39

Err, and the “t; d;” part in the sed script isn’t even necessary.