Terminal: Exporting Text from Commands

| TMO Quick Tip

There are two great menu options in Terminal that I don’t think many folks know about. They’re labeled Export Text As and Export Selected Text As.

What these do is pretty simple but really helpful. Let’s say you’ve just run a command with a lot of output—fs_usage, for example, which I’ve written about before—and you decide that you want to save all the data that Terminal just gave you. If you think about it ahead of time, you can of course use the pipe command. I wrote about that in yet another article on Terminal a while back. (I’m sensing a pattern here on what I like to do tips about, you guys.)

But what do you do if you want to save what’s already in your Terminal window, without having to run commands again? That’s where Shell> Export Text As comes in. It’ll insert everything that’s been output in your current Terminal window to a text file that you can save anywhere.

One of the things that’s nifty about this is that it saves everything out since your Terminal window was opened, even if you quit the program in the meantime (as long as you’re running a recent version of the operating system that’ll let you resume where you left off in your applications, that is). If you’ve got System Preferences> General> Close windows when quitting an application toggled on, Terminal of course won’t reopen any windows you had from your previous sessions, so you’ll only be saving out what you’d done since the last time the program was opened. 

If what you’re looking to save out instead is not the entire contents of your Terminal window but only some of it, you can use Export Selected Text As. To try this, click and drag to select whatever portion of the Terminal output you intend to keep:

After you’ve got your text selected, pick Shell> Export Selected Text As from the menu at the top of your screen, and as before, you’ll be given an option to save it somewhere as a pretty little text file.

I find this handy quite often; sometimes it’s just easier to look through a ton of output in a text file rather than in Terminal. I mean, I love me some Terminal, but it ain’t always the most convenient way to perform tasks. Blasphemy, I know.

Sign Up for the Newsletter

Join the TMO Express Daily Newsletter to get the latest Mac headlines in your e-mail every weekday.

Comments

webjprgm

Is this functionally different than cmd-a, cmd-c, cmd-tab (to text app), cmd-v? Or cmd-a, cmd-v, then “pbpaste > output.txt” ?

Hmm, from your screenshot I see that the shortcut is cmd-s. I hit that all the time by accident in VI (when I switch between TextWrangler and VI too much and confuse myself), so I guess I’ve seen this feature before but always ignored it because it was the wrong function at the time.  Well now I know what that does!

vpndev

One point in favor of piped/redirected output is that certain output is in a more suitable format. In the screenshots in the article, for example, the list is in two columns. If you redirect it to a file then it will be a single column, one name per line.

Pipe/redirection is not a commonly-known capability, but it should be. It’s one of the cornerstone productivity capabilities of Unix.

Melissa Holt

Hey webjprgm!

It’s not different from copying and pasting in the end, but it does save some time. And I do like saving time. grin

And you’re right, vpndev—the formatting when you use a pipe is often superior, but I don’t know many casual users who know how to do that! My hope in writing this article was to assist people who might be using Terminal as a troubleshooting tool rather than the command-line power users. One day I aspire to be one of those…

—Melissa

Log-in to comment