Linux Redirect all output to file
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6674327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect all output to file
提问by Rayne
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.
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?
Is there a way to redirect all output to file?
采纳答案by Op De Cirkel
That part is written to stderr, use 2>
to redirect it. For example:
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:
or if you want in same file:
foo > allout.txt 2>&1
Note: this works in (ba)sh, check your shell for proper syntax
Note: this works in (ba)sh, check your shell for proper syntax
回答by Petar Ivanov
It might be the the standard error. You can redirect it:
It might be the the standard error. You can redirect it:
... > out.txt 2>&1
回答by Bryan Agee
All POSIX operating systems have 3 streams: stdin, stdout, and stderr. stdin is the input, which can accept the stdout or stderr. stdout is the primary output, which is redirected with >
, >>
, or |
. stderr is the error output, which is handled separately so that any exceptions do not get passed to a command or written to a file that it might break; normally, this is sent to a log of some kind, or dumped directly, even when the stdout is redirected. To redirect both to the same place, use:
All POSIX operating systems have 3 streams: stdin, stdout, and stderr. stdin is the input, which can accept the stdout or stderr. stdout is the primary output, which is redirected with >
, >>
, or |
. stderr is the error output, which is handled separately so that any exceptions do not get passed to a command or written to a file that it might break; normally, this is sent to a log of some kind, or dumped directly, even when the stdout is redirected. To redirect both to the same place, use:
command&> /some/file
command&> /some/file
EDIT: thanks to Zack for pointing out that the above solution is not portable--use instead:
EDIT: thanks to Zack for pointing out that the above solution is not portable--use instead:
*command* > file 2>&1
If you want to silence the error, do:
If you want to silence the error, do:
*command* 2> /dev/null
回答by Sagar Jain
To get the output on the console AND in a file file.txt
for example.
To get the output on the console AND in a file file.txt
for example.
make 2>&1 | tee file.txt
Note: &
(in 2>&1
) specifies that 1
is not a file name but a file descriptor.
Note: &
(in 2>&1
) specifies that 1
is not a file name but a file descriptor.
回答by khushi muhammad
Use >>
to append:
Use >>
to append:
command >> file
command >> file
回答by osexp2003
You can use exec
command to redirect all stdout/stderr output of any commands later.
You can use exec
command to redirect all stdout/stderr output of any commands later.
sample script:
sample script:
exec 2> your_file2 > your_file1
your other commands.....
回答by YouAreAwesome
Use this - "require command here" > log_file_name 2>&1
Use this - "require command here" > log_file_name 2>&1
Detail description of redirection operator in Unix/Linux.
Detail description of redirection operator in Unix/Linux.
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
If you don't specify a number then the standard output stream is assumed but you can also redirect errors
If you don't specify a number then the standard output stream is assumed but you can also redirect errors
> file redirects stdout to file
1> file redirects stdout to file
2> file redirects stderr to file
&> file redirects stdout and stderr to file
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
回答by Jerry
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.
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.
回答by ellockie
Command:
Command:
foo >> output.txt 2>&1
appendsto the output.txtfile, without replacing the content.
appendsto the output.txtfile, without replacing the content.
回答by qr?bn?
Credits to osexp2003 and j.a. …
Credits to osexp2003 and j.a. …
Instead of putting:
Instead of putting:
&>> your_file.log
behind a line in:
behind a line in:
crontab -e
I use:
I use:
#!/bin/bash
exec &>> your_file.log
…
at the beginning of a BASHscript.
at the beginning of a BASHscript.
Advantage: You have the log definitions within your script. Good for Git etc.
Advantage: You have the log definitions within your script. Good for Git etc.