Visualize the Directory Tree

Ever have to work with a new directory and you have no idea what its structure is? Or maybe you have a few files laying around but you’re not sure which sub-directory they are in? Or maybe you’re showing someone else a project and you want to show them the directory hierarchy. The bare bones solutions of `ls -R` or `find .` are just too archaic and offer no visualization of the structure. To solve this problem, people have built their own “tree” scripts.

There are a few tree scripts available online. Some as simple as find | sed and others are slightly more advanced like a python script. I wasn’t pleased with the existing solutions, so I wrote my own. To get an idea of what I’m talking about take a look at this screenshot showing the listing of a Rails project:


Its a simple, clean listing of the directory tree. I will admit, the style is based off of another tree script that I’ve seen that I liked. Also, this isn’t really production quality code. I take the lazy way out and first get a directory listing and then work from there. This means that for large directories there may be an initial pause before it starts outputting. I wouldn’t suggest running this on your home directory. Although I can think of better algorithms its unlikely that I would want to run this on huge directories so I’m more then happy right now.

The usage is pretty bare bones:

tree usage

This is one more script I’ve added to my ~/bin and its completely open source on GitHub. Its just straight Ruby, no extra packages, works with 1.8 and 1.9. Oh and did I mention its customizable?

I hope you like it.

Shell Brace Expansion

I would say that most developers that I know are comfortable with a shell/terminal but they are not proficient with the shell. I, for one, am constantly striving to improve my knowledge of the shell, customizing my environment, and trying to maximize my productivity with it. Even after years of constant usage I am always learning new tricks and techniques. It also doesn’t help that there are thousands of command line applications out there waiting to be absorbed.

My shell of choice, like most people I know, is bash. Most developers only use the basic features available in shells such as launching programs, command history, auto-completion, and the like. However, most shells have functionality people don’t even know about and could probably benefit from. Who knows, you might even be able to multitask and partake in foxy bingo online or pogo games while programming simultaneously. One such feature is Brace Expansion.

Take a look:

Brace Expansion

Its a little hard to see from the image so I’ll run down the examples here:

shell> echo hello{world,goodbye}
helloworld hellogoodbye

This example shows exactly what Brace Expansion is doing. Its taking the input line “hello{}” and replacing the “{}” section with each of the comma separated values, producing “helloworld” and “hellogoodbye.”

Its important to note that there is no space between “hello” and the opening brace. This is called the preamble and therefore it gets prepended to the front of both “world” and “goodbye.” Also, there is no space around the comma inside the braces. If there were a space then brace expansion doesn’t take place at all! The output itself will separate each expansion with a space, just like any other built-in shell expansion. If you really want spaces in the expansion portions you can escape them as you normally would and it will respect that.

shell> echo file.{txt,rb}
file.txt file.rb

This points out a couple things. First of all, I’m using echo to debug the brace expansion so you can see what it produced. What seemingly happens, like in the previous case, is that it ran the brace expansion when it parsed the input line, and turned it into the following:

shell> echo file.txt file.rb

Now some simple usages pop to mind. Need to rename a file? How about “mv filename.{txt,rb}.” Want to quickly backup a file, how about “cp filename.{txt,save}.” Whenever you feel yourself typing the same thing twice in a row that is a perfect candidate for brace expansion! Save yourself some keystrokes, some time, and potential errors by reusing what you’ve already typed in already!

The third usage is combining two separate but connected brace expansions “{}{}.” This works differently then two completely disjoint sets “{} … {}” and is still different then nested sets “{…{}…}.” Rather then discuss the semantics of those, which you can easily find out yourself by just trying them out, I’ll give you a glimpse of another trick you can do with brace expansion.

Here is a simple example of Sequences:

Brace Expansion Sequences

