Terminal Tip: How to quickly open output in a text file

TerminalIconXWhen using the OS X Terminal, you will be dealing with text output from the various commands you run, be it a directory listing after running the “ls” command, or brief help for some commands that will show when you enter just the command with no additional arguments. Regardless of what it is, you may find that frequently the output can be extensive, and may wish to save it in a more permanent way.

The common way of doing this in the Unix Terminal is to use a redirect (greater-than character) after your entered command, which will save any standard output to a file. For instance, to save the listing of the current directory to a file called “listings.txt” on your Desktop, you would run the following command:

ls > ~/Desktop/listings.txt

This notation is common for those familiar with the Terminal, and this “listings.txt” file can then be opened directly in TextEdit or any other editor, as needed; however, in OS X there is a special command that can be used to present command output in a new TextEdit document, and it is just as easy to use.

“| open -fe” to the rescue!

Instead of using the greater-than symbol, simply use a pipe (vertical bar) to direct the output to the “open” command, where by using the “-f” and “-e” flags you can have the piped command output open as a new TextEdit document. For instance, the following command will list the quick help for the “networksetup” command:

networksetup | open -fe

…and the following command will list the contents of the current directory, and similarly show it in a new TextEdit document:

ls | open -fe
open -fe in OS X Terminal

The “| open -fe” suffix here opens the output of the “ls” command in a temporary TextEdit document.

This approach is easy to memorize, and a quick way to handle practically any text from the Terminal, and can be exceptionally useful as yet another option for handling your workflow in the Terminal. It can be especially useful if you are instructing others to get information about their Macs using Terminal commands, where having the output show up in a text document makes it far more user-friendly to those who are perhaps not so familiar with the Terminal.

Note that when you use the “open” command in this way, the document that is created will be located in your account’s temporary folder, which is a hidden system-specified folder that is regularly emptied by OS X. As a result, any documents created in this manner that you do not purposefully save elsewhere will eventually be removed from your system.

5 thoughts on “Terminal Tip: How to quickly open output in a text file

  1. Strod

    Excellent tip. A couple of comments:

    The “e” option seems unnecessary. In my tests, “open -fe” and “open -f” produce the exact same result. That’s because “-e” opens (a document) in TextEdit while “-f” opens whatever is passed to it in the default text editor… which is TextEdit!

    An alternative for those who use TextWrangler is to pipe the text to the “edit” command, like this:

    networksetup | edit
    ls | edit
    etc.

    For this to work, you need to have TextWrangler’s command line tools installed; if you didn’t do this when you first ran TextWrangler, choose “Installl Command Line Tools…” from the “TextWrangler” menu.

    For those who may not know: TextWrangler is a fantastic free text editor by Bare Bones Software. It is almost a must-have tool for all medium-to-power users. If your needs are more demanding, you may want to purchase Bare Bones’ high end editor, BBEdit.

    If you don’t have TextWrangler/BBEdit’s command line tools installed, the command “edit” will instead open the default text editor for the Terminal (either ed, vi, emacs or pico; I don’t remember which one is he default).

    1. Topher Kessler Post author

      Tue the -f flag should be all that’s needed, but if you have another default text editor, the “e” flag will ensure it opens in TextEdit. I guess that’s been my approach to this, to ensure it opens in TextEdit, but the “e” flag is optional.

      TextWrangler’s command line tools are great options (though I believe they only come with the one downloaded from BareBones’ Web site, and not from the App Store?–could be wrong there).

    1. Topher Kessler Post author

      Black text on white background is the default. If you go to the Preferences and the to the Profiles section, you can choose from some alternative themes there, or customize your own. I like the Homebrew profile, which is the green on black. I then tweak the background color to be slightly transparent.

  2. alex M

    Good info here. For me “ls | edit” opens the text in Text Wrangler, even tho BBedit is my default text app. (for OS X, perhaps not for bash) with Command Line tools enabled. I found that using “ls | bbedit” will open the text in BBedit.

Comments are closed.