我根本不會使用子命令。內置的Dir.glob
應該在這種情況下工作。
喜歡的東西:
Dir.glob("#{path}/**/**").each { |fileOrDir| do_something }
在任何情況下,你的find
是遞歸的,但你的dir
不會。
如果你真的想知道,如果在Windows上運行,還有一些紅寶石爲中心的方式,但多年來我一直在環境被檢查的WINDIR
變量,例如
if ENV["WINDIR"]
puts "On Windows"
else
puts "Not Windows *probably*"
end
編輯:這是我幾年前寫的一個工具,生成一個節點樹,然後顯示它們以各種方式排序。
def usage
puts <<END
Recursive listing of files or dirs, sortable by date or size or count.
rl [-cdfnrsuv] [-p pathname] [pathname ...]
Where:
pathname = Dir or file to process; default is ".".
-c = Sort by file count; default is sort by date.
-d = List dirs only, with their contents sizes and counts.
-f = List files only, no dirs or links.
-n = Sort by name; default is sort by date.
-p = Add pathname even if it starts with "-".
-r = Reverse sort order; default order is desc, except by name is asc.
-s = Sort by size; default is sort by date.
-u = Unsorted; default is sort by date.
-v = Verbose, including type, perms, and owner.
END
exit(1)
end # usage
class Node
attr_reader :path, :stat
def load(path)
@path, @stat, @children = path, File.lstat(path), []
@stat.directory? and Dir.glob("#{path}/*", File::FNM_DOTMATCH).each { |sub_path|
sub_path[-2,2] != "/." && sub_path[-3,3] != "/.." and @children << Node.new.load(sub_path)
}
self
end
def size
@size or @size = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.size }) : @stat.size
end
def count
@count or @count = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.count }) : 1
end
def to_a
@children.map { |child| child.to_a }.flatten + [self]
end
end # Node
only_dirs = only_files = by_count = by_name = by_sz = verbose = false; sort = 1; paths = []
while (arg = ARGV.shift)
arg =~ /^-[^-]*[h?]/ and usage
arg =~ /^-[^-]*c/ and by_count = true
arg =~ /^-[^-]*d/ and only_dirs = true
arg =~ /^-[^-]*f/ and only_files = true
arg =~ /^-[^-]*n/ and by_name = true
arg =~ /^-[^-]*r/ and sort *= -1
arg =~ /^-[^-]*s/ and by_sz = true
arg =~ /^-[^-]*u/ and sort = 0
arg =~ /^-[^-]*v/ and verbose = true
arg =~ /^-[^-]*p/ and paths << ARGV.shift
arg !~ /^-/ and paths << arg
end
nodes = (paths.empty? ? ["."] : paths).map { |path| Node.new.load(path).to_a }.flatten
if sort != 0
if by_sz then nodes.sort! { |a, b| sort * (2 * (b.size <=> a.size) + (a.path <=> b.path)) }
elsif by_count then nodes.sort! { |a, b| sort * (2 * (b.count <=> a.count) + (a.path <=> b.path)) }
elsif by_name then nodes.sort! { |a, b| sort * (a.path <=> b.path) }
else nodes.sort! { |a, b| sort * (2 * (b.stat.mtime <=> a.stat.mtime) + (a.path <=> b.path)) }
end
end
for node in nodes
next if only_dirs && ! node.stat.directory?
next if only_files && ! node.stat.file?
puts "%s %11s %6s %s%s" % [
node.stat.mtime.strftime("%Y-%m-%d %H:%M:%S"),
node.size.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse,
node.count.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse,
verbose ? "%-9s %6o %4d %4d " % [:ftype, :mode, :uid, :gid].map { |v| node.stat.send(v) } : "",
node.path]
end
這是傳統Ruby認爲使用系統工具和功能而不是語言功能的情況下,當您嘗試成爲平臺包容性時,這種情況會持平。這只是一種務實或徹底的折衷。 – Azolo