955

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?

0

9 Answers 9

1511

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

Sign up to request clarification or add additional context in comments.

12 Comments

well, 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" :)
also 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"
Two 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 the 2>&1 after the | will redirect stderr for cmd2 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. by ssh). If you need to redirect that too, the shell cannot help you, but expect can.
Change > to >> to append instead of overwrite. Kinda obvious but worth mentioning.
@DustinGriffith So, foo >> allout.txt 2>>&1 or foo >> allout.txt 2>&1?
|
204

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

3 Comments

seems &> and 2>&! both doing the same thing ?
&> 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 from csh). In a portable shell script (and if you don't need portability, why are you writing a shell script?), use > file 2>&1 only.
&> 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.
135

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.

1 Comment

by the way, tee -a file.txt means appends to a file.
69

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.

3 Comments

Redirection operator in Unix/Linux? Which shell?
this will work in both unix and linux and irrespective of shell we used.
Apparently &> is an extension supported by some shells, but not specified by POSIX. Be prepared for it to break in #!/bin/sh scripts if dash 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.
59

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.

2 Comments

Exactly 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
I 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 (&>>).
24

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.....

Comments

19

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

... > out.txt 2>&1

Comments

17

Command:

foo >> output.txt 2>&1

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

1 Comment

it worked for me inside a crontab for scripts in R, Ubuntu 14.
8

Use >> to append:

command >> file

2 Comments

This is related to the original question, but does not answer it.
Does not answer, but commonly misunderstood and the first answer in fact suggested that > and >> and | were interchangeable. ie - this was not worth your downvote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.