X

Terminal fun: Options for printing folder and subfolder contents

Printing the contents of a folder in OS X currently still requires either third-party software or workarounds using TextEdit or the Terminal. Here are some ways to go about doing this.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
5 min read

Periodically you may have a collection of files that you may wish to catalog by printing a list of the file names out on a per-directory basis, or by saving the name list in a document. Unfortunately there is no way to do this directly in the Finder. There is a "Print" command in the Finder, but this will open a document in the preferred application and try to print it from there instead of printing the current folder view.

There are a number of ways to get around this limitation of OS X, some of which may be better than others depending on the circumstances, which include using TextEdit for simple listings and the Terminal for more complex ones, but for people who are not inclined on having fun with the Terminal, you can grab a Finder alternative such as "Path Finder" which does have the ability to print folder views.

Using the Finder and TextEdit

Besides third-party solutions, the only easy solution in OS X is to copy a folder's contents and paste it into a plain text document in a program such as TextEdit.

To do this, open the desired folder you want to list the contents of, and select all items (command-A) then copy them (command-C) and open TextEdit. Change the document's format to "Plain Text" in the "Format" menu and then paste the clipboard contents to the document.

The benefits of this are that it's simple and straightforward, and if you just want to catalog the contents of the current folder then this is all you need. However, it does not indicate which items are folders versus files, and does not display any heirarchies so if you are in the Finder list view and have a subdirectory expanded, upon pasting them all into the text document you will not see any indication of which are in the subdirectories.

Keep in mind that OS X treats many copied items as objects, so in trying to paste them into other programs the system may try embedding the copied items' content instead of just the name. For TextEdit and other text editors, using a plain text format will overcome this since the format only supports text characters, but for other document formats this may be an issue to be aware of.

The Terminal Approach

The fun way (at least, from a geek's point of view) to do this is to use the terminal to output and format directory contents.

One beneficial feature when running terminal commands is redirecting the output into a text file that you can then open and print. There are a number of terminal commands that will list and find files and folders on the drive, the most notable of which is the "ls" command. If you have a directory open in the terminal, you can use the "ls" command followed by a unix redirect to save the contents to a text file. The syntax for this would be as follows:

ls > ~/FILE.txt

In this command, the redirect ">" will create a file called "FILE.txt" in your home folder (indicated by the tilde "~") that will contain the output of the "ls" command. This can be used for any Terminal command, including "ls" and others that output the contents of directories and subdirectories in the system.

Unfortunately, the terminal is very syntax-aware, so typos in commands may give errors; however, with the following commands you cannot harm anything in your system, so feel free to play around with them and saving the output to files using the Unix redirect. For these commands, first use the "cd" command to go to the folder of your choice, and then run them to list the contents of the folder and subdirectories.

  1. List files in a directory:

    ls -l /path/to/folder

  2. List files on a per-subfolder basis:

    ls -lR /path/to/folder

  3. List all subfolders with formatted output (go ahead, copy and paste it to the Terminal)

    cd /path/to/folder
    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'

As you can see by that last option, the command-line options for formatting and clearly displaying folder contents can get complicated, and while it can be fun for IT geeks, for the average user it can be an exercise in frustration. There are a number of ways to format the output of file lists in OS X using the terminal, but one command that is quite useful is the "tree" tool that is available for a number of Unix systems, including OS X.

Installing and using "tree"

Unfortunately "tree" is not available as a precompiled binary, so if you want to use it you will need to compile and install it from source. The process is actually quite simple, but does require the presence of the Apple developer tools on your system, so here are the steps needed:

  1. Create a free ADC membership if you do not have one, and download and install Xcode (You wont actually use it, but you need some components): http://developer.apple.com/technology/xcode.html

  2. Download the source code for "tree": http://mama.indstate.edu/users/ice/tree/

  3. Unzip the downloaded file and you should have a folder called "tree-1.5.2.2" which you should open.

  4. Open the file called "Makefile" in TextEdit, and remove the hash marks (#) from the lines under where it says "# Uncomment for OS X:" so it looks like the following (only do this for these lines, and not for any others, and do not edit any other part of the file):

    # Uncomment for OS X:
    CC=cc
    CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp
    LDFLAGS=
    XOBJS=strverscmp.o

  5. Save and close the document, and then open the Terminal

  6. Enter "cd" followed by a space, and then drag the "tree-1.5.2.2" folder to the terminal window. The folder path should complete, so upon pressing enter the terminal should be pointing to the "tree-1.5.2.2" folder. To double-check that you are in the right location, enter "ls" in the terminal and you should see the output of all the files in the folder:

    Tophers-Laptop:tree-1.5.2.2 tkessler$ ls
    CHANGES		LICENSE		README	strverscmp.c	tree.c
    INSTALL		Makefile	man		tree
    
     

  7. With the current directory confirmed, enter the following command to compile the program:

    make

  8. After the program is compiled, run the following command to install it along with the other Unix tools on your system:

    sudo make install

With the program now installed, quit the terminal and restart it, and then enter "tree" to see the command display the contents of the current folder and any subfolders in a formatted output. You can use the Unix redirect "> ~/filename.txt" to save the output to a text file. For information on the options for "tree", enter "tree --help" at the command prompt.

The "tree" command running and outputting neatly formatted directory structure


Questions? Comments? Post them below or email us!
Be sure to check us out on Twitter and the CNET Mac forums.