The syntax for sequences is “{#..#}.” You can even do simple character sequences like “{a..z}.” The second example from the picture shows a postscript, the opposite of the preamble. Its text that comes immediately after the braces (no space) and is thus appended to the end of each expansion.

I hope you give it try, you might end up liking it. I actually do some real neat tricks with it in one of my scripts. If you want to explore it further you should check out some of the more technical documentation:

Until next time, wrap your head around this crazy line. And no, there is no typo, its running a brace expansion once, which then outputs a very long command which includes brace expansion, and then runs that command with bash:

shell> echo echo {A..Z}\{0..{1..4}\} | bash

Check if Your Ruby Script is in a Pipe

Short and simple this week. When you’re writing a Ruby script you might want to know if you’re in a pipe or not. One reason might be changing how you buffer your input/output. A number of basic Unix commands do this, they act differently when piped.

I actually wrote a script where I wanted to change the command line arguments if I was in a pipe. This is probably confusing but its really useful! So there is actually an easy way to do this in Ruby:

#!/usr/bin/env ruby
# Prints if the input or output is regular or piped
puts "INPUT:  #{STDIN.tty?  ? "regular" : "pipe"}"
puts "OUTPUT: #{STDOUT.tty? ? "regular" : "pipe"}"

Proof that it works as advertised:


The documentation on IO#tty? says the following:

Returns true if [the IO stream] is associated with a terminal device (tty), false otherwise.

For those that don’t know, tty is short for teletype writer. This is an old Unix term for an interactive process that takes in user input. Nowadays it almost always means a shell/console/terminal.

Ruby Readline Documentation

Before today the Ruby readline library lacked documentation! In order to find some decent documentation you would have to read the README packaged with the source. Michael Fellinger (manveru) from #ruby-lang offered to help me get some decent documentation online. Take a look!


Mini MP3 Searching Shell – Skreemr

So its way to hard to download an mp3 in Safari. Right click the link and download? Pff, I want to ⌃S and be done with it. Well, this time I decided to avoid the problem all together. I use Skreemr to search for a particular song when it interests me.

In the past I wrote a little bash script, that makes use of curl, to download an mp3 to my desktop unique named so it wouldn’t have conflicts. This shell essentially wraps and drastically improves that to allow for searching, pagination, history, downloading, and opening mp3s off of Skreemr. It gives me just what I need. The functionality that I want without having to use torrents etc. I’m thinking of turning this into a gem.


This script requires the popular “escape.rb” script that gives some nice and safe shell escaping functions. You can download both from my GitHub scripts project.

Of course its available on my ~/bin and there will be another article later on that goes over a few aspects of this simple little script.


Instant Web Sharing on Your Mac

I came across a few articles recently that point out how to instantly share a folder on your computer. They basically ride on top of this elegant python script:

python -m SimpleHTTPServer

It works great but I wanted to improve on it in a number of ways:

  • Automatically copy a URI into my clipboard so I can easily paste it to others.

  • Make that URI nicer then just an IP address.

  • Use a non-standard port, for security.

  • Open in a new tab so I can keep working in the directory and yet still monitor the HTTP requests being made.

Here was what I produced. (Its up in my ~/bin.)

# Start Date: Sunday February 8, 2009
# Current Version: 0.9
# Author: Joseph Pecoraro
# Contact:
# Decription:  Immediately Share the current directory
#   in a new tab so you can monitor the requests made
#   have your original tab to continue working in that
#   directory.  Meant for Mac OS X.
#   1. Echos the URI
#   2. Puts the URI into your Clipboard
#   3. Opens a new tab in the terminal
#   4. Changes Directory to the other tabs directory
#   5. Echos the URI
#   6. Runs the Web Server
#   7. Optionally Opens in Safari
# Sources that Helped:
#   New Tab Here:
#   HTTPServer:
#   Paul Berens:

# -----------------
#   Host and Port
# -----------------

# This gets your ip address and converts it to a nice string
es_host=$(curl --silent
es_host=$(nslookup $es_host | awk '/name =/{print substr($4,1,length($4)-1)}')

# -----------------
#   Script Below
# -----------------

echo "http://$es_host:$es_port"
echo -n "http://$es_host:$es_port" | pbcopy
osascript -e "
Tell application \"Terminal\"
  tell application \"System Events\" to tell process \"Terminal\" to keystroke \"t\" using command down
  do script with command \"cd '$(pwd)'\" in selected tab of the front window
  do script with command \"clear; echo '$es_host:$es_port/'\" in selected tab of the front window
  do script with command \"python -m SimpleHTTPServer $es_port\" in selected tab of the front window
end tell" &> /dev/null

# Optional: Open Safari, Just Uncomment the next line
# open "http://$es_host:$es_port"

# Cleanup
unset es_host
unset es_port

Now that should work on any Mac. And it should give a nicer URL then an ugly IP address. You should see something like this:


As soon as it starts you can paste the URL to anyone you’re chatting with. It couldn’t be simpler!

If you’re experienced enough with DNS servers and you’ve given your computer a Dynamic Name you can customize the script. Paul Berens gave me a great suggestion to determine if I’m on my local network at home. I can check the MAC address of my default gateway (my wireless router). That is a quick check to see if I’m at home. If I’m at home I use my URI automatically! Otherwise it defaults to generating the dynamic address generation. Check it out:

# -----------------
#   Host and Port
# -----------------

# Mac Address of my Router At Home
if [ -n "$(arp -a | grep 0:1e:2a:76:17:98)" ]; then
# Otherwise Dynamically Determine
  es_host=$(curl --silent
  es_host=$(nslookup $es_host | awk '/name =/{print substr($4,1,length($4)-1)}')

So now when I run easy_share at my house it always throws out URIs. Much nicer on the eyes and easy to remember. I’ll write about dynamic names like this another time!

Updates to line.rb – Open to more suggestions

I was chatting with a friend the other day and he had some really good ideas concerning last week’s line.rb script. He wants to make an improved version. I look forward to seeing what he will come up with because he shared some great ideas. Well, I couldn’t help but implement them immediately in my version. His ideas also spurred a little creativity from me and I came up with a few new features as well. The usage is now looks like:

  # Check Cmd Line Args and Print Usage if needed
  if ARGV.size <= 1
    puts "usage: line [options] filename numbers"
    puts " options:"
    puts "   --silent or -s   Just print the line, without [#]"
    puts " number formats:"
    puts "   1-3              Prints lines 1 through 3"
    puts "   5                Prints line 5"
    puts "   -1               Prints the last line, (negative)"
    puts " extra formats:"
    puts "   ~5               Prints 2 (default) lines before and after 5"
    puts "   4~10             Prints 4 lines before and after 10"
    puts "   *7 or 8*         Prints all lines before or after the number"
    puts "   5/1              Prints 5 lines, then skips 1 line..."
    puts "   2:5/1            Starts at line 2, prints and skips..."
    exit 1

So there is some cool new syntax:

  • ~# – prints 2 lines before and after the given line number

  • #~# – prints the given number of lines before and after the given line number

  • *# – prints all of the lines before the given line number

  • #* – prints all of the lines after the given line number

  • #/# – is a print and skip option

  • #:#/# – allows you to provide an offset

Lets say you want to print every even line in a text file. With this script that is not a problem. The solution is: starting on the 2nd line, print 1 line, then skip 1 line. (line file 2:1/1):

Print only even lines using line.rb

I think these new formats are pretty neat. You can always access the latest version of the script on Github or in my ~/bin. I’m willing to change the syntax and introduce new concepts. Send suggestions my way by dropping a comment! Cheers.