Looping through all files, skipping some dynamically Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionLooping through files with spaces in the names?Bash script for looping through filesLooping through a file using a columnLooping through unix datesLooping through lines in several files (bash)Bash looping through two sets of fileslooping through JSON array in shell scriptNeed to iterate through subdirectories, concatenating files, with an iterative numberFlatpak - Safely Relocate Sandboxed Home DirectoryWhile loops not looping through all values

Hangman Game with C++

How does the secondary effect of the Heat Metal spell interact with a creature resistant/immune to fire damage?

How could we fake a moon landing now?

Quadrilaterals with equal sides

Should there be a hyphen in the construction "IT affin"?

How often does castling occur in grandmaster games?

Sentence order: Where to put もう

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Do wooden building fires get hotter than 600°C?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How do I decide if I need to go for Normalization and not Standardization or vice-versa?

Who can remove European Commissioners?

What initially awakened the Balrog?

How do living politicians protect their readily obtainable signatures from misuse?

Amount of permutations on an NxNxN Rubik's Cube

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

Why weren't discrete x86 CPUs ever used in game hardware?

How fail-safe is nr as stop bytes?

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

QGIS virtual layer functionality does not seem to support memory layers

How to improve on this Stylesheet Manipulation for Message Styling

Trademark violation for app?

An adverb for when you're not exaggerating

"Lost his faith in humanity in the trenches of Verdun" — last line of an SF story



Looping through all files, skipping some dynamically



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionLooping through files with spaces in the names?Bash script for looping through filesLooping through a file using a columnLooping through unix datesLooping through lines in several files (bash)Bash looping through two sets of fileslooping through JSON array in shell scriptNeed to iterate through subdirectories, concatenating files, with an iterative numberFlatpak - Safely Relocate Sandboxed Home DirectoryWhile loops not looping through all values



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.



My idea was to create a for loop, for i in $(find ./ -printf '%fn')
as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
As in



