tags:

views:

38

answers:

1

I need to read a directory structure from the file system and cache it in an xml document. Can someone suggest me on how to do that using bash ? I only need to cache the directories/subdirectories not files under them .

The structure is randomly generated , so don't know how many nodes and how much the depth of the tree would be each time. It's hierachical and contains lots of subdirectories at each level.

Any guidance would be appreciated.

+1  A: 

Without knowing what the target XML is supposed to look like and what information is needed to be captured, here's a guess:

#!/bin/bash.exe

echo $'<?xml version="1.0" encoding="utf-8"?>\n<dirlist>' > directory_list.xml
find . -type d -exec echo "<dir>{}</dir>" \; >> directory_list.xml
echo "</dirlist>" >> directory_list.xml

(The bash.exe is because I'm on Cygwin.)

Here's a listing of the "tools" directory in my oXygen install dir:

directory_list.xml

<?xml version="1.0" encoding="utf-8"?>
<dirlist>
<dir>.</dir>
<dir>./.svn</dir>
<dir>./.svn/prop-base</dir>
<dir>./.svn/props</dir>
<dir>./.svn/text-base</dir>
<dir>./.svn/tmp</dir>
<dir>./.svn/tmp/prop-base</dir>
<dir>./.svn/tmp/props</dir>
<dir>./.svn/tmp/text-base</dir>
<dir>./ant</dir>
<dir>./ant/.svn</dir>
<dir>./ant/.svn/prop-base</dir>
<dir>./ant/.svn/props</dir>
<dir>./ant/.svn/text-base</dir>
<dir>./ant/.svn/tmp</dir>
<dir>./ant/.svn/tmp/prop-base</dir>
<dir>./ant/.svn/tmp/props</dir>
<dir>./ant/.svn/tmp/text-base</dir>
<dir>./ant/bin</dir>
<dir>./ant/etc</dir>
<dir>./ant/etc/checkstyle</dir>
<dir>./ant/lib</dir>
<dir>./config</dir>
</dirlist>
DevNull