Created
April 28, 2013 15:05
-
-
Save seanchas116/5477131 to your computer and use it in GitHub Desktop.
A Ruby script that just counts the number of lines in a given directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pathname' | |
def count_lines(pathname) | |
count = 0 | |
pathname.each_child do |child| | |
if child.directory? | |
count = count + count_lines(child) | |
elsif child.file? | |
count = count + %x{wc -l \"#{child.to_s}\"}.split.first.to_i | |
end | |
end | |
count | |
end | |
if ARGV.length != 1 | |
puts "Error: invalid arguments" | |
exit() | |
end | |
pathname = Pathname.new(ARGV[0]) | |
if !pathname.exist? || !pathname.directory? | |
puts "Error: directory not found" | |
exit() | |
end | |
puts "Line count: #{count_lines(pathname)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment