Gdb print to file instead of stdout

I've found that you can redirect the output from gdb to a file via the run command:

(gdb) run > outfile

You need to enable logging:

(gdb) set logging on

Now GDB will log to ./gdb.txt. You can tell it which file to use:

(gdb) set logging file my_god_object.log

And you can examine the current logging configuration:

(gdb) show logging

Extending on @qubodup's answer

gdb core.3599 -ex bt -ex quit |& tee backtrace.log

the -ex switch runs a gdb command. So the above loads the core file, runs bt command, then quit command. Output is written to backtrace.log and also on the screen.

Another useful gdb invocation (giving stacktrace with local variables from all threads) is

gdb core.3599 -ex 'thread apply all bt full' -ex quit