Redirect all output to file [duplicate]

2018-12-31 20:44发布

This question already has an answer here:

I know that in Linux, to redirect output from the screen to a file, I can either use the > or tee. However, I'm not sure why part of the output is still output to the screen and not written to the file.

Is there a way to redirect all output to file?

10条回答
长期被迫恋爱
2楼-- · 2018-12-31 21:19

You can use exec command to redirect all stdout/stderr output of any commands later.

sample script:

exec 2> your_file2 > your_file1
your other commands.....
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 21:19

Command:

foo >> output.txt 2>&1

appends to the output.txt file, without replacing the content.

查看更多
像晚风撩人
4楼-- · 2018-12-31 21:19

In Linux Mint, this command string routed executing script and errors to a single txt file. bash -x ./setup.sh > setup.txt 2>&1. Script name was setup.sh and output destination was setup.txt.

查看更多
只靠听说
5楼-- · 2018-12-31 21:29

It might be the the standard error. You can redirect it:

... > out.txt 2>&1
查看更多
冷夜・残月
6楼-- · 2018-12-31 21:31

Credits to osexp2003 and j.a. …


Instead of putting

&>> your_file.log

behind a line in

crontab -e

I use

#!/bin/bash
exec &>> your_file.log
…

at the beginning of a BASH script.

Advantage: You have the log definitions within your script. Good for Git etc.

查看更多
浅入江南
7楼-- · 2018-12-31 21:34

That part is written to stderr, use 2> to redirect it. For example:

foo > stdout.txt 2> stderr.txt

or if you want in same file:

foo > allout.txt 2>&1

Note: this works in (ba)sh, check your shell for proper syntax

查看更多
登录 后发表回答