How to pass a list of file names to xargs after checking if the file exists?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a command (command1) that returns a list of file names like below



/consumer/a.txt
/consumer/b.txt
/consumer/doesnotexist.txt


When I pipe the output like command1 | xargs command2 command2 throws exception if one of the files does not exist.



How do I remove non-existant files before piping it to command2. I am expecting something in the lines of



command1 | xargs remove_nonexistant_files | xargs command2



command2 should receive



/consumer/a.txt
/consumer/b.txt


as input.









share









New contributor




Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    0
    down vote

    favorite












    I have a command (command1) that returns a list of file names like below



    /consumer/a.txt
    /consumer/b.txt
    /consumer/doesnotexist.txt


    When I pipe the output like command1 | xargs command2 command2 throws exception if one of the files does not exist.



    How do I remove non-existant files before piping it to command2. I am expecting something in the lines of



    command1 | xargs remove_nonexistant_files | xargs command2



    command2 should receive



    /consumer/a.txt
    /consumer/b.txt


    as input.









    share









    New contributor




    Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a command (command1) that returns a list of file names like below



      /consumer/a.txt
      /consumer/b.txt
      /consumer/doesnotexist.txt


      When I pipe the output like command1 | xargs command2 command2 throws exception if one of the files does not exist.



      How do I remove non-existant files before piping it to command2. I am expecting something in the lines of



      command1 | xargs remove_nonexistant_files | xargs command2



      command2 should receive



      /consumer/a.txt
      /consumer/b.txt


      as input.









      share









      New contributor




      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a command (command1) that returns a list of file names like below



      /consumer/a.txt
      /consumer/b.txt
      /consumer/doesnotexist.txt


      When I pipe the output like command1 | xargs command2 command2 throws exception if one of the files does not exist.



      How do I remove non-existant files before piping it to command2. I am expecting something in the lines of



      command1 | xargs remove_nonexistant_files | xargs command2



      command2 should receive



      /consumer/a.txt
      /consumer/b.txt


      as input.







      bash files osx





      share









      New contributor




      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share









      New contributor




      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share








      edited 6 mins ago





















      New contributor




      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 mins ago









      Sathish

      11




      11




      New contributor




      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Sathish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          $ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2




          share



























            up vote
            0
            down vote













            command1 |
            xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
            xargs command 2


            The extra bit in the middle is a another xargs invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.





            share




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Sathish is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478526%2fhow-to-pass-a-list-of-file-names-to-xargs-after-checking-if-the-file-exists%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              $ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2




              share
























                up vote
                0
                down vote













                $ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2




                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  $ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2




                  share












                  $ for file in $(command1); do if [[ -f "$file" ]]; then echo "$file"; done | xargs command2





                  share











                  share


                  share










                  answered 6 mins ago









                  DopeGhoti

                  41.9k55180




                  41.9k55180






















                      up vote
                      0
                      down vote













                      command1 |
                      xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
                      xargs command 2


                      The extra bit in the middle is a another xargs invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.





                      share
























                        up vote
                        0
                        down vote













                        command1 |
                        xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
                        xargs command 2


                        The extra bit in the middle is a another xargs invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.





                        share






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          command1 |
                          xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
                          xargs command 2


                          The extra bit in the middle is a another xargs invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.





                          share












                          command1 |
                          xargs sh -c 'for p do [ -f "$p" ] && printf "%sn" "$p"; done' sh |
                          xargs command 2


                          The extra bit in the middle is a another xargs invocation of a short script that basically just loops over its given command line arguments and prints the pathnames that correspond to existing regular files (or symbolic links to regular files). These existing pathnames are then passed on to the last part of the pipeline.






                          share











                          share


                          share










                          answered 34 secs ago









                          Kusalananda

                          112k15216344




                          112k15216344




















                              Sathish is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              Sathish is a new contributor. Be nice, and check out our Code of Conduct.












                              Sathish is a new contributor. Be nice, and check out our Code of Conduct.











                              Sathish is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478526%2fhow-to-pass-a-list-of-file-names-to-xargs-after-checking-if-the-file-exists%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?