Redirect all output to file in Bash
Solution 1
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
Solution 2
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
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:
$command 2> /dev/null
Solution 3
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.
Solution 4
Use this - "require command here" > log_file_name 2>&1
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.
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.
Solution 5
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.
Rayne
Updated on May 15, 2021Comments
-
Rayne about 3 years
I know that in Linux, to redirect output from the screen to a file, I can either use the
>
ortee
. 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?
-
shelleybutterfly almost 13 yearswell, i found the reference and have deleted my post for having incorrect information. from the bash manual: '"ls 2>&1 > dirlist" directs only the standard output to dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist" :)
-
shelleybutterfly almost 13 yearsalso from the bash man "There are two formats for redirecting standard output and standard error: &>word and >&word Of the two forms, the first is preferred. This is semantically equivalent to >word 2>&1"
-
Marin Sagovac over 11 yearsInteresting, when I'm setting to
top > stdout.txt 2> stderr.txt
it will output onstdout.txt
but if is likefoo > stdout.txt 2> stderr.txt
it will only result onstderr.txt
not onstdout.txt
(blank file) -
ARH over 11 yearsseems &> and 2>&! both doing the same thing ?
-
zwol almost 11 yearsTwo important addenda: If you want to pipe both stdout and stderr, you have to write the redirections in the opposite order from what works for files,
cmd1 2>&1 | cmd2
; putting the2>&1
after the|
will redirect stderr forcmd2
instead. If both stdout and stderr are redirected, a program can still access the terminal (if any) by opening/dev/tty
; this is normally done only for password prompts (e.g. byssh
). If you need to redirect that too, the shell cannot help you, butexpect
can. -
zwol almost 11 years
&> file
(aka>& file
) is not part of the official POSIX shell spec, but has been added to many Bourne shells as a convenience extension (it originally comes fromcsh
). In a portable shell script (and if you don't need portability, why are you writing a shell script?), use> file 2>&1
only. -
Dustin Griffith almost 10 yearsChange
>
to>>
to append instead of overwrite. Kinda obvious but worth mentioning. -
Jacob Bryan over 9 yearsThis doesn't work if your program sends an error. What should you do when you want even that output into the log file??
-
Geremia over 9 years@DustinGriffith So,
foo >> allout.txt 2>>&1
orfoo >> allout.txt 2>&1
? -
Dustin Griffith over 9 years@Geremia This is really easy to test by yourself but I will go ahead and say to use
2>&1
. -
Geremia over 9 years@DustinGriffith, Yes, just using
2>&1
does work; I tried it. -
Gábor Bakos about 9 yearsThis is related to the original question, but does not answer it.
-
davea0511 about 9 yearsDoes not answer, but commonly misunderstood and the first answer in fact suggested that > and >> and | were interchangeable. ie - this was not worth your downvote.
-
Lanti over 8 yearsCan we limit the filesizes of stdout and stderr somehow? I not want to dockerise supervisord for this. It would be nice if it's possible.
-
deldev over 8 yearsthe
&1
sufix means that the output will be the first parameter of the command, in the case,allout.txt
-
RhinoDevel about 8 yearsRedirection operator in Unix/Linux? Which shell?
-
Indrajeet Gour about 8 yearsthis will work in both unix and linux and irrespective of shell we used.
-
jcarlos almost 8 yearsit worked for me inside a crontab for scripts in R, Ubuntu 14.
-
Lion Lai almost 7 yearsby the way, tee -a file.txt means appends to a file.
-
Paolo Benvenuto about 6 years&> is not implemented in sh, so beware with commands executed from cron files, crontabl uses sh to run the commands unless you tell it to use another shell.
-
The Bndr almost 6 yearsKeep in mind, that
>
will create/empty the destination file, before the command executes. This is an problem, if you like to modify the file content.cat longlisting.txt | sort > longlisting.txt
will result in an empty file. In this case better use| tee
instead of>
-
verboze over 5 yearsExactly what I wanted, thanks! In my case, I am not directly executing the script (some wrapper script I don't control is calling it), so I cannot specify the input redirect after the command
-
qräbnö over 5 yearsI was happy to support you. By the way, I'd like to note that the example I'm not using (crontab) doesn't even work because crontab uses SH and not BASH. To get further here, the other answers can help. And if you don't know,
&>> /dev/null
only works with BASH and redirects both STANDARD_OUT (1) and STANDARD_ERROR (2) to Nirvana (3 is STANDARD_IN). In this particular case, it makes no difference whether you overwrite (&>
) or append (&>>
). -
Peter Cordes over 3 yearsApparently
&>
is an extension supported by some shells, but not specified by POSIX. Be prepared for it to break in#!/bin/sh
scripts ifdash
is your /bin/sh. In dash, it parses the same as two separate commands:foo &
, i.e. running the command in the background with output connected to the shell's stdout (e.g. the terminal), and then separately>output
which truncates the output file. The other ones are standard syntax, and all of them work in Bash and Zsh, and other typical interactive shells that modern GNU/Linux distros provide as their default.