8

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
2
  • Related: piping strace to grep however it will likely be simpler to use the strace -o option rather than shell redirection Aug 27 '20 at 17:57
  • Have you tried strace -o ls_sys.txt ls?
    – fpmurphy
    Aug 27 '20 at 18:06
15

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.

5
  • 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! 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
  • 5
    stderr can be redirected, using 2>. See this question. Aug 27 '20 at 18:16
  • @JdeBP lack of knowledge
    – john doe
    Aug 27 '20 at 18:19
-2

For those looking for a command to run:

strace ./elf_file 2>strace_output.txt

1
  • 4
    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. Jan 7 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.