15

I'm trying to write the output of strace ls to a file. I know that I need to use > in order to forward output of a command to a file, but it doesn't work. It creates a file but the command prints the output of strace ls to stdout but writes the file name into the file.

$ strace ls > ls_sys.txt
...
strace output
...
$ cat ls_sys.txt
ls_sys.txt
Greenonline
  • 1,759
  • 7
  • 16
  • 21
yeled zevel
  • 151
  • 1
  • 1
  • 3
  • Related: [piping strace to grep](https://unix.stackexchange.com/questions/48223/piping-strace-to-grep) however it will likely be simpler to use the strace `-o` option rather than shell redirection – steeldriver Aug 27 '20 at 17:57
  • Have you tried `strace -o ls_sys.txt ls`? – fpmurphy Aug 27 '20 at 18:06

2 Answers2

27

By default strace outputs to stderr. By simply typing man strace, you will have the full documentation of strace. In the manual page, it states that the -o option can be used to output it to a file instead of stderr. You can type man [insert command here] for the vast majority of programs, and have all the documentation you will need to effectively use them.

john doe
  • 746
  • 1
  • 12
  • 27
  • `By default strace outputs to stderr, which is not accessible via grep or file redirection`. Good to know! And I looked at the manual page of `strace` but I didn't see the `-o` option, my fault. Thanks! – yeled zevel Aug 27 '20 at 18:11
  • no problem, I actually just figured out this strace option myself yesterday. When you are in a man page, you can press `/` and it will bring a dialog that you can search. In my case, I simply searched for output and found the part of the man page I was looking for. – john doe Aug 27 '20 at 18:13
  • 2
    What on Earth makes you think that you cannot redirect standard error or arrange for it to be sent through a pipe to `grep`? – JdeBP Aug 27 '20 at 18:15
  • 6
    stderr can be redirected, using `2>`. See [this question](https://unix.stackexchange.com/q/159513/86440). – Stephen Kitt Aug 27 '20 at 18:16
  • @JdeBP lack of knowledge – john doe Aug 27 '20 at 18:19
  • A bash-ism I guess is `command |& command` it merges and pipes both _stdout_ and _stderr_ to the next command. – alkino Feb 02 '22 at 22:42
-2

For those looking for a command to run:

strace ./elf_file 2>strace_output.txt

  • 8
    Note that it redirects both the `strace` tracing and the `./elf_file` errors to `strace_output.txt`. For that reason, it's better to use `strace -o strace_output.txt ./elf_file` or `strace -o strace_output.txt ./elf_file 1> stdout.txt 2> stderr.txt` if you want traces, stdout and stderr in different files. – Stéphane Chazelas Jan 07 '22 at 12:57