See a tree-based view of folder structure

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

Every so often, I’ll run across something I find useful, and yet I have only the vaguest sense of exactly how it works. Today’s Geeky Friday tip falls into that category—it’s a one-line Terminal command to display the structure (i.e. all the sub-folders) of any given folder.

There are many ways to get this information in the Finder (or via third-party programs), but I’ve found it useful when remotely connecting to other Macs, or when I want a quick reminder of a folder’s structure while working in Terminal.

The command outputs an indented list showing all the sub-folders within the current folder, and it does so very quickly, even on large folders.

Here’s the command:

find . -type d | sed -e 1d -e 's/[^-][^/]*//--/g' -e 's/^/   /' -e 's/-/|-/'

To use it, simply cd into the directory whose structure you’d like to see, then run the command. If you use it a lot, you might want to put it in your user’s .bash_profile file as an alias—just prefix the above command with alias mytree=”, and then add the closing double-quote ( ) at the end. Save the file, and the next time you open a Terminal window, you’ll be able to simply type mytree to see the tree structure. Here’s an example of the output it creates:

	$ cd ~/Pictures

$ mytree
|--iPhoto Library
|--------Roll 1
|--------Roll 2
|--------Roll 3
|--------Roll 4
|--------Roll 5
|--------Roll 6
|--------Roll 7
|--------Roll 8
|----iPod Photo Cache

As I noted in the intro, I can’t tell you exactly how this works, because I really don’t know myself. It relies heavily on sed, a Unix program that can transform text in a multitude of ways ( man sed will give you a brief overview of its capabilities). I do know that the first bit, find . -type d, tells the system to find all directories (folders) at or below the current level. After that, though, the magic starts with sed, replacing characters as necessary to create the formatted output.

You can do other things with the output, of course. Append | more at the end to have it scroll by one page at a time, for instance. Or append > ~/Desktop/my_folders.txt at the end to send the output to a file on your desktop.

Although I usually prefer to understand exactly how the Unix commands I use accomplish their tasks, in this case I’ve decided that the usefulness of the command outweighs my desire to become an sed wizard to understand it!

Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.
Shop Tech Products at Amazon