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.txtWill not show the results ofls /etcin 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.txtSinceasfasdfasdfisn'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 bashDoing this allows me to pipe the standard error into something likegrep, since otherwise I'd get an error as sinceasdfasdfwould 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>&1Here is what confuses me.
- Since
-yisn't a valid option forlswould the command ever produce standard output? From what I've tested the answer is "no." Even though-fis a valid option the command fails when-yis also used. - Reading
ls -fy > log.file 2>&1from left to right makes me think that nothing will be put into log.file sincels -fywill not produce standard output. Yet having2>&1does 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.txtWill not show the results ofls /etcin 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.txtSinceasfasdfasdfisn'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 bashDoing this allows me to pipe the standard error into something likegrep, since otherwise I'd get an error as sinceasdfasdfwould 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>&1Here is what confuses me.
- Since
-yisn't a valid option forlswould the command ever produce standard output? From what I've tested the answer is "no." Even though-fis a valid option the command fails when-yis also used. - Reading
ls -fy > log.file 2>&1from left to right makes me think that nothing will be put into log.file sincels -fywill not produce standard output. Yet having2>&1does 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.