Skip to content

Instantly share code, notes, and snippets.

@higuma
Created June 13, 2022 14:01
Show Gist options
  • Save higuma/f47e9927831a38f3c242172269c6c2f0 to your computer and use it in GitHub Desktop.
Save higuma/f47e9927831a38f3c242172269c6c2f0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Recursive line/word/byte counter
require 'find'
require 'fileutils'
ARGV.push '.' if ARGV.empty?
total_lines = total_words = total_bytes = 0
for arg in ARGV
Find.find arg do |path|
if FileTest.file? path
bytes = FileTest.size path
contents = File::readlines(path)
lines = contents.size
words = contents.inject(0) {|result, line| result + line.split(/\s+/).size }
printf "%7d %8d %9d %s\n", lines, words, bytes, path
total_lines += lines
total_words += words
total_bytes += bytes
end
end
end
printf "%7d %8d %9d [Total]\n", total_lines, total_words, total_bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment