Redirecting output in Linux
-
So I saw an example of redirection in one of my LinuxAcademy.com videos, and I'm try to figure out exactly how this works.
I understand redirecting standard output.
ls /etc > files.txt
Will not show the results ofls /etc
in the console, but rather will create (or overwrite if it already exists) files.txt and write the output.I understand redirecting standard error.
asfasdfasdf 2> anError.txt
Sinceasfasdfasdf
isn't a real command, what is output to the console isn't standard output it's standard error. To redirect that to a file I use2>
.I also understand redirecting standard error to standard output.
asdfasdf 2>&1 | grep bash
Doing this allows me to pipe the standard error into something likegrep
, since otherwise I'd get an error as sinceasdfasdf
would produce standard error rather than standard output.Here is the example that confuses me. According to the video this example would redirect standard output as well as standard standard error to a file.
ls -fy > log.file 2>&1
Here is what confuses me.
- Since
-y
isn't a valid option forls
would the command ever produce standard output? From what I've tested the answer is "no." Even though-f
is a valid option the command fails when-y
is also used. - Reading
ls -fy > log.file 2>&1
from left to right makes me think that nothing will be put into log.file sincels -fy
will not produce standard output. Yet having2>&1
does apparently redirect the standard error to standard output and somehow knows to use the original>
to redirect the resultant standard output into log.file. Is that what's going on?
- Since
-
@eddiejennings said in Redirecting output in Linux:
So I saw an example of redirection in one of my LinuxAcademy.com videos, and I'm try to figure out exactly how this works.
I understand redirecting standard output.
ls /etc > files.txt
Will not show the results ofls /etc
in the console, but rather will create (or overwrite if it already exists) files.txt and write the output.I understand redirecting standard error.
asfasdfasdf 2> anError.txt
Sinceasfasdfasdf
isn't a real command, what is output to the console isn't standard output it's standard error. To redirect that to a file I use2>
.I also understand redirecting standard error to standard output.
asdfasdf 2>&1 | grep bash
Doing this allows me to pipe the standard error into something likegrep
, since otherwise I'd get an error as sinceasdfasdf
would produce standard error rather than standard output.Here is the example that confuses me. According to the video this example would redirect standard output as well as standard standard error to a file.
ls -fy > log.file 2>&1
Here is what confuses me.
- Since
-y
isn't a valid option forls
would the command ever produce standard output? From what I've tested the answer is "no." Even though-f
is a valid option the command fails when-y
is also used. - Reading
ls -fy > log.file 2>&1
from left to right makes me think that nothing will be put into log.file sincels -fy
will not produce standard output. Yet having2>&1
does apparently redirect the standard error to standard output and somehow knows to use the original>
to redirect the resultant standard output into log.file. Is that what's going on?
The command is not read left to right. That's the confusion.
- Since
-
For my learning, how should it be read?
-
-
Link was great, now I think I get what was going on with
ls -fy > file 2>&1
.Bash points the stdout file descriptor to
file
, and then duplicates stderror to stdout, which is already pointing tofile.