Counting html files with ruby and bash

At work, I had the task to count how many html files where located within a specific folder and its subfolders.

I did it first in Ruby, then in bash. I will show the source code and explain the solution afterwards.

First, the Ruby solution:

puts Dir.glob('**/*.html').count

I invoke the glob method of the Dir class and send it a glob pattern. This pattern refers to all html files within the current directory and within all sub directories. The method returns an array of all found files. This array has a count method which returns the number of entries. The number is printed to the console using the puts command. Read it like this: The program puts the number to the console.

You can invoke this directly from the command line using ruby -e as shown below:

ruby-e "puts Dir.glob('**/*.html').count"

Note, that you have to be careful how to use the or signs. For more information on the Dir class and its methods, please refer to the ruby api on the Dir class.

In bash, the solution is as follows:

find -name "*.html" | wc -l

It consists of two parts: First, all files which ends with .html are found within the current directory and its subfolders using the find command. Each found file will be send to the STDOUT. Normally, this would print this to the console. However, using |  (the pipe character) we redirect the found files to the wc command which stands for word count. This command can count the characters, words or lines of a file/input. In our case, we want it to count the lines by passing it the -l flag. For more information on pipes on unix, read this.

Both solutions work! 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s