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 of ls /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 Since asfasdfasdf 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 use 2>.

    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 like grep, since otherwise I'd get an error as since asdfasdf 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.

    1. Since -y isn't a valid option for ls 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.
    2. Reading ls -fy > log.file 2>&1 from left to right makes me think that nothing will be put into log.file since ls -fy will not produce standard output. Yet having 2>&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?

  • Service Provider

    @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 of ls /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 Since asfasdfasdf 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 use 2>.

    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 like grep, since otherwise I'd get an error as since asdfasdf 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.

    1. Since -y isn't a valid option for ls 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.
    2. Reading ls -fy > log.file 2>&1 from left to right makes me think that nothing will be put into log.file since ls -fy will not produce standard output. Yet having 2>&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.



  • 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 to file.


Log in to reply
 

Looks like your connection to MangoLassi was lost, please wait while we try to reconnect.