tags:

views:

18

answers:

1

In Ruby 1.8, how do I delete a tree of directories where some of the subdirectories begin with '.'?

For example, I have an embedded Linux filesystem directory that I want to clean out. One of its subdirectories is ./dev/.udev/files.

Dir[ "{**/*,**/.**,**/.*}" ].sort.reverse.each do | p |
    puts p
    if ( ( p != '..' ) and ( p != '.' ) ) then
        if File.directory? p then
            Dir.rmdir p
        else
            File.delete p 
        end
    end
end

This recognizes ./dev/.udev/, but it won't remove the files (or files and directories) under .udev.

I realize that I could be brutal and execute

system("rm -Rf *")

from the working directory, but I'd like to understand the globbing methodology better.

Thanks in advance! :D

A: 

You could try

Dir.glob(".[^.]*")

For removing directory, you can try removing all files inside the directory first, then do the remove, or you can use FileUtils.rm_rf. See the FileUtils doc for more information

ghostdog74
The Dir[].sort.reverse was intended to list the deepest files first before the directory they reside in. It works perfectly except that the glob patterns ** and * don't accept files or directories that begin with .
Donald Scott Wilde
Thank you for the suggestion on FileUtils; that's cleaner than system(). :D
Donald Scott Wilde