ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Redirecting output in Linux

    IT Discussion
    rhel 7 rhcsa learning
    3
    5
    1.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EddieJenningsE
      EddieJennings
      last edited by

      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?
      scottalanmillerS 1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller @EddieJennings
        last edited by

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

        1 Reply Last reply Reply Quote 1
        • EddieJenningsE
          EddieJennings
          last edited by

          For my learning, how should it be read?

          momurdaM 1 Reply Last reply Reply Quote 0
          • momurdaM
            momurda @EddieJennings
            last edited by

            @eddiejennings
            http://wiki.bash-hackers.org/howto/redirection_tutorial#order_of_redirection_ie_file_2_1_vs_2_1_file
            This has good explanation

            1 Reply Last reply Reply Quote 3
            • EddieJenningsE
              EddieJennings
              last edited by

              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.

              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post