Redirect both standard output and standard error to different file in the same command [duplicate] - linux

I know this much:
$ command 2>> error
$ command 1>> output
Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?

Just add them in one line command 2>> error 1>> output
However, note that >> is for appending if the file already has data. Whereas, > will overwrite any existing data in the file.
So, command 2> error 1> output if you do not want to append.
Just for completion's sake, you can write 1> as just > since the default file descriptor is the output. so 1> and > is the same thing.
So, command 2> error 1> output becomes, command 2> error > output

Try this:
your_command 2>stderr.log 1>stdout.log
More information
The numerals 0 through 9 are file descriptors in bash.
0 stands for standard input, 1 stands for standard output, 2 stands for standard error. 3 through 9 are spare for any other temporary usage.
Any file descriptor can be redirected to a file or to another file descriptor using the operator >. You can instead use the operator >> to appends to a file instead of creating an empty one.
Usage:
file_descriptor > filename
file_descriptor > &file_descriptor
Please refer to Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.

Like that:
$ command >>output 2>>error

Or if you like to mix outputs (stdout & stderr) in one single file you may want to use:
command > merged-output.txt 2>&1

Multiple commands' output can be redirected. This works for either the command line or most usefully in a bash script. The -s directs the password prompt to the screen.
Hereblock cmds stdout/stderr are sent to seperate files and nothing to display.
sudo -s -u username <<'EOF' 2>err 1>out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to a single file and display.
sudo -s -u username <<'EOF' 2>&1 | tee out
ls; pwd;
EOF
Hereblock cmds stdout/stderr are sent to separate files and stdout to display.
sudo -s -u username <<'EOF' 2>err | tee out
ls; pwd;
EOF
Depending on who you are(whoami) and username a password may or may not be required.

Related

Linux: tee usage misunderstanding

When I use
$ ls | tee log.txt
I get correct, expected result: log.txt keeps 'ls' output.
I need to save output of the command:
$ svnadmin dump MyRepo -r1:r2 > dumpfile
* Dumped revision 1.
* Dumped revision 2.
Where "$ svnadmin .." is command and
"* Dumped .." are outputs
So, the question itself. When I run the command:
$ svnadmin dump MyRepo -r1:r2 > dumpfile | tee log.txt
* Dumped revision 1.
* Dumped revision 2.
log.txt has 0 size
TL;DR -- Try this one:
svnadmin dump MyRepo -r1:r2 2>&1 > dumpfile | tee log.txt
Explanation: Each command has three connected "standard streams": Standard Input (STDIN, filehandle 0), Standard Output (STDOUT, 1) and Standard Error (STDERR, 2). Usually commands ouput their "data" on STDOUT and errors on STDERR.
A simple command like ls has all three of them connected to the console. Because both STDOUT and STDERR are connected to the console the output of the command is interleaved.
A "pipe" like ls | tee log.txt redirects STDOUT of the first command to STDIN of the second command. No more - no less. Therefore all other streams are still connected to the console. Should the ls part produce error messages they will be written to the console, not to the file! But your ls example did not output any errors so you didn't notice.
After the pipe is setup the shell evaluates the other redirection operator of the first command -- from left to right.
Therefore svnadmin dump > dumpfile | tee log.txt will redirect STDOUT of svnadmin to dumpfile leaving effectively no data for the tee command because that's a redirection, not a copy.
svnadmin dump MyRepo 2>&1 > dumpfile | tee log.txt adds another redirection step. It reads "make filehandle 2 (STDERR) a copy of the filehandle 1 (STDOUT)" which is the pipe at this point. Writing to either STDOUT or STDERR will write to the pipe. But after that the > dumpfile redirection is applied and STDOUT is redirected to the file.
You can read all this (and much more) in the shell's manual. For bash it is in the section REDIRECTION.

How to save linux command line outputs to a .txt file?

I would like to make a .sh executable that inputs a terminal command, and saves the output to a .txt file.
For example, take the output of i2cdump and save to a file. The terminal commands for this are
i2cdump -r -y 0x0-6 0 0x68
"outputs the specified bytes 0x0-6 to terminal window"
How can I use a .sh executable to do this automatically, and save the output to a file stored in /dir/?
Some pseudo code I have for myfile.sh:
#!/bin/bash
output=$(i2cdump -r -y 0x0-6 0 0x68)
FILE * fp;
// write output to file
// save to directory
close(fp)
You can using shell redirection like so:
echo "Hello world" > greetings.txt
Or to suit your needs:
#!/bin/bash
i2cdump -r -y 0x0-6 0 0x68 > output.txt
# closing is automatic at the end of redirecting.
General information about shell redirection of standart output stream in Bash manual: Redirections
Any command can have > appended to it to redirect the output.
As in:
echo "foo" > /path/to/file
Note, there are two things you should know:
1 ">" is overwriting a file while >> is appending.
As in:
echo "foo" > /path/to/file
file content will be:
foo
while
echo "foo" > /path/to/file
echo "foo2" >> /path/to/file
file content will be:
foo
foo2
And also, if you want to redirect errors you can use the 2 operand.
As in:
cat /path/to/non-existing-file 2> /outputfile
Will write all the operation's error into outputfile.
The same > and >> logic applies.

What is meant by 'output to stdout'

