Filter directories from list of files and directories

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











up vote
1
down vote

favorite












I have a file containing full path names of files an directories.



From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.



Can anybody think of an elegant solution?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I have a file containing full path names of files an directories.



    From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.



    Can anybody think of an elegant solution?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a file containing full path names of files an directories.



      From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.



      Can anybody think of an elegant solution?







      share|improve this question












      I have a file containing full path names of files an directories.



      From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.



      Can anybody think of an elegant solution?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 14 at 22:06









      ARF

      1336




      1336




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          while IFS= read -r file; do
          [ -d "$file" ] || printf '%sn' "$file"
          done <input_file


          Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).



          Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):




          • the file exists (after symlink resolution) and is not of type directory:



            [ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"



          • file exists and is a regular file (after symlink resolution):



            [ -f "$file" ] && printf '%sn' "$file"



          • file exists and is a regular file before symlink resolution (excludes symlinks):



            [ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"


          • etc.






          share|improve this answer





























            up vote
            0
            down vote













            Assuming the file contains one file path per line, with zsh:



            printf '%sn' $(f)^"$(<file)"(N^/)


            would print those that do exist (that we can tell exist) and are not determined to be of type directory (change / to -/ to also discard symlinks to directories).



            Since you mention xargs, with the GNU implementation (for -d and -r), and assuming none of the file paths start with - or are find predicates (like !) you could do:



            xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file


            Replace -type with -xtype (assuming GNU find) to also exclude symlinks to directories.






            share|improve this answer






















              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
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417123%2ffilter-directories-from-list-of-files-and-directories%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
              3
              down vote



              accepted










              while IFS= read -r file; do
              [ -d "$file" ] || printf '%sn' "$file"
              done <input_file


              Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).



              Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):




              • the file exists (after symlink resolution) and is not of type directory:



                [ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"



              • file exists and is a regular file (after symlink resolution):



                [ -f "$file" ] && printf '%sn' "$file"



              • file exists and is a regular file before symlink resolution (excludes symlinks):



                [ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"


              • etc.






              share|improve this answer


























                up vote
                3
                down vote



                accepted










                while IFS= read -r file; do
                [ -d "$file" ] || printf '%sn' "$file"
                done <input_file


                Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).



                Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):




                • the file exists (after symlink resolution) and is not of type directory:



                  [ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"



                • file exists and is a regular file (after symlink resolution):



                  [ -f "$file" ] && printf '%sn' "$file"



                • file exists and is a regular file before symlink resolution (excludes symlinks):



                  [ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"


                • etc.






                share|improve this answer
























                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  while IFS= read -r file; do
                  [ -d "$file" ] || printf '%sn' "$file"
                  done <input_file


                  Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).



                  Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):




                  • the file exists (after symlink resolution) and is not of type directory:



                    [ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"



                  • file exists and is a regular file (after symlink resolution):



                    [ -f "$file" ] && printf '%sn' "$file"



                  • file exists and is a regular file before symlink resolution (excludes symlinks):



                    [ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"


                  • etc.






                  share|improve this answer














                  while IFS= read -r file; do
                  [ -d "$file" ] || printf '%sn' "$file"
                  done <input_file


                  Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).



                  Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):




                  • the file exists (after symlink resolution) and is not of type directory:



                    [ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"



                  • file exists and is a regular file (after symlink resolution):



                    [ -f "$file" ] && printf '%sn' "$file"



                  • file exists and is a regular file before symlink resolution (excludes symlinks):



                    [ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"


                  • etc.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 15 at 8:12









                  Stéphane Chazelas

                  281k53518849




                  281k53518849










                  answered Jan 14 at 22:15









                  Hauke Laging

                  53.4k1282130




                  53.4k1282130






















                      up vote
                      0
                      down vote













                      Assuming the file contains one file path per line, with zsh:



                      printf '%sn' $(f)^"$(<file)"(N^/)


                      would print those that do exist (that we can tell exist) and are not determined to be of type directory (change / to -/ to also discard symlinks to directories).



                      Since you mention xargs, with the GNU implementation (for -d and -r), and assuming none of the file paths start with - or are find predicates (like !) you could do:



                      xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file


                      Replace -type with -xtype (assuming GNU find) to also exclude symlinks to directories.






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        Assuming the file contains one file path per line, with zsh:



                        printf '%sn' $(f)^"$(<file)"(N^/)


                        would print those that do exist (that we can tell exist) and are not determined to be of type directory (change / to -/ to also discard symlinks to directories).



                        Since you mention xargs, with the GNU implementation (for -d and -r), and assuming none of the file paths start with - or are find predicates (like !) you could do:



                        xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file


                        Replace -type with -xtype (assuming GNU find) to also exclude symlinks to directories.






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Assuming the file contains one file path per line, with zsh:



                          printf '%sn' $(f)^"$(<file)"(N^/)


                          would print those that do exist (that we can tell exist) and are not determined to be of type directory (change / to -/ to also discard symlinks to directories).



                          Since you mention xargs, with the GNU implementation (for -d and -r), and assuming none of the file paths start with - or are find predicates (like !) you could do:



                          xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file


                          Replace -type with -xtype (assuming GNU find) to also exclude symlinks to directories.






                          share|improve this answer














                          Assuming the file contains one file path per line, with zsh:



                          printf '%sn' $(f)^"$(<file)"(N^/)


                          would print those that do exist (that we can tell exist) and are not determined to be of type directory (change / to -/ to also discard symlinks to directories).



                          Since you mention xargs, with the GNU implementation (for -d and -r), and assuming none of the file paths start with - or are find predicates (like !) you could do:



                          xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file


                          Replace -type with -xtype (assuming GNU find) to also exclude symlinks to directories.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 15 at 7:32

























                          answered Jan 14 at 23:20









                          Stéphane Chazelas

                          281k53518849




                          281k53518849






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417123%2ffilter-directories-from-list-of-files-and-directories%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?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay