Ls and input redirection











up vote
-2
down vote

favorite












I'd like to redirect contents of a file to ls command input:



ls -l < /home/john/1.txt


where 1.txt content is just:



/etc


I expected ls to list the content of file defined in /home/jonh1.txt - that is /etc dir
but it seems to ignore the input stream - still lists the contents of the current dir insted of /etc dir as defined in /home/jonh1.txt file.





WHY WAS MY QUESTION DOWNVOTED AND BY WHO!?










share|improve this question




















  • 4




    Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
    – Kamil Maciorowski
    Dec 4 at 10:46








  • 1




    What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
    – Xen2050
    Dec 5 at 8:30








  • 1




    Why your question may have been downwoted.
    – Kamil Maciorowski
    Dec 5 at 10:05

















up vote
-2
down vote

favorite












I'd like to redirect contents of a file to ls command input:



ls -l < /home/john/1.txt


where 1.txt content is just:



/etc


I expected ls to list the content of file defined in /home/jonh1.txt - that is /etc dir
but it seems to ignore the input stream - still lists the contents of the current dir insted of /etc dir as defined in /home/jonh1.txt file.





WHY WAS MY QUESTION DOWNVOTED AND BY WHO!?










share|improve this question




















  • 4




    Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
    – Kamil Maciorowski
    Dec 4 at 10:46








  • 1




    What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
    – Xen2050
    Dec 5 at 8:30








  • 1




    Why your question may have been downwoted.
    – Kamil Maciorowski
    Dec 5 at 10:05















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I'd like to redirect contents of a file to ls command input:



ls -l < /home/john/1.txt


where 1.txt content is just:



/etc


I expected ls to list the content of file defined in /home/jonh1.txt - that is /etc dir
but it seems to ignore the input stream - still lists the contents of the current dir insted of /etc dir as defined in /home/jonh1.txt file.





WHY WAS MY QUESTION DOWNVOTED AND BY WHO!?










share|improve this question















I'd like to redirect contents of a file to ls command input:



ls -l < /home/john/1.txt


where 1.txt content is just:



/etc


I expected ls to list the content of file defined in /home/jonh1.txt - that is /etc dir
but it seems to ignore the input stream - still lists the contents of the current dir insted of /etc dir as defined in /home/jonh1.txt file.





WHY WAS MY QUESTION DOWNVOTED AND BY WHO!?







bash redirection input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 9:59

























asked Dec 4 at 10:40









Mulligun007

122




122








  • 4




    Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
    – Kamil Maciorowski
    Dec 4 at 10:46








  • 1




    What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
    – Xen2050
    Dec 5 at 8:30








  • 1




    Why your question may have been downwoted.
    – Kamil Maciorowski
    Dec 5 at 10:05
















  • 4




    Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
    – Kamil Maciorowski
    Dec 4 at 10:46








  • 1




    What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
    – Xen2050
    Dec 5 at 8:30








  • 1




    Why your question may have been downwoted.
    – Kamil Maciorowski
    Dec 5 at 10:05










4




4




Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
– Kamil Maciorowski
Dec 4 at 10:46






Why do some commands not read from their standard input?, Unix pipe into ls, Piping the contents of a file to ls.
– Kamil Maciorowski
Dec 4 at 10:46






1




1




What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
– Xen2050
Dec 5 at 8:30






What's the goal, to get a ls-like listing of specific files (from a list in a separate textfile)? stat might be useful
– Xen2050
Dec 5 at 8:30






1




1




Why your question may have been downwoted.
– Kamil Maciorowski
Dec 5 at 10:05






Why your question may have been downwoted.
– Kamil Maciorowski
Dec 5 at 10:05












3 Answers
3






active

oldest

votes

















up vote
3
down vote














it seems to ignore the input stream




Yes. The ls program never uses the input stream, because it wasn't written to read anything from the input stream. This is not something that comes automatically with any Unix program – it has to be implemented separately.



Do not confuse the input stream (stdin) with the command-line arguments (argv); the former is an actual stream which can be read from line-by-line, while the latter is a simple read-only array of words. In your case, both are present and contain different data.