if [ "$i" == something I don't want to go down ] 
then
skip this directory
fi


Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.










share|improve this question









New contributor




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


























    0















    rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.



    My idea was to create a for loop, for i in $(find ./ -printf '%fn')
    as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
    As in



    if [ "$i" == something I don't want to go down ] 
    then
    skip this directory
    fi


    Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.










    share|improve this question









    New contributor




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






















      0












      0








      0








      rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.



      My idea was to create a for loop, for i in $(find ./ -printf '%fn')
      as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
      As in



      if [ "$i" == something I don't want to go down ] 
      then
      skip this directory
      fi


      Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.










      share|improve this question









      New contributor




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












      rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.



      My idea was to create a for loop, for i in $(find ./ -printf '%fn')
      as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
      As in



      if [ "$i" == something I don't want to go down ] 
      then
      skip this directory
      fi


      Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.







      linux bash shell scripting find






      share|improve this question









      New contributor




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











      share|improve this question









      New contributor




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









      share|improve this question




      share|improve this question








      edited Apr 15 at 2:41









      mosvy

      10.4k11238




      10.4k11238






      New contributor




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









      asked Apr 15 at 1:00









      m.smith7878m.smith7878

      1




      1




      New contributor




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





      New contributor





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






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




















          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, -exec is a predicate just like -name, and find will consider the exit status of the command and treat it as false if it's non-zero.



          But notice that a) this only applies to the -exec ... ; form, not to the -exec .. + form (which is always true) and b) forking a separate process for each file or directory is slow.



          For example this will skip all directories that contain a file named foo.txt:



          find . -type d -exec test -f /foo.txt ; -prune -or -print

          $ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
          $ find . -type d -exec test -f /foo.txt ; -prune -or -print
          ... lots of files ...
          $ touch a/1,2/foo.txt a/3/x,y/foo.txt
          $ find . -type d -exec test -f /foo.txt ; -prune -or -print
          .
          ./a
          ./a/3
          ./a/3/z
          ./a/3/z/a.txt


          Of course, instead of the test -f ... static command you can use eg. path/to/some/script .... With GNU find (the default in Linux) you can use -execdir instead of -exec.






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



            );






            m.smith7878 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%2f512466%2flooping-through-all-files-skipping-some-dynamically%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Yes, -exec is a predicate just like -name, and find will consider the exit status of the command and treat it as false if it's non-zero.



            But notice that a) this only applies to the -exec ... ; form, not to the -exec .. + form (which is always true) and b) forking a separate process for each file or directory is slow.



            For example this will skip all directories that contain a file named foo.txt:



            find . -type d -exec test -f /foo.txt ; -prune -or -print

            $ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
            $ find . -type d -exec test -f /foo.txt ; -prune -or -print
            ... lots of files ...
            $ touch a/1,2/foo.txt a/3/x,y/foo.txt
            $ find . -type d -exec test -f /foo.txt ; -prune -or -print
            .
            ./a
            ./a/3
            ./a/3/z
            ./a/3/z/a.txt


            Of course, instead of the test -f ... static command you can use eg. path/to/some/script .... With GNU find (the default in Linux) you can use -execdir instead of -exec.






            share|improve this answer





























              1














              Yes, -exec is a predicate just like -name, and find will consider the exit status of the command and treat it as false if it's non-zero.



              But notice that a) this only applies to the -exec ... ; form, not to the -exec .. + form (which is always true) and b) forking a separate process for each file or directory is slow.



              For example this will skip all directories that contain a file named foo.txt:



              find . -type d -exec test -f /foo.txt ; -prune -or -print

              $ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
              $ find . -type d -exec test -f /foo.txt ; -prune -or -print
              ... lots of files ...
              $ touch a/1,2/foo.txt a/3/x,y/foo.txt
              $ find . -type d -exec test -f /foo.txt ; -prune -or -print
              .
              ./a
              ./a/3
              ./a/3/z
              ./a/3/z/a.txt


              Of course, instead of the test -f ... static command you can use eg. path/to/some/script .... With GNU find (the default in Linux) you can use -execdir instead of -exec.






              share|improve this answer



























                1












                1








                1







                Yes, -exec is a predicate just like -name, and find will consider the exit status of the command and treat it as false if it's non-zero.



                But notice that a) this only applies to the -exec ... ; form, not to the -exec .. + form (which is always true) and b) forking a separate process for each file or directory is slow.



                For example this will skip all directories that contain a file named foo.txt:



                find . -type d -exec test -f /foo.txt ; -prune -or -print

                $ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
                $ find . -type d -exec test -f /foo.txt ; -prune -or -print
                ... lots of files ...
                $ touch a/1,2/foo.txt a/3/x,y/foo.txt
                $ find . -type d -exec test -f /foo.txt ; -prune -or -print
                .
                ./a
                ./a/3
                ./a/3/z
                ./a/3/z/a.txt


                Of course, instead of the test -f ... static command you can use eg. path/to/some/script .... With GNU find (the default in Linux) you can use -execdir instead of -exec.






                share|improve this answer















                Yes, -exec is a predicate just like -name, and find will consider the exit status of the command and treat it as false if it's non-zero.



                But notice that a) this only applies to the -exec ... ; form, not to the -exec .. + form (which is always true) and b) forking a separate process for each file or directory is slow.



                For example this will skip all directories that contain a file named foo.txt:



                find . -type d -exec test -f /foo.txt ; -prune -or -print

                $ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
                $ find . -type d -exec test -f /foo.txt ; -prune -or -print
                ... lots of files ...
                $ touch a/1,2/foo.txt a/3/x,y/foo.txt
                $ find . -type d -exec test -f /foo.txt ; -prune -or -print
                .
                ./a
                ./a/3
                ./a/3/z
                ./a/3/z/a.txt


                Of course, instead of the test -f ... static command you can use eg. path/to/some/script .... With GNU find (the default in Linux) you can use -execdir instead of -exec.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 15 at 2:45

























                answered Apr 15 at 2:12









                mosvymosvy

                10.4k11238




                10.4k11238




















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









                    draft saved

                    draft discarded


















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












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











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














                    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%2f512466%2flooping-through-all-files-skipping-some-dynamically%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

                    Helsingborg Esperantistoj el Helsingborg | Vidu ankaŭ | Navigada menuo1 ŝanĝostabila versiopatrolita1 ŝanĝostabila versiopatrolita56°03′N 12°42′O  /  56.05°N, 12.7°O / 56.05; 12.7 (Helsingborg)56°03′N 12°42′O  /  56.05°N, 12.7°O / 56.05; 12.7 (Helsingborg)Helsingborg en la Vikimedia KomunejoKategorio Helsingborg en la Vikimedia KomunejoHelsingborg en la Vikimedia KomunejoKategorio Helsingborg en la Vikimedia Komunejo

                    Linux Checkpoint SNX tool configuration issuesgetting Checkpoint VPN SSL Network Extender working in the command lineL2TP IPsec VPN client configurationOpenvpn stops respondingIssues with getting a tun0 connection to route any and all connections from eth0 to be made to this interface and if not working dropHow to setup port forwarding properly in FreeBsd 11?Getting certificate verify failed error in a Python applicationssh is unable to connect to server in VPNVPN SSL Network Extender in Firefoxgetting Checkpoint VPN SSL Network Extender working in the command lineisc-dhcp-server configurationUsing Checkpoint VPN SSL Network Extender CLI with certificate

                    NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided