Counting files in a directory

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








up vote
4
down vote

favorite
2












I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.



# Report on Current Status
echo -n "Cropped Files: "
ls "$Destination" | wc -l


My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.



Duplicate file: foo.pdf



Changed name: foo.x.pdf



If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls output so wc -l can count the unique files only?



TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?










share|improve this question



























    up vote
    4
    down vote

    favorite
    2












    I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.



    # Report on Current Status
    echo -n "Cropped Files: "
    ls "$Destination" | wc -l


    My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.



    Duplicate file: foo.pdf



    Changed name: foo.x.pdf



    If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls output so wc -l can count the unique files only?



    TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?










    share|improve this question

























      up vote
      4
      down vote

      favorite
      2









      up vote
      4
      down vote

      favorite
      2






      2





      I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.



      # Report on Current Status
      echo -n "Cropped Files: "
      ls "$Destination" | wc -l


      My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.



      Duplicate file: foo.pdf



      Changed name: foo.x.pdf



      If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls output so wc -l can count the unique files only?



      TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?










      share|improve this question















      I use the following code at the end of one of my scripts to tally up the number of files I have processed and moved into that directory.



      # Report on Current Status
      echo -n "Cropped Files: "
      ls "$Destination" | wc -l


      My problem lies with how I handle duplicate files. As of right now, I check for the file's presence first (as my script is destructive in nature to the source files I am processing). If it senses a file of that name already processed, I alter the filename as follows.



      Duplicate file: foo.pdf



      Changed name: foo.x.pdf



      If there is a foo.x.pdf, then I rename again to foo.xx.pdf. Repeat as necessary. I intend to go in later and evaluate each 'version' and select the best one to keep on hand. But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on. How do I strip these out of the ls output so wc -l can count the unique files only?



      TL;DR: How do I get the count of files in a given directory that do not contain a given substring in their filename?







      command-line bash scripts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 9 at 21:19









      wjandrea

      7,27742256




      7,27742256










      asked Feb 9 at 21:05









      Aaron Nichols

      686




      686




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          To find the number of files in a directory that do not contain .x.pdf, try:



          find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c


          To find the number of files in a directory that do not contain period - one or more x - period - pdf, try:



          find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


          The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1. For example:



          find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


          Note that because we use -printf '1', this method is safe even if the directory contains files whose names contain newline characters.






          share|improve this answer


















          • 1




            Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
            – Aaron Nichols
            Feb 9 at 21:17











          • @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
            – John1024
            Feb 21 at 18:23

















          up vote
          2
          down vote













          Without subdirectories:



          echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))


          because:



          for file in *.sh ; do echo -n 1+; done; echo 0;
          1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0





          share|improve this answer
















          • 1




            I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
            – John1024
            Feb 10 at 0:31






          • 1




            @John1024: Count all files, count all files with .x*.pdf, subtract.
            – user unknown
            Feb 10 at 0:51










          • user-unknown, OK. Very good.
            – John1024
            Feb 10 at 1:46


















          up vote
          0
          down vote













          You can exclude a file or files that match to a pattern from the ls command by using (one or more times) the option -I, --ignore=PATTERN (reference):



          ls -I "*.x*.pdf" "$Destination" | wc -l


          Or you could use the subtraction method in this way:



          echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))





          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2faskubuntu.com%2fquestions%2f1004680%2fcounting-files-in-a-directory%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            8
            down vote



            accepted










            To find the number of files in a directory that do not contain .x.pdf, try:



            find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c


            To find the number of files in a directory that do not contain period - one or more x - period - pdf, try:



            find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1. For example:



            find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            Note that because we use -printf '1', this method is safe even if the directory contains files whose names contain newline characters.






            share|improve this answer


















            • 1




              Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
              – Aaron Nichols
              Feb 9 at 21:17











            • @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
              – John1024
              Feb 21 at 18:23














            up vote
            8
            down vote



            accepted










            To find the number of files in a directory that do not contain .x.pdf, try:



            find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c


            To find the number of files in a directory that do not contain period - one or more x - period - pdf, try:



            find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1. For example:



            find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            Note that because we use -printf '1', this method is safe even if the directory contains files whose names contain newline characters.






            share|improve this answer


















            • 1




              Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
              – Aaron Nichols
              Feb 9 at 21:17











            • @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
              – John1024
              Feb 21 at 18:23












            up vote
            8
            down vote



            accepted







            up vote
            8
            down vote



            accepted






            To find the number of files in a directory that do not contain .x.pdf, try:



            find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c


            To find the number of files in a directory that do not contain period - one or more x - period - pdf, try:



            find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1. For example:



            find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            Note that because we use -printf '1', this method is safe even if the directory contains files whose names contain newline characters.






            share|improve this answer














            To find the number of files in a directory that do not contain .x.pdf, try:



            find "$Destination" -mindepth 1 ! -name '*.x.pdf' -printf '1' | wc -c


            To find the number of files in a directory that do not contain period - one or more x - period - pdf, try:



            find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            The above search recursively through subdirectories. If you don't want that, add the option -maxdepth 1. For example:



            find "$Destination" -mindepth 1 -maxdepth 1 ! -regex '.*.x+.pdf' -printf '1' | wc -c


            Note that because we use -printf '1', this method is safe even if the directory contains files whose names contain newline characters.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 21 at 18:22

























            answered Feb 9 at 21:13









            John1024

            9,2441933




            9,2441933







            • 1




              Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
              – Aaron Nichols
              Feb 9 at 21:17











            • @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
              – John1024
              Feb 21 at 18:23












            • 1




              Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
              – Aaron Nichols
              Feb 9 at 21:17











            • @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
              – John1024
              Feb 21 at 18:23







            1




            1




            Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
            – Aaron Nichols
            Feb 9 at 21:17





            Altered your second example and tested it. Works! Thank you. find "$Destination" -mindepth 1 ! -regex '.*.x+.pdf' -printf '1n' | wc -l
            – Aaron Nichols
            Feb 9 at 21:17













            @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
            – John1024
            Feb 21 at 18:23




            @DavidFoerster Yes, that does seem simpler. Answer updated to eliminate n. Thanks.
            – John1024
            Feb 21 at 18:23












            up vote
            2
            down vote













            Without subdirectories:



            echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))


            because:



            for file in *.sh ; do echo -n 1+; done; echo 0;
            1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0





            share|improve this answer
















            • 1




              I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
              – John1024
              Feb 10 at 0:31






            • 1




              @John1024: Count all files, count all files with .x*.pdf, subtract.
              – user unknown
              Feb 10 at 0:51










            • user-unknown, OK. Very good.
              – John1024
              Feb 10 at 1:46















            up vote
            2
            down vote













            Without subdirectories:



            echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))


            because:



            for file in *.sh ; do echo -n 1+; done; echo 0;
            1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0





            share|improve this answer
















            • 1




              I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
              – John1024
              Feb 10 at 0:31






            • 1




              @John1024: Count all files, count all files with .x*.pdf, subtract.
              – user unknown
              Feb 10 at 0:51










            • user-unknown, OK. Very good.
              – John1024
              Feb 10 at 1:46













            up vote
            2
            down vote










            up vote
            2
            down vote









            Without subdirectories:



            echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))


            because:



            for file in *.sh ; do echo -n 1+; done; echo 0;
            1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0





            share|improve this answer












            Without subdirectories:



            echo $(($(for file in *.sh ; do echo -n 1+; done; echo 0;)))


            because:



            for file in *.sh ; do echo -n 1+; done; echo 0;
            1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+0






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 9 at 23:50









            user unknown

            4,80622151




            4,80622151







            • 1




              I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
              – John1024
              Feb 10 at 0:31






            • 1




              @John1024: Count all files, count all files with .x*.pdf, subtract.
              – user unknown
              Feb 10 at 0:51










            • user-unknown, OK. Very good.
              – John1024
              Feb 10 at 1:46













            • 1




              I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
              – John1024
              Feb 10 at 0:31






            • 1




              @John1024: Count all files, count all files with .x*.pdf, subtract.
              – user unknown
              Feb 10 at 0:51










            • user-unknown, OK. Very good.
              – John1024
              Feb 10 at 1:46








            1




            1




            I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
            – John1024
            Feb 10 at 0:31




            I see how this counts files in a directory but how does it avoid counting the files that the OP doesn't want to count: "But herein lies my problem. I would like to count the number of files that do not contain .x. .xx. and so on"?
            – John1024
            Feb 10 at 0:31




            1




            1




            @John1024: Count all files, count all files with .x*.pdf, subtract.
            – user unknown
            Feb 10 at 0:51




            @John1024: Count all files, count all files with .x*.pdf, subtract.
            – user unknown
            Feb 10 at 0:51












            user-unknown, OK. Very good.
            – John1024
            Feb 10 at 1:46





            user-unknown, OK. Very good.
            – John1024
            Feb 10 at 1:46











            up vote
            0
            down vote













            You can exclude a file or files that match to a pattern from the ls command by using (one or more times) the option -I, --ignore=PATTERN (reference):



            ls -I "*.x*.pdf" "$Destination" | wc -l


            Or you could use the subtraction method in this way:



            echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))





            share|improve this answer


























              up vote
              0
              down vote













              You can exclude a file or files that match to a pattern from the ls command by using (one or more times) the option -I, --ignore=PATTERN (reference):



              ls -I "*.x*.pdf" "$Destination" | wc -l


              Or you could use the subtraction method in this way:



              echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))





              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can exclude a file or files that match to a pattern from the ls command by using (one or more times) the option -I, --ignore=PATTERN (reference):



                ls -I "*.x*.pdf" "$Destination" | wc -l


                Or you could use the subtraction method in this way:



                echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))





                share|improve this answer














                You can exclude a file or files that match to a pattern from the ls command by using (one or more times) the option -I, --ignore=PATTERN (reference):



                ls -I "*.x*.pdf" "$Destination" | wc -l


                Or you could use the subtraction method in this way:



                echo $(($(ls "$Destination" | wc -l) - $(ls "$Destination"/*.x*.pdf | wc -l)))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 10 at 8:48

























                answered Feb 10 at 8:33









                pa4080

                12.3k52357




                12.3k52357



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1004680%2fcounting-files-in-a-directory%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Which professions warranted travel in Medieval times?

                    How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

                    Trouble downloading packages list due to a “Hash sum mismatch” error