sum all numbers from “du”

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












5















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question
























  • du -bs /tmp would get you the answer too

    – roaima
    Feb 28 at 17:55












  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    Feb 28 at 17:57











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    Feb 28 at 17:59
















5















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question
























  • du -bs /tmp would get you the answer too

    – roaima
    Feb 28 at 17:55












  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    Feb 28 at 17:57











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    Feb 28 at 17:59














5












5








5


1






We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question
















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?







linux shell-script awk perl disk-usage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 17:33









Jeff Schaller

44k1161142




44k1161142










asked Feb 28 at 17:51









yaelyael

2,78132777




2,78132777












  • du -bs /tmp would get you the answer too

    – roaima
    Feb 28 at 17:55












  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    Feb 28 at 17:57











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    Feb 28 at 17:59


















  • du -bs /tmp would get you the answer too

    – roaima
    Feb 28 at 17:55












  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    Feb 28 at 17:57











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    Feb 28 at 17:59

















du -bs /tmp would get you the answer too

– roaima
Feb 28 at 17:55






du -bs /tmp would get you the answer too

– roaima
Feb 28 at 17:55














See How can I quickly sum all numbers in a file?

– glenn jackman
Feb 28 at 17:57





See How can I quickly sum all numbers in a file?

– glenn jackman
Feb 28 at 17:57













See also Is there a way to sum up the size of files listed?

– Jeff Schaller
Feb 28 at 17:59






See also Is there a way to sum up the size of files listed?

– Jeff Schaller
Feb 28 at 17:59











3 Answers
3






active

oldest

votes


















14














In AWK:



 sum += $1 
END print sum


So



du -b /tmp/* | awk ' sum += $1 END print sum '


Note that the result won’t be correct if the directories under /tmp have subdirectories themselves, because du produces running totals on directories and their children.



du -s will calculate the sum for you correctly (on all subdirectories and files in /tmp, including hidden ones):



du -sb /tmp


and du -c will calculate the sum of the listed directories and files, correctly too:



du -cb /tmp/*





share|improve this answer
































    4














    It is simple you can use:



     du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'


    If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



    size1 file1
    size2 file2
    size_total .


    So now you should avoid this last entry, so use:



    du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'


    However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



    du -s directory





    share|improve this answer




















    • 1





      variables initialize to zero, if you'd like to golf some bytes off :)

      – Jeff Schaller
      Feb 28 at 17:57


















    2














    You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



    $ du -sb file1 file2
    17 file1
    18 file2

    $ du -cb file1 file2
    17 file1
    18 file2
    35 total


    BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



    $ du -ch file1 file2
    4.0K file1
    4.0K file2
    8.0K total





    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%2f503601%2fsum-all-numbers-from-du%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      14














      In AWK:



       sum += $1 
      END print sum


      So



      du -b /tmp/* | awk ' sum += $1 END print sum '


      Note that the result won’t be correct if the directories under /tmp have subdirectories themselves, because du produces running totals on directories and their children.



      du -s will calculate the sum for you correctly (on all subdirectories and files in /tmp, including hidden ones):



      du -sb /tmp


      and du -c will calculate the sum of the listed directories and files, correctly too:



      du -cb /tmp/*





      share|improve this answer





























        14














        In AWK:



         sum += $1 
        END print sum


        So



        du -b /tmp/* | awk ' sum += $1 END print sum '


        Note that the result won’t be correct if the directories under /tmp have subdirectories themselves, because du produces running totals on directories and their children.



        du -s will calculate the sum for you correctly (on all subdirectories and files in /tmp, including hidden ones):



        du -sb /tmp


        and du -c will calculate the sum of the listed directories and files, correctly too:



        du -cb /tmp/*





        share|improve this answer



























          14












          14








          14







          In AWK:



           sum += $1 
          END print sum


          So



          du -b /tmp/* | awk ' sum += $1 END print sum '


          Note that the result won’t be correct if the directories under /tmp have subdirectories themselves, because du produces running totals on directories and their children.



          du -s will calculate the sum for you correctly (on all subdirectories and files in /tmp, including hidden ones):



          du -sb /tmp


          and du -c will calculate the sum of the listed directories and files, correctly too:



          du -cb /tmp/*





          share|improve this answer















          In AWK:



           sum += $1 
          END print sum


          So



          du -b /tmp/* | awk ' sum += $1 END print sum '


          Note that the result won’t be correct if the directories under /tmp have subdirectories themselves, because du produces running totals on directories and their children.



          du -s will calculate the sum for you correctly (on all subdirectories and files in /tmp, including hidden ones):



          du -sb /tmp


          and du -c will calculate the sum of the listed directories and files, correctly too:



          du -cb /tmp/*






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 19:23









          Matthew

          4162616




          4162616










          answered Feb 28 at 17:55









          Stephen KittStephen Kitt

          178k24405481




          178k24405481























              4














              It is simple you can use:



               du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer




















              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                Feb 28 at 17:57















              4














              It is simple you can use:



               du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer




















              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                Feb 28 at 17:57













              4












              4








              4







              It is simple you can use:



               du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer















              It is simple you can use:



               du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 28 at 18:19

























              answered Feb 28 at 17:56









              Prvt_YadvPrvt_Yadv

              2,92531327




              2,92531327







              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                Feb 28 at 17:57












              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                Feb 28 at 17:57







              1




              1





              variables initialize to zero, if you'd like to golf some bytes off :)

              – Jeff Schaller
              Feb 28 at 17:57





              variables initialize to zero, if you'd like to golf some bytes off :)

              – Jeff Schaller
              Feb 28 at 17:57











              2














              You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



              $ du -sb file1 file2
              17 file1
              18 file2

              $ du -cb file1 file2
              17 file1
              18 file2
              35 total


              BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



              $ du -ch file1 file2
              4.0K file1
              4.0K file2
              8.0K total





              share|improve this answer



























                2














                You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                $ du -sb file1 file2
                17 file1
                18 file2

                $ du -cb file1 file2
                17 file1
                18 file2
                35 total


                BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                $ du -ch file1 file2
                4.0K file1
                4.0K file2
                8.0K total





                share|improve this answer

























                  2












                  2








                  2







                  You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                  $ du -sb file1 file2
                  17 file1
                  18 file2

                  $ du -cb file1 file2
                  17 file1
                  18 file2
                  35 total


                  BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                  $ du -ch file1 file2
                  4.0K file1
                  4.0K file2
                  8.0K total





                  share|improve this answer













                  You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                  $ du -sb file1 file2
                  17 file1
                  18 file2

                  $ du -cb file1 file2
                  17 file1
                  18 file2
                  35 total


                  BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                  $ du -ch file1 file2
                  4.0K file1
                  4.0K file2
                  8.0K total






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 28 at 18:28









                  jimmijjimmij

                  32.3k875109




                  32.3k875109



























                      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%2f503601%2fsum-all-numbers-from-du%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?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay