Loop through files excluding directories

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












10















I need my script to do something to every file in the current directory excluding any sub-directories.



For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.



My current script:



#!/bin/bash
dir=`pwd`
for file in $dir/*
do
$* $file
done


I just need to exclude directories for the loop, but I don`t know how.










share|improve this question




























    10















    I need my script to do something to every file in the current directory excluding any sub-directories.



    For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
    My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.



    My current script:



    #!/bin/bash
    dir=`pwd`
    for file in $dir/*
    do
    $* $file
    done


    I just need to exclude directories for the loop, but I don`t know how.










    share|improve this question


























      10












      10








      10


      1






      I need my script to do something to every file in the current directory excluding any sub-directories.



      For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
      My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.



      My current script:



      #!/bin/bash
      dir=`pwd`
      for file in $dir/*
      do
      $* $file
      done


      I just need to exclude directories for the loop, but I don`t know how.










      share|improve this question
















      I need my script to do something to every file in the current directory excluding any sub-directories.



      For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
      My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.



      My current script:



      #!/bin/bash
      dir=`pwd`
      for file in $dir/*
      do
      $* $file
      done


      I just need to exclude directories for the loop, but I don`t know how.







      bash for






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 13 '17 at 20:25









      don_crissti

      51.3k15137165




      51.3k15137165










      asked Mar 13 '17 at 18:28









      Tõnis PiipTõnis Piip

      5615




      5615




















          4 Answers
          4






          active

          oldest

          votes


















          8














          #!/bin/bash -

          for file in "$dir"/*
          do
          if [ ! -d "$file" ]; then
          "$@" "$file"
          fi
          done


          Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).



          Alternative (from comments), check only for files:



          for file in "$dir"/*
          do
          if [ -f "$file" ]; then
          "$@" "$file"
          fi
          done





          share|improve this answer

























          • @roaima good idea!

            – laktak
            Mar 13 '17 at 19:16






          • 2





            Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

            – JRFerguson
            Mar 13 '17 at 19:29












          • @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

            – roaima
            Mar 13 '17 at 20:41











          • I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

            – Tõnis Piip
            Mar 14 '17 at 8:07


















          3














          Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).



          You can use find with -exec and use -maxdepth 1 to avoid recursing into the subdirectory.



          [ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;


          The [ -n "$1" ] is there to avoid executing all the files in the directory when the script isn't passed any arguments.






          share|improve this answer






























            2














            In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.) after the pattern restricts it to regular files.



            wc -w *(.)


            To cope with file names beginning with - or ., use wc -c -- *(.N) or wc -c ./*(.N). If you want to include symbolic links to regular files as well, make that *(-.).



            The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find.






            share|improve this answer






























              0














              Bash for loop excluding sub-directories ('/') and links (@):



              for f in `ls|egrep -v '/|@'`; do echo $f; done





              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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                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%2funix.stackexchange.com%2fquestions%2f351210%2floop-through-files-excluding-directories%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                8














                #!/bin/bash -

                for file in "$dir"/*
                do
                if [ ! -d "$file" ]; then
                "$@" "$file"
                fi
                done


                Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).



                Alternative (from comments), check only for files:



                for file in "$dir"/*
                do
                if [ -f "$file" ]; then
                "$@" "$file"
                fi
                done





                share|improve this answer

























                • @roaima good idea!

                  – laktak
                  Mar 13 '17 at 19:16






                • 2





                  Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                  – JRFerguson
                  Mar 13 '17 at 19:29












                • @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                  – roaima
                  Mar 13 '17 at 20:41











                • I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                  – Tõnis Piip
                  Mar 14 '17 at 8:07















                8














                #!/bin/bash -

                for file in "$dir"/*
                do
                if [ ! -d "$file" ]; then
                "$@" "$file"
                fi
                done


                Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).



                Alternative (from comments), check only for files:



                for file in "$dir"/*
                do
                if [ -f "$file" ]; then
                "$@" "$file"
                fi
                done





                share|improve this answer

























                • @roaima good idea!

                  – laktak
                  Mar 13 '17 at 19:16






                • 2





                  Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                  – JRFerguson
                  Mar 13 '17 at 19:29












                • @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                  – roaima
                  Mar 13 '17 at 20:41











                • I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                  – Tõnis Piip
                  Mar 14 '17 at 8:07













                8












                8








                8







                #!/bin/bash -

                for file in "$dir"/*
                do
                if [ ! -d "$file" ]; then
                "$@" "$file"
                fi
                done


                Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).



                Alternative (from comments), check only for files:



                for file in "$dir"/*
                do
                if [ -f "$file" ]; then
                "$@" "$file"
                fi
                done





                share|improve this answer















                #!/bin/bash -

                for file in "$dir"/*
                do
                if [ ! -d "$file" ]; then
                "$@" "$file"
                fi
                done


                Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).



                Alternative (from comments), check only for files:



                for file in "$dir"/*
                do
                if [ -f "$file" ]; then
                "$@" "$file"
                fi
                done






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 13 '17 at 21:10

























                answered Mar 13 '17 at 18:31









                laktaklaktak

                811510




                811510












                • @roaima good idea!

                  – laktak
                  Mar 13 '17 at 19:16






                • 2





                  Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                  – JRFerguson
                  Mar 13 '17 at 19:29












                • @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                  – roaima
                  Mar 13 '17 at 20:41











                • I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                  – Tõnis Piip
                  Mar 14 '17 at 8:07

















                • @roaima good idea!

                  – laktak
                  Mar 13 '17 at 19:16






                • 2





                  Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                  – JRFerguson
                  Mar 13 '17 at 19:29












                • @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                  – roaima
                  Mar 13 '17 at 20:41











                • I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                  – Tõnis Piip
                  Mar 14 '17 at 8:07
















                @roaima good idea!

                – laktak
                Mar 13 '17 at 19:16





                @roaima good idea!

                – laktak
                Mar 13 '17 at 19:16




                2




                2





                Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                – JRFerguson
                Mar 13 '17 at 19:29






                Better to use if [ -f "$file" ]; then .... Not a directory would return not only regular files but sockets and symlinks too.

                – JRFerguson
                Mar 13 '17 at 19:29














                @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                – roaima
                Mar 13 '17 at 20:41





                @JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with ! -d already.

                – roaima
                Mar 13 '17 at 20:41













                I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                – Tõnis Piip
                Mar 14 '17 at 8:07





                I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.

                – Tõnis Piip
                Mar 14 '17 at 8:07













                3














                Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).



                You can use find with -exec and use -maxdepth 1 to avoid recursing into the subdirectory.



                [ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;


                The [ -n "$1" ] is there to avoid executing all the files in the directory when the script isn't passed any arguments.






                share|improve this answer



























                  3














                  Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).



                  You can use find with -exec and use -maxdepth 1 to avoid recursing into the subdirectory.



                  [ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;


                  The [ -n "$1" ] is there to avoid executing all the files in the directory when the script isn't passed any arguments.






                  share|improve this answer

























                    3












                    3








                    3







                    Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).



                    You can use find with -exec and use -maxdepth 1 to avoid recursing into the subdirectory.



                    [ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;


                    The [ -n "$1" ] is there to avoid executing all the files in the directory when the script isn't passed any arguments.






                    share|improve this answer













                    Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).



                    You can use find with -exec and use -maxdepth 1 to avoid recursing into the subdirectory.



                    [ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;


                    The [ -n "$1" ] is there to avoid executing all the files in the directory when the script isn't passed any arguments.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 13 '17 at 20:40









                    Gregory NisbetGregory Nisbet

                    1,4441020




                    1,4441020





















                        2














                        In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.) after the pattern restricts it to regular files.



                        wc -w *(.)


                        To cope with file names beginning with - or ., use wc -c -- *(.N) or wc -c ./*(.N). If you want to include symbolic links to regular files as well, make that *(-.).



                        The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find.






                        share|improve this answer



























                          2














                          In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.) after the pattern restricts it to regular files.



                          wc -w *(.)


                          To cope with file names beginning with - or ., use wc -c -- *(.N) or wc -c ./*(.N). If you want to include symbolic links to regular files as well, make that *(-.).



                          The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find.






                          share|improve this answer

























                            2












                            2








                            2







                            In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.) after the pattern restricts it to regular files.



                            wc -w *(.)


                            To cope with file names beginning with - or ., use wc -c -- *(.N) or wc -c ./*(.N). If you want to include symbolic links to regular files as well, make that *(-.).



                            The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find.






                            share|improve this answer













                            In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.) after the pattern restricts it to regular files.



                            wc -w *(.)


                            To cope with file names beginning with - or ., use wc -c -- *(.N) or wc -c ./*(.N). If you want to include symbolic links to regular files as well, make that *(-.).



                            The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 14 '17 at 1:20









                            GillesGilles

                            540k12810941607




                            540k12810941607





















                                0














                                Bash for loop excluding sub-directories ('/') and links (@):



                                for f in `ls|egrep -v '/|@'`; do echo $f; done





                                share|improve this answer



























                                  0














                                  Bash for loop excluding sub-directories ('/') and links (@):



                                  for f in `ls|egrep -v '/|@'`; do echo $f; done





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Bash for loop excluding sub-directories ('/') and links (@):



                                    for f in `ls|egrep -v '/|@'`; do echo $f; done





                                    share|improve this answer













                                    Bash for loop excluding sub-directories ('/') and links (@):



                                    for f in `ls|egrep -v '/|@'`; do echo $f; done






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 8 at 18:25









                                    James ArmstrongJames Armstrong

                                    1




                                    1



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • 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%2funix.stackexchange.com%2fquestions%2f351210%2floop-through-files-excluding-directories%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

                                        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?