Iterating over files in Bash and obtaining the index and the counts

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












2















The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:



for f in *.jpg; do
echo "- Processing file: $f"
done


But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.










share|improve this question




























    2















    The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:



    for f in *.jpg; do
    echo "- Processing file: $f"
    done


    But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.










    share|improve this question


























      2












      2








      2








      The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:



      for f in *.jpg; do
      echo "- Processing file: $f"
      done


      But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.










      share|improve this question
















      The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:



      for f in *.jpg; do
      echo "- Processing file: $f"
      done


      But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.







      shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 3:02







      bman

















      asked Jan 4 at 2:49









      bmanbman

      13019




      13019




















          1 Answer
          1






          active

          oldest

          votes


















          4














          I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.



          files=(*.jpg)
          total=$#files[@]
          i=0
          for f in "$files[@]"; do
          i=$(( i + 1 ))
          echo index $i
          echo total $total
          echo "- Processing file: $f"
          done


          Explanation




          • files=(*.jpg): store the glob into the array $files


          • total=$#files[@]: read the total into $total


          • i=0: initialise $i to 0.


          • i=$(( i + 1 )): add 1 to $i each loop

          This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.






          share|improve this answer

























          • I need to know the count while the for loop is running to show the progress, not at the end.

            – bman
            Jan 4 at 2:55











          • @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

            – Sparhawk
            Jan 4 at 2:56











          • It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

            – bman
            Jan 4 at 2:58











          • @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

            – Sparhawk
            Jan 4 at 2:59











          • @bman Okay done.

            – Sparhawk
            Jan 4 at 3:05










          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%2f492366%2fiterating-over-files-in-bash-and-obtaining-the-index-and-the-counts%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









          4














          I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.



          files=(*.jpg)
          total=$#files[@]
          i=0
          for f in "$files[@]"; do
          i=$(( i + 1 ))
          echo index $i
          echo total $total
          echo "- Processing file: $f"
          done


          Explanation




          • files=(*.jpg): store the glob into the array $files


          • total=$#files[@]: read the total into $total


          • i=0: initialise $i to 0.


          • i=$(( i + 1 )): add 1 to $i each loop

          This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.






          share|improve this answer

























          • I need to know the count while the for loop is running to show the progress, not at the end.

            – bman
            Jan 4 at 2:55











          • @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

            – Sparhawk
            Jan 4 at 2:56











          • It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

            – bman
            Jan 4 at 2:58











          • @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

            – Sparhawk
            Jan 4 at 2:59











          • @bman Okay done.

            – Sparhawk
            Jan 4 at 3:05















          4














          I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.



          files=(*.jpg)
          total=$#files[@]
          i=0
          for f in "$files[@]"; do
          i=$(( i + 1 ))
          echo index $i
          echo total $total
          echo "- Processing file: $f"
          done


          Explanation




          • files=(*.jpg): store the glob into the array $files


          • total=$#files[@]: read the total into $total


          • i=0: initialise $i to 0.


          • i=$(( i + 1 )): add 1 to $i each loop

          This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.






          share|improve this answer

























          • I need to know the count while the for loop is running to show the progress, not at the end.

            – bman
            Jan 4 at 2:55











          • @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

            – Sparhawk
            Jan 4 at 2:56











          • It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

            – bman
            Jan 4 at 2:58











          • @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

            – Sparhawk
            Jan 4 at 2:59











          • @bman Okay done.

            – Sparhawk
            Jan 4 at 3:05













          4












          4








          4







          I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.



          files=(*.jpg)
          total=$#files[@]
          i=0
          for f in "$files[@]"; do
          i=$(( i + 1 ))
          echo index $i
          echo total $total
          echo "- Processing file: $f"
          done


          Explanation




          • files=(*.jpg): store the glob into the array $files


          • total=$#files[@]: read the total into $total


          • i=0: initialise $i to 0.


          • i=$(( i + 1 )): add 1 to $i each loop

          This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.






          share|improve this answer















          I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.



          files=(*.jpg)
          total=$#files[@]
          i=0
          for f in "$files[@]"; do
          i=$(( i + 1 ))
          echo index $i
          echo total $total
          echo "- Processing file: $f"
          done


          Explanation




          • files=(*.jpg): store the glob into the array $files


          • total=$#files[@]: read the total into $total


          • i=0: initialise $i to 0.


          • i=$(( i + 1 )): add 1 to $i each loop

          This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 3:05

























          answered Jan 4 at 2:54









          SparhawkSparhawk

          9,41263992




          9,41263992












          • I need to know the count while the for loop is running to show the progress, not at the end.

            – bman
            Jan 4 at 2:55











          • @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

            – Sparhawk
            Jan 4 at 2:56











          • It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

            – bman
            Jan 4 at 2:58











          • @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

            – Sparhawk
            Jan 4 at 2:59











          • @bman Okay done.

            – Sparhawk
            Jan 4 at 3:05

















          • I need to know the count while the for loop is running to show the progress, not at the end.

            – bman
            Jan 4 at 2:55











          • @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

            – Sparhawk
            Jan 4 at 2:56











          • It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

            – bman
            Jan 4 at 2:58











          • @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

            – Sparhawk
            Jan 4 at 2:59











          • @bman Okay done.

            – Sparhawk
            Jan 4 at 3:05
















          I need to know the count while the for loop is running to show the progress, not at the end.

          – bman
          Jan 4 at 2:55





          I need to know the count while the for loop is running to show the progress, not at the end.

          – bman
          Jan 4 at 2:55













          @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

          – Sparhawk
          Jan 4 at 2:56





          @bman I'm not sure what you mean. This will display the count during each loop, not at the end.

          – Sparhawk
          Jan 4 at 2:56













          It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

          – bman
          Jan 4 at 2:58





          It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files: ls -1q *.jpg | wc -l

          – bman
          Jan 4 at 2:58













          @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

          – Sparhawk
          Jan 4 at 2:59





          @bman Ah got it. that might work generally, but (as you suggest in your question), parsing ls is not great. Give me a minute and I'll edit.

          – Sparhawk
          Jan 4 at 2:59













          @bman Okay done.

          – Sparhawk
          Jan 4 at 3:05





          @bman Okay done.

          – Sparhawk
          Jan 4 at 3:05

















          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%2f492366%2fiterating-over-files-in-bash-and-obtaining-the-index-and-the-counts%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?