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












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 5 hours ago

























      asked 5 hours ago









      bman

      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
            5 hours ago










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










          • 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
            5 hours ago










          • @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
            5 hours ago










          • @bman Okay done.
            – Sparhawk
            4 hours ago











          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
            5 hours ago










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










          • 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
            5 hours ago










          • @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
            5 hours ago










          • @bman Okay done.
            – Sparhawk
            4 hours ago
















          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
            5 hours ago










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










          • 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
            5 hours ago










          • @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
            5 hours ago










          • @bman Okay done.
            – Sparhawk
            4 hours ago














          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 4 hours ago

























          answered 5 hours ago









          Sparhawk

          9,33263991




          9,33263991












          • I need to know the count while the for loop is running to show the progress, not at the end.
            – bman
            5 hours ago










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










          • 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
            5 hours ago










          • @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
            5 hours ago










          • @bman Okay done.
            – Sparhawk
            4 hours ago


















          • I need to know the count while the for loop is running to show the progress, not at the end.
            – bman
            5 hours ago










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










          • 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
            5 hours ago










          • @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
            5 hours ago










          • @bman Okay done.
            – Sparhawk
            4 hours ago
















          I need to know the count while the for loop is running to show the progress, not at the end.
          – bman
          5 hours ago




          I need to know the count while the for loop is running to show the progress, not at the end.
          – bman
          5 hours ago












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




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












          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
          5 hours ago




          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
          5 hours ago












          @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
          5 hours ago




          @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
          5 hours ago












          @bman Okay done.
          – Sparhawk
          4 hours ago




          @bman Okay done.
          – Sparhawk
          4 hours ago


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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

          Eastern Orthodox Church

          Zagreb

          Understanding the information contained in the Deep Space Network XML data?