(Why shouldn't it be automatic for all programs? Well, take sort or cat or grep as an example. These programs accept different kinds of values – file names as arguments, but text as stdin contents. So you can pipe any command to | sort, and it sorts the input. But if stdin and command-line were the same thing, you couldn't pipe anything into | sort anymore because it would think that the pipe input was a list of filenames. Or vice versa, you couldn't give it filenames because it'd just sort the names.)



For an alternative take, see this explanation on StackOverflow (which you already got in the comments).






share|improve this answer






























    up vote
    1
    down vote













    If you write any command line program that takes input from the user, you'll see very clearly what's going on. If a program has a read/readLine(different languages might give it a different name), statement, so, the program prompts the user for input, then you can also pipe or redirect that input in using | or <



    If you look at a program that takes input, you see like with more or less, if you type it with no input $more<ENTER>, then it prompts for input. By piping data to it, it stops it prompting for input. If we run sed with no parameters $sed<ENTER> then sure it gives an error, and we can't do $echo abc|sed<ENTER> either. But if we do $sed 's/a/b/'<ENTER> then it prompts for input. And that's why echo abc|sed 's/a/b/'<ENTER> works.



    You can see this if you write a program even as simple as Hello World but that takes input like the program prompts for a word and if the person types 'abc' it says "Hello abc". You'll be able to pipe to that program. It doesn't take any extra programming to get piping and redirection of input to work. If the program accepts keyboard input from the user then piping and redirection to it will work and then the program won't prompt for input.






    share|improve this answer




























      up vote
      0
      down vote













      Try using xargs:



      xargs ls -l < /home/john/1.txt





      share|improve this answer





















      • I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
        – Mulligun007
        Dec 4 at 11:12








      • 3




        Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
        – Twisty Impersonator
        Dec 4 at 12:49






      • 1




        @Mulligun007 No, you didn't.
        – Luke Girvin
        Dec 4 at 14:05











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1380654%2fls-and-input-redirection%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote














      it seems to ignore the input stream




      Yes. The ls program never uses the input stream, because it wasn't written to read anything from the input stream. This is not something that comes automatically with any Unix program – it has to be implemented separately.



      Do not confuse the input stream (stdin) with the command-line arguments (argv); the former is an actual stream which can be read from line-by-line, while the latter is a simple read-only array of words. In your case, both are present and contain different data.



      (Why shouldn't it be automatic for all programs? Well, take sort or cat or grep as an example. These programs accept different kinds of values – file names as arguments, but text as stdin contents. So you can pipe any command to | sort, and it sorts the input. But if stdin and command-line were the same thing, you couldn't pipe anything into | sort anymore because it would think that the pipe input was a list of filenames. Or vice versa, you couldn't give it filenames because it'd just sort the names.)



      For an alternative take, see this explanation on StackOverflow (which you already got in the comments).






      share|improve this answer



























        up vote
        3
        down vote














        it seems to ignore the input stream




        Yes. The ls program never uses the input stream, because it wasn't written to read anything from the input stream. This is not something that comes automatically with any Unix program – it has to be implemented separately.



        Do not confuse the input stream (stdin) with the command-line arguments (argv); the former is an actual stream which can be read from line-by-line, while the latter is a simple read-only array of words. In your case, both are present and contain different data.



        (Why shouldn't it be automatic for all programs? Well, take sort or cat or grep as an example. These programs accept different kinds of values – file names as arguments, but text as stdin contents. So you can pipe any command to | sort, and it sorts the input. But if stdin and command-line were the same thing, you couldn't pipe anything into | sort anymore because it would think that the pipe input was a list of filenames. Or vice versa, you couldn't give it filenames because it'd just sort the names.)



        For an alternative take, see this explanation on StackOverflow (which you already got in the comments).






        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote










          it seems to ignore the input stream




          Yes. The ls program never uses the input stream, because it wasn't written to read anything from the input stream. This is not something that comes automatically with any Unix program – it has to be implemented separately.



          Do not confuse the input stream (stdin) with the command-line arguments (argv); the former is an actual stream which can be read from line-by-line, while the latter is a simple read-only array of words. In your case, both are present and contain different data.



          (Why shouldn't it be automatic for all programs? Well, take sort or cat or grep as an example. These programs accept different kinds of values – file names as arguments, but text as stdin contents. So you can pipe any command to | sort, and it sorts the input. But if stdin and command-line were the same thing, you couldn't pipe anything into | sort anymore because it would think that the pipe input was a list of filenames. Or vice versa, you couldn't give it filenames because it'd just sort the names.)



          For an alternative take, see this explanation on StackOverflow (which you already got in the comments).






          share|improve this answer















          it seems to ignore the input stream




          Yes. The ls program never uses the input stream, because it wasn't written to read anything from the input stream. This is not something that comes automatically with any Unix program – it has to be implemented separately.



          Do not confuse the input stream (stdin) with the command-line arguments (argv); the former is an actual stream which can be read from line-by-line, while the latter is a simple read-only array of words. In your case, both are present and contain different data.



          (Why shouldn't it be automatic for all programs? Well, take sort or cat or grep as an example. These programs accept different kinds of values – file names as arguments, but text as stdin contents. So you can pipe any command to | sort, and it sorts the input. But if stdin and command-line were the same thing, you couldn't pipe anything into | sort anymore because it would think that the pipe input was a list of filenames. Or vice versa, you couldn't give it filenames because it'd just sort the names.)



          For an alternative take, see this explanation on StackOverflow (which you already got in the comments).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 4 at 13:13

























          answered Dec 4 at 12:59









          grawity

          230k35486544




          230k35486544
























              up vote
              1
              down vote













              If you write any command line program that takes input from the user, you'll see very clearly what's going on. If a program has a read/readLine(different languages might give it a different name), statement, so, the program prompts the user for input, then you can also pipe or redirect that input in using | or <



              If you look at a program that takes input, you see like with more or less, if you type it with no input $more<ENTER>, then it prompts for input. By piping data to it, it stops it prompting for input. If we run sed with no parameters $sed<ENTER> then sure it gives an error, and we can't do $echo abc|sed<ENTER> either. But if we do $sed 's/a/b/'<ENTER> then it prompts for input. And that's why echo abc|sed 's/a/b/'<ENTER> works.



              You can see this if you write a program even as simple as Hello World but that takes input like the program prompts for a word and if the person types 'abc' it says "Hello abc". You'll be able to pipe to that program. It doesn't take any extra programming to get piping and redirection of input to work. If the program accepts keyboard input from the user then piping and redirection to it will work and then the program won't prompt for input.






              share|improve this answer

























                up vote
                1
                down vote













                If you write any command line program that takes input from the user, you'll see very clearly what's going on. If a program has a read/readLine(different languages might give it a different name), statement, so, the program prompts the user for input, then you can also pipe or redirect that input in using | or <



                If you look at a program that takes input, you see like with more or less, if you type it with no input $more<ENTER>, then it prompts for input. By piping data to it, it stops it prompting for input. If we run sed with no parameters $sed<ENTER> then sure it gives an error, and we can't do $echo abc|sed<ENTER> either. But if we do $sed 's/a/b/'<ENTER> then it prompts for input. And that's why echo abc|sed 's/a/b/'<ENTER> works.



                You can see this if you write a program even as simple as Hello World but that takes input like the program prompts for a word and if the person types 'abc' it says "Hello abc". You'll be able to pipe to that program. It doesn't take any extra programming to get piping and redirection of input to work. If the program accepts keyboard input from the user then piping and redirection to it will work and then the program won't prompt for input.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you write any command line program that takes input from the user, you'll see very clearly what's going on. If a program has a read/readLine(different languages might give it a different name), statement, so, the program prompts the user for input, then you can also pipe or redirect that input in using | or <



                  If you look at a program that takes input, you see like with more or less, if you type it with no input $more<ENTER>, then it prompts for input. By piping data to it, it stops it prompting for input. If we run sed with no parameters $sed<ENTER> then sure it gives an error, and we can't do $echo abc|sed<ENTER> either. But if we do $sed 's/a/b/'<ENTER> then it prompts for input. And that's why echo abc|sed 's/a/b/'<ENTER> works.



                  You can see this if you write a program even as simple as Hello World but that takes input like the program prompts for a word and if the person types 'abc' it says "Hello abc". You'll be able to pipe to that program. It doesn't take any extra programming to get piping and redirection of input to work. If the program accepts keyboard input from the user then piping and redirection to it will work and then the program won't prompt for input.






                  share|improve this answer












                  If you write any command line program that takes input from the user, you'll see very clearly what's going on. If a program has a read/readLine(different languages might give it a different name), statement, so, the program prompts the user for input, then you can also pipe or redirect that input in using | or <



                  If you look at a program that takes input, you see like with more or less, if you type it with no input $more<ENTER>, then it prompts for input. By piping data to it, it stops it prompting for input. If we run sed with no parameters $sed<ENTER> then sure it gives an error, and we can't do $echo abc|sed<ENTER> either. But if we do $sed 's/a/b/'<ENTER> then it prompts for input. And that's why echo abc|sed 's/a/b/'<ENTER> works.



                  You can see this if you write a program even as simple as Hello World but that takes input like the program prompts for a word and if the person types 'abc' it says "Hello abc". You'll be able to pipe to that program. It doesn't take any extra programming to get piping and redirection of input to work. If the program accepts keyboard input from the user then piping and redirection to it will work and then the program won't prompt for input.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 4 at 13:41









                  barlop

                  15.3k2287145




                  15.3k2287145






















                      up vote
                      0
                      down vote













                      Try using xargs:



                      xargs ls -l < /home/john/1.txt





                      share|improve this answer





















                      • I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                        – Mulligun007
                        Dec 4 at 11:12








                      • 3




                        Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                        – Twisty Impersonator
                        Dec 4 at 12:49






                      • 1




                        @Mulligun007 No, you didn't.
                        – Luke Girvin
                        Dec 4 at 14:05















                      up vote
                      0
                      down vote













                      Try using xargs:



                      xargs ls -l < /home/john/1.txt





                      share|improve this answer





















                      • I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                        – Mulligun007
                        Dec 4 at 11:12








                      • 3




                        Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                        – Twisty Impersonator
                        Dec 4 at 12:49






                      • 1




                        @Mulligun007 No, you didn't.
                        – Luke Girvin
                        Dec 4 at 14:05













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Try using xargs:



                      xargs ls -l < /home/john/1.txt





                      share|improve this answer












                      Try using xargs:



                      xargs ls -l < /home/john/1.txt






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 4 at 10:47









                      Luke Girvin

                      155314




                      155314












                      • I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                        – Mulligun007
                        Dec 4 at 11:12








                      • 3




                        Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                        – Twisty Impersonator
                        Dec 4 at 12:49






                      • 1




                        @Mulligun007 No, you didn't.
                        – Luke Girvin
                        Dec 4 at 14:05


















                      • I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                        – Mulligun007
                        Dec 4 at 11:12








                      • 3




                        Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                        – Twisty Impersonator
                        Dec 4 at 12:49






                      • 1




                        @Mulligun007 No, you didn't.
                        – Luke Girvin
                        Dec 4 at 14:05
















                      I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                      – Mulligun007
                      Dec 4 at 11:12






                      I didn't asked about an alternate way but WHY doesn't ls ignores input stream.
                      – Mulligun007
                      Dec 4 at 11:12






                      3




                      3




                      Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                      – Twisty Impersonator
                      Dec 4 at 12:49




                      Please edit your question to more explicitly ask that. Right now your question doesn't even include the word why, making it hard to know this is what you want from an answer.
                      – Twisty Impersonator
                      Dec 4 at 12:49




                      1




                      1




                      @Mulligun007 No, you didn't.
                      – Luke Girvin
                      Dec 4 at 14:05




                      @Mulligun007 No, you didn't.
                      – Luke Girvin
                      Dec 4 at 14:05


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1380654%2fls-and-input-redirection%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                      Eduardo VII do Reino Unido