Save Terminal Output command on OSX

14,410

Assuming command is the command you want to run, you can pipe it's output into the output.txt using:

$ command > output.txt

or

$ command | tee output.txt

If this doesn't hell it may be because the output is not printed on stdout but on stderr. The pipe > is implicit writing for 1> which means "pipe stdout to ...".
If you want to pipe stderr into the file too, you can use

$ command >output.txt 2>&1

This pipes all the output of stderr to the file-descriptor 1 (=stdout) which in turn is piped into the text file.

Share:
14,410

Related videos on Youtube

hzleonardo
Author by

hzleonardo

Updated on September 18, 2022

Comments

  • hzleonardo
    hzleonardo over 1 year

    It is simple question but couldn't find the answer, I found and tried tee output.txt and system_profiler > output.txt but no success, they save blank text file. How do I save my terminal output with command in terminal on OSX El Capitan?

    • BloodyEl
      BloodyEl almost 8 years
      Your command should work fine. Please see my tested answer.
    • hzleonardo
      hzleonardo almost 8 years
      Yes you are right, I waited like 1-2 minutes and text file is not blank anymore but it doesn't save the output, it saves application's detail stuff. I am speaking of system_profiler > output.txt
    • BloodyEl
      BloodyEl almost 8 years
      "system_profiler > output.txt" has the exact same effect as "system_profiler", except saved to a file. "system_profiler -xml > output.txt" will print in an xml format that is sometimes useful but not what you are looking for. Can you show me the difference you get between "system_profiler" and the file from: "system_profiler > output.txt"? I'm using exactly the same syntax as you. I get exactly the same thing, the expected result.
    • BloodyEl
      BloodyEl almost 8 years
      There is something missing. Either you are cancelling the command mid-way, or you are not satisfied with the original output of "system_profiler" as it is without printing. Whether printing to file or not, the result is massive. If you only want to limit the results to the first paragraph, then use "system_profiler --timout 5 > output.txt" and it will stop itself. That is not a printing issue. Please explain how the result in the file is different from the result on screen. CONFIRM YOU ARE NOT SENDING SIGKILL [CTRL-C] etc.... because then we are not doing the same thing at all.
    • hzleonardo
      hzleonardo almost 8 years
      Yes I do CTRL-C. When I do it there is no problem when I save terminal output manually, why would it be a problem to do it with command?
  • dave_thompson_085
    dave_thompson_085 almost 8 years
    I don't have Mac but every shell I've ever seen does explicit (i.e. other than pipe) redirections in the order given, so you need >output 2>&1. Alternatively in bash you can use &>output or >&output to do both at once; see the man page.
  • ljrk
    ljrk almost 8 years
    I only use OSX in a Virtual Machine, my primary is Linux -- and every shell I've used works with both ways.
  • dave_thompson_085
    dave_thompson_085 almost 8 years
    Not the two Linuxes I currently use: on Ubuntu(14) with bash or dash, doing cat bad 2>&1 >output sends the error message to the tty and nothing to the file; on CentOS(6) with bash or dash or ksh93 the same, and with tcsh it gives an error Ambiguous output redirection. Could you give me an example I can download (on x86-64) or connect to that works for that form?
  • hzleonardo
    hzleonardo almost 8 years
    Am I the only one who is doing something wrong? I see these commands everywhere but it doesn't work for me, I waited, still blank text file. Are you el capitan 10.11.5?
  • hzleonardo
    hzleonardo almost 8 years
    Still blank text file and no success, it may be an el capitan issue? Are you on latest el capitan?
  • ljrk
    ljrk almost 8 years
    @dave_thompson_085 I just noticed I only use command 2>&1 | tee output.txt which does work this way around, but you're right, for > it doesn't. I never noticed O.o
  • ljrk
    ljrk almost 8 years
    @hzleonardo I just fired up ElCapitan 10.11.5 and yes it works, you just need to give it some time. You do not even need the stderr-redirection except you want to get info about devices that didn't respond etc. For me it took quite some time: time system_profiler > out.txt 2> err.txt 23.36s user 39.51s system 96% cpu 1:04.97 total, so perhaps just wait ...
  • hzleonardo
    hzleonardo almost 8 years
    Yes same result for me too which I don't want. I am trying to do "Export Text As" in shell menu with a command. When I do it manually it's ok, only print's from terminal in the text file in a second but with commands, blank file or this output in text after 1-2 minutes.