Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Capture command line output directly to a text file UNIX
If you're running a Unix command (inside Terminal or otherwise), and you want to quickly capture the standard output in an editable buffer (for examination or snipping), just add | open -tf after the command.

The output of the command will be placed into a new TextEdit buffer, stored in the /private/tmp folder. For example, to get the output of the command-line version of System Profiler into a text file, just type (in the Terminal window):
system_profiler | open -tf
You can then edit the document, and save it (use File » Save As to pick a save location). This method saves a step over using a redirect (>) and then opening the resulting file.
    •    
  • Currently 2.22 / 5
  You rated: 4 / 5 (9 votes cast)
 
[27,233 views]  

Capture command line output directly to a text file | 19 comments | Create New Account
Click here to return to the 'Capture command line output directly to a text file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Capture command line output directly to a text file
Authored by: alittleknowledge on Jul 29, '08 07:44:37AM
Doesn't work for me in 10.4 - I get the following error:

$ system_profiler | open -tf
open[22070] No such file: /Users/notyou/-tf


[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: spicyj on Jul 29, '08 08:05:44AM
I believe you actually want system_profiler | open -t -f for it to work in Tiger. Also, the hint is incorrect. It will open in the default text editor. If you want to open TextEdit specifically, replace the "-t" with a "-e".

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: channui on Jul 29, '08 08:38:22AM

According to the 10.5 manpage you should only need to use '-f' to invoke the default text editor using stdin.

ls | open -f

If you want to open a file you can use:

open -t /path/to/some/file.txt



[ Reply to This | # ]
Or send it to the clipboard ...
Authored by: ManasTungare on Jul 29, '08 08:49:23AM
I find it more useful in many scenarios to copy to the clipboard instead.
system_profiler | pbcopy
E.g. I have a script that generates tab-separated values. By piping it to pbcopy and then pasting it into Numbers, I get a well-formatted spreadsheet with no intermediate files to save and open.

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: rspress on Jul 29, '08 09:26:02AM
He was talking about Tiger which has a little different usage of the open command.

To open a file in a textedit document you use:

system_profiler | open -e -f

the one listed in the main hint does not work under tiger.

To open a directory listing in textedit you would use:

ls | open -e -f

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: BlackPenguin on Jul 29, '08 09:51:06AM
If you find you need to do this a lot, you might consider using a better text editor than TextEdit. While there are plenty of options, my favorite is the free TextWrangler. Among many other features, it includes a set of command-line tools which aid in this regard:

system_profiler | edit
That will pipe the output from system_profiler to a new document in TextWrangler, which is more suited to handling that type of text than TextEdit, anyway.

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: Doc Drang on Jul 29, '08 09:54:24AM

And if you prefer to open the output in a more serious text editor, use

system_profiler | mate

for TextMate or

system_profiler | bbedit

for BBEdit.



---
Doc Drang
http://www.leancrew.com/all-this



[ Reply to This | # ]
Tee
Authored by: Colin Jensen on Jul 29, '08 10:37:40AM

The command you are looking for is tee. It passes the output through while saving a copy to a file

system_profiler | tee myfile.txt



[ Reply to This | # ]
Tee
Authored by: wjv on Jul 30, '08 01:54:43AM
And if you're trying to capture the output of an interactive process instead, use script(1).

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: megagram on Jul 29, '08 11:26:48AM
Why not just do it all on one line?
system_profiler > /path/to/profiler.txt && open -e /path/to/profiler.txt

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: Cerberus on Jul 29, '08 02:00:11PM

I think the primary consideration for NOT doing this is that you would then have to go and delete this file once you were done with it. By using a file stored in /tmp it is automatically deleted after 3 days.

But otherwise your point is valid.

And to argue AGAINST my own statement then your command could make the file in /tmp/* fashion.

OK, Base fact is that I think this tip works well due to its simplicity. If I don't care about the file (or file name) and do want the data then piping the output to 'open' would be more than acceptable and the system cleans up the resultant debris and I don't need to care about the initial file name or its location. Remember, there is potentially THOUSANDS of ways to skin data in *NIX.



[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: delight1 on Jul 29, '08 01:15:28PM
personally, i use aqualess, and its aless unix command, for piping to a gui.
http://aqualess.sourceforge.net/


[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: CarlRJ on Jul 29, '08 03:09:28PM

So, I'm late to this party, but I have to point out that the title of this article, "Capture command line output directly to a text file" is just plain wrong, as the direct obvious way to do what the title says is clearly:

system_profiler > /tmp/some_file_name

This puts the output of system_profiler directly into the file /tmp/some_file_name, while the hint, and all the comments, are directing the output to an editor of some sort, and suggesting you could then save the output. That can be a nice thing to do, but it doesn't fit the title.

And... on that front, I must offer:

system_profiler | vim -

Which keeps everything in the Terminal, and uses The Best Editor Ever.

But, please, if the hint remains the same, change the title to something like "Capture command line output into a text editor".

[ Reply to This | # ]

Capture command line output directly to a text file
Authored by: _mikk on Jul 29, '08 04:13:58PM

In order to use any other app:

open -f -a <application>

For example:

open -f -a Smultron



[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: Hunted Charlie on Jul 29, '08 04:18:01PM

Nice idea, I can't believe I haven't tried that before. I spend a good deal of my time on the command line and another good deal in TextMate. I had never thought of piping something straight to a GUI app, which I think is the really cool bit of this (piping to vi(m)/nano/emacs seems a bit pointless to me).



[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: drudus on Aug 01, '08 07:19:08AM
TextMate has a command line helper app (I think its installed via an option in its preferences). It means you can do mate myfile or folder to open a file in the current path to textmate, you can also pipe output into mate.
It's the best way to access TextMate via the CLI.

[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: mario_grgic on Jul 29, '08 04:42:36PM

Well, you can't call yourself a UNIX geek if you don't know how to re-direct std out (or error stream) to a file or device. Eg.

find / -name "*.txt" 2>/dev/null > list.lst

finds all files with .txt extension and puts the list to list.lst in current directory, and the error stream (e.g. you don't have a permission to list the directory or file) is sent to /dev/null (i.e. thrown away).

If you use (a real editor) vi/vim you can read from std out stream directly as in

ls / | vim -

which lists all files on your file system you are allowed to see and pipes them to vim which reads it from the stream.



[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: tom larkin on Jul 29, '08 05:39:49PM

I use a different option but here it is

cat /path/to/file | open -f -a "textwrangler.app"



[ Reply to This | # ]
Capture command line output directly to a text file
Authored by: leamanc on Jul 29, '08 07:01:59PM

What about the way people have been doing it since Unix has existed?
system_profiler > my_text_file.txt
Or if you want to append the output to an existing file, use ">>" instead of ">".
system_profiler >> my_existing_text_file.txt



[ Reply to This | # ]