New to bash programming. I am not sure what is meant by 'output to stdout'. Does it mean print out to the command line?
If I have a simple bash script:
#!/bin/bash
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello'
it outputs a string to the terminal. Does this mean it's 'outputting to stdout' ?
Thanks
Yes, stdout is the terminal (unless it's redirected to a file using the > operator or into the stdin of another process using |)
In your specific example, you're actually redirecting using | grep ... through grep then to the terminal.
Every process on a Linux system (and most others) has at least 3 open file descriptors:
stdin (0)
stdout (1)
stderr (2)
Regualary every of this file descriptors will point to the terminal from where the process was started. Like this:
cat file.txt # all file descriptors are pointing to the terminal where you type the command
However, bash allows to modify this behaviour using input / output redirection:
cat < file.txt # will use file.txt as stdin
cat file.txt > output.txt # redirects stdout to a file (will not appear on terminal anymore)
cat file.txt 2> /dev/null # redirects stderr to /dev/null (will not appear on terminal anymore
The same is happening when you are using the pipe symbol like:
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello'
What is actually happening is that the stdout of the wget process (the process before the | ) is redirected to the stdin of the grep process. So wget's stdout isn't a terminal anymore while grep's output is the current terminal. If you want to redirect grep's output to a file for example, then use this:
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello' > output.txt
Unless redirected, standard output is the text terminal which initiated the program.
Here's a wikipedia article: http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29

Command output redirect to file and terminal [duplicate]

This question already has answers here:
How to redirect output to a file and stdout
(11 answers)
Closed 4 years ago.
I am trying to throw command output to file plus console also. This is because i want to keep record of output in file. I am doing following and it appending to file but not printing ls output on terminal.
$ls 2>&1 > /tmp/ls.txt
Yes, if you redirect the output, it won't appear on the console. Use tee.
ls 2>&1 | tee /tmp/ls.txt
It is worth mentioning that 2>&1 means that standard error will be redirected too, together with standard output. So
someCommand | tee someFile
gives you just the standard output in the file, but not the standard error: standard error will appear in console only. To get standard error in the file too, you can use
someCommand 2>&1 | tee someFile
(source: In the shell, what is " 2>&1 "? ). Finally, both the above commands will truncate the file and start clear. If you use a sequence of commands, you may want to get output&error of all of them, one after another. In this case you can use -a flag to "tee" command:
someCommand 2>&1 | tee -a someFile
In case somebody needs to append the output and not overriding, it is possible to use "-a" or "--append" option of "tee" command :
ls 2>&1 | tee -a /tmp/ls.txt
ls 2>&1 | tee --append /tmp/ls.txt

how to redirect result of linux time command to some file

I'm running the following command (on Ubuntu)
time wget 'http://localhost:8080/upLoading.jsp' --timeout=0
and get a result in the command line
real 0m0.042s
user 0m0.000s
sys 0m0.000s
I've tried the following:
time -a o.txt wget 'http://localhost:8080/upLoading.jsp' --timeout=0
and get the following error
-a: command not found
I want to get the result to be redirected to some file. How can I do that?
-a is only understood by the time binary (/usr/bin/time), When just using time you're using the bash built-in version which does not process the -a option, and hence tries to run it as a command.
/usr/bin/time -o foo.txt -a wget 'http://localhost:8080/upLoading.jsp' --timeout=0
Checking man time, I guess what you need is
time -o o.txt -a ...
(Note you need both -a and -o).
[EDIT:] If you are in bash, you must also take care to write
/usr/bin/time
(check manpage for explanation)
You can direct the stdout output of any commmand to a file using the > character.
To append the output to a file use >>
Note that unless done explicitly, output to stderr will still go to the console. To direct both stderr and stdout to the same output stream use
command 2>&1 outfile.txt (with bash)
or
command >& outfile.txt (with t/csh)
If you are working with bash All about redirection will give you more details and control about redirection.
\time 2> time.out.text command
\time -o time.out.text command
This answer based on earlier comments. It is tested it works. The advantage of the \ over /usr/bin/ is that you don't have to know the install directory of time.
These answers also only capture the time, not other output.
Exactly the time from GNU writes it's output to stderr and if you want to redirect it to file, you can use --output=PATH parameter of time
See this http://unixhelp.ed.ac.uk/CGI/man-cgi?time
And if you want to redirect stdout to some file, you can use > filename to create file and fill it or >> filename to append to some file after the initial command.
If you want to redirect stderr by yourself, you can use $ command >&2 your_stderr_output
Try to use /usr/bin/time since many shells have their own implementation of time which may or may not support the same flags as /usr/bin/time
so change your command to
/usr/bin/time -a -o foo.txt wget ....
How about your LANG ?
$ time -ao o.txt echo 1
bash: -ao: コマンドが見つかりません
real 0m0.001s
user 0m0.000s
sys 0m0.000s
$ export|grep LANG
declare -x LANG="ja_JP.utf8"
$ LANG=C time -ao o.txt echo 1
1
$ cat o.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 1984maxresident)k
0inputs+0outputs (0major+158minor)pagefaults 0swaps
Try:
command 2> log.txt
and the real-time output from "command" can be seen in another console window with:
tail -f log.txt
This worked for me:
( time command ) |& tee output.txt
https://unix.stackexchange.com/questions/115980/how-can-i-redirect-time-output-and-command-output-to-the-same-pipe
You can do that with > if you want to redirect the output.
For example:
time wget 'http://localhost:8080/upLoading.jsp' --timeout=0 > output.txt 2>&1
2>&1 says to redirect STDERR to the same file.
This command will erase any output.txt files and creates a new one with your output. If you use >> it will append the output at the end of any existing output.txt file. If it doesn't exist, it will create it.

Resources