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! 🙂

Advertisements

Starting Sublime Text 2 from the Command Line

My all-new-favorite text editor is Sublime Text 2. This editor is blazingly fast, has a project view for simple navigation, supports syntax highlighting for allmost all formats out of the box and can be extended. I often have to work in the command line, however, I do not want to use vim. Thus, I have to start and navigate to my project in the command line as well as in my editor. Additionally, opening files in my editor instead of vim has to be done using my windows explorer.

Scripting to the rescue!

I wrote a small bash script, which can be used in cygwin/git bash environments for starting sublime:

#!/bin/sh
/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe $1 &

A few comments to this script:

  • The first line indicates that this is a script which is executed using /bin/sh (the standard shell)
  • The second line executes the sublime_text.exe. Note, that spaces in the folders had to be escaped using the backslash character (\).
  • $1 refers to the first parameter passed to this script. This can be a file or a folder which is passed on to Sublime Text 2 which in turn automatically opens the file or in case of a folder adds it to the current project.
  • The & sign at the end moves this process into the background. This allows to use the current command line further while Sublime Text 2 is running (as it is NOT running in the foreground of the command line. )

Of course, you have to chmod +x this file to make it executable and place it into a folder which is within the PATH. Perhaps renaming the script to sl would be nice in order to reduce typing.

But then, you can use the script as follows to open the current directory:

$> sublime .

Or open the test.txt file:

$> sublime test.txt

TextMate (solely available on the Mac) already has such a feature named mate.