Adding size of files using shell script











up vote
2
down vote

favorite












I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:



$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112









share|improve this question




















  • 3




    you don't we simply use du?
    – msp9011
    22 hours ago






  • 1




    @msp9011, du will calculate also subdirectories
    – Romeo Ninov
    21 hours ago






  • 1




    @RomeoNinov here we are checking only files...du -b /etc/*.conf
    – msp9011
    20 hours ago










  • @msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
    – BlackJack
    11 hours ago















up vote
2
down vote

favorite












I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:



$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112









share|improve this question




















  • 3




    you don't we simply use du?
    – msp9011
    22 hours ago






  • 1




    @msp9011, du will calculate also subdirectories
    – Romeo Ninov
    21 hours ago






  • 1




    @RomeoNinov here we are checking only files...du -b /etc/*.conf
    – msp9011
    20 hours ago










  • @msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
    – BlackJack
    11 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:



$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112









share|improve this question















I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:



$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112






shell-script shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 22 hours ago









muru

35k581154




35k581154










asked 22 hours ago









C. Cristi

1497




1497








  • 3




    you don't we simply use du?
    – msp9011
    22 hours ago






  • 1




    @msp9011, du will calculate also subdirectories
    – Romeo Ninov
    21 hours ago






  • 1




    @RomeoNinov here we are checking only files...du -b /etc/*.conf
    – msp9011
    20 hours ago










  • @msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
    – BlackJack
    11 hours ago














  • 3




    you don't we simply use du?
    – msp9011
    22 hours ago






  • 1




    @msp9011, du will calculate also subdirectories
    – Romeo Ninov
    21 hours ago






  • 1




    @RomeoNinov here we are checking only files...du -b /etc/*.conf
    – msp9011
    20 hours ago










  • @msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
    – BlackJack
    11 hours ago








3




3




you don't we simply use du?
– msp9011
22 hours ago




you don't we simply use du?
– msp9011
22 hours ago




1




1




@msp9011, du will calculate also subdirectories
– Romeo Ninov
21 hours ago




@msp9011, du will calculate also subdirectories
– Romeo Ninov
21 hours ago




1




1




@RomeoNinov here we are checking only files...du -b /etc/*.conf
– msp9011
20 hours ago




@RomeoNinov here we are checking only files...du -b /etc/*.conf
– msp9011
20 hours ago












@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
11 hours ago




@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
11 hours ago










6 Answers
6






active

oldest

votes

















up vote
1
down vote



accepted










You can do this …



total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done





share|improve this answer





















  • Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
    – C. Cristi
    22 hours ago






  • 4




    Don't use grep or cut on stat output. stat has format flags (%s) for this
    – ohno
    19 hours ago






  • 4




    Also, 'Size' is likely to assume an English or "C " locale.
    – xenoid
    15 hours ago


















up vote
11
down vote













stat -c "%s" /etc/*.conf|paste -sd+|bc -l





share|improve this answer





















  • Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
    – TheDudeAbides
    6 hours ago




















up vote
10
down vote













Also something like can do the work (with awk)



stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'





share|improve this answer




























    up vote
    9
    down vote













    With bc



    { stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc



    • The stat format adds a + sign and a continuation character after each size

    • a 0 is appended at the end to close the dangling final +






    share|improve this answer




























      up vote
      6
      down vote













      The most straightforward way is to use du -bc:



      $ du -bc /etc/*.conf
      5139 /etc/man_db.conf
      393 /etc/nsswitch.conf
      5532 total


      If you need to extract only the number of bytes, pipe the output to awk:



      $ du -bc /etc/*.conf | awk 'END { print $1 }'
      5532





      share|improve this answer








      New contributor




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


















      • Hope ... OP doesn't require the grand total size of all files...
        – msp9011
        20 hours ago










      • Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
        – Ruslan
        12 hours ago












      • @Ruslan The awk line also strips off the total
        – Izkata
        11 hours ago










      • @Izkata oh, indeed, didn't notice this bit.
        – Ruslan
        10 hours ago


















      up vote
      -1
      down vote













      The easiest way to sum numbers from a shell is using num-utils, which is available as a package in Debian-based distros such as Ubuntu. Simply pipe the output into numsum.



      Example:



      $ printf "%dn" 1 2 3 | numsum
      6


      The package contains a bunch of other useful numeric utils as well. Quoting from the project page:




      The 'num-utils' are a set of programs for dealing with numbers from the Unix command line. Much like the other Unix command line utilities like grep, awk, sort, cut, etc. these utilities work on data from both standard in and data from files.




      • numaverage: A program for calculating the average of numbers.

      • numbound: Finds the boundary numbers (min and max) of input.

      • numinterval: Shows the numeric intervals between each number in a sequence.

      • numnormalize: Normalizes a set of numbers between 0 and 1 by default.

      • numgrep: Like normal grep, but for sets of numbers.

      • numprocess: Do mathematical operations on numbers.

      • numsum: Add up all the numbers.

      • numrandom: Generate a random number from a given expression.

      • numrange: Generate a set of numbers in a range expression.

      • numround: Round each number according to its value.







      share|improve this answer





















      • This would be an answer if it showed how to use that to solve the problem at hand.
        – xenoid
        15 hours ago










      • The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
        – Zoltan
        15 hours ago










      • and all answers provided some sample code...
        – xenoid
        14 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',
      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%2f483135%2fadding-size-of-files-using-shell-script%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      You can do this …



      total=0
      for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
      total=$(expr $total + $s)
      done





      share|improve this answer





















      • Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
        – C. Cristi
        22 hours ago






      • 4




        Don't use grep or cut on stat output. stat has format flags (%s) for this
        – ohno
        19 hours ago






      • 4




        Also, 'Size' is likely to assume an English or "C " locale.
        – xenoid
        15 hours ago















      up vote
      1
      down vote



      accepted










      You can do this …



      total=0
      for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
      total=$(expr $total + $s)
      done





      share|improve this answer





















      • Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
        – C. Cristi
        22 hours ago






      • 4




        Don't use grep or cut on stat output. stat has format flags (%s) for this
        – ohno
        19 hours ago






      • 4




        Also, 'Size' is likely to assume an English or "C " locale.
        – xenoid
        15 hours ago













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      You can do this …



      total=0
      for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
      total=$(expr $total + $s)
      done





      share|improve this answer












      You can do this …



      total=0
      for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
      total=$(expr $total + $s)
      done






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 22 hours ago









      Red Cricket

      1,19331731




      1,19331731












      • Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
        – C. Cristi
        22 hours ago






      • 4




        Don't use grep or cut on stat output. stat has format flags (%s) for this
        – ohno
        19 hours ago






      • 4




        Also, 'Size' is likely to assume an English or "C " locale.
        – xenoid
        15 hours ago


















      • Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
        – C. Cristi
        22 hours ago






      • 4




        Don't use grep or cut on stat output. stat has format flags (%s) for this
        – ohno
        19 hours ago






      • 4




        Also, 'Size' is likely to assume an English or "C " locale.
        – xenoid
        15 hours ago
















      Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
      – C. Cristi
      22 hours ago




      Thanks! and if I want to output it in a file I do: echo total > my_file.txt, right? And what if I want to output the errors too what do I do then?
      – C. Cristi
      22 hours ago




      4




      4




      Don't use grep or cut on stat output. stat has format flags (%s) for this
      – ohno
      19 hours ago




      Don't use grep or cut on stat output. stat has format flags (%s) for this
      – ohno
      19 hours ago




      4




      4




      Also, 'Size' is likely to assume an English or "C " locale.
      – xenoid
      15 hours ago




      Also, 'Size' is likely to assume an English or "C " locale.
      – xenoid
      15 hours ago












      up vote
      11
      down vote













      stat -c "%s" /etc/*.conf|paste -sd+|bc -l





      share|improve this answer





















      • Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
        – TheDudeAbides
        6 hours ago

















      up vote
      11
      down vote













      stat -c "%s" /etc/*.conf|paste -sd+|bc -l





      share|improve this answer





















      • Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
        – TheDudeAbides
        6 hours ago















      up vote
      11
      down vote










      up vote
      11
      down vote









      stat -c "%s" /etc/*.conf|paste -sd+|bc -l





      share|improve this answer












      stat -c "%s" /etc/*.conf|paste -sd+|bc -l






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 22 hours ago









      Ipor Sircer

      10.2k11024




      10.2k11024












      • Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
        – TheDudeAbides
        6 hours ago




















      • Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
        – TheDudeAbides
        6 hours ago


















      Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
      – TheDudeAbides
      6 hours ago






      Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated) paste utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile.
      – TheDudeAbides
      6 hours ago












      up vote
      10
      down vote













      Also something like can do the work (with awk)



      stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'





      share|improve this answer

























        up vote
        10
        down vote













        Also something like can do the work (with awk)



        stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'





        share|improve this answer























          up vote
          10
          down vote










          up vote
          10
          down vote









          Also something like can do the work (with awk)



          stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'





          share|improve this answer












          Also something like can do the work (with awk)



          stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 22 hours ago









          Romeo Ninov

          4,89431627




          4,89431627






















              up vote
              9
              down vote













              With bc



              { stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc



              • The stat format adds a + sign and a continuation character after each size

              • a 0 is appended at the end to close the dangling final +






              share|improve this answer

























                up vote
                9
                down vote













                With bc



                { stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc



                • The stat format adds a + sign and a continuation character after each size

                • a 0 is appended at the end to close the dangling final +






                share|improve this answer























                  up vote
                  9
                  down vote










                  up vote
                  9
                  down vote









                  With bc



                  { stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc



                  • The stat format adds a + sign and a continuation character after each size

                  • a 0 is appended at the end to close the dangling final +






                  share|improve this answer












                  With bc



                  { stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc



                  • The stat format adds a + sign and a continuation character after each size

                  • a 0 is appended at the end to close the dangling final +







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 19 hours ago









                  xenoid

                  2,5481723




                  2,5481723






















                      up vote
                      6
                      down vote













                      The most straightforward way is to use du -bc:



                      $ du -bc /etc/*.conf
                      5139 /etc/man_db.conf
                      393 /etc/nsswitch.conf
                      5532 total


                      If you need to extract only the number of bytes, pipe the output to awk:



                      $ du -bc /etc/*.conf | awk 'END { print $1 }'
                      5532





                      share|improve this answer








                      New contributor




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


















                      • Hope ... OP doesn't require the grand total size of all files...
                        – msp9011
                        20 hours ago










                      • Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                        – Ruslan
                        12 hours ago












                      • @Ruslan The awk line also strips off the total
                        – Izkata
                        11 hours ago










                      • @Izkata oh, indeed, didn't notice this bit.
                        – Ruslan
                        10 hours ago















                      up vote
                      6
                      down vote













                      The most straightforward way is to use du -bc:



                      $ du -bc /etc/*.conf
                      5139 /etc/man_db.conf
                      393 /etc/nsswitch.conf
                      5532 total


                      If you need to extract only the number of bytes, pipe the output to awk:



                      $ du -bc /etc/*.conf | awk 'END { print $1 }'
                      5532





                      share|improve this answer








                      New contributor




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


















                      • Hope ... OP doesn't require the grand total size of all files...
                        – msp9011
                        20 hours ago










                      • Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                        – Ruslan
                        12 hours ago












                      • @Ruslan The awk line also strips off the total
                        – Izkata
                        11 hours ago










                      • @Izkata oh, indeed, didn't notice this bit.
                        – Ruslan
                        10 hours ago













                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      The most straightforward way is to use du -bc:



                      $ du -bc /etc/*.conf
                      5139 /etc/man_db.conf
                      393 /etc/nsswitch.conf
                      5532 total


                      If you need to extract only the number of bytes, pipe the output to awk:



                      $ du -bc /etc/*.conf | awk 'END { print $1 }'
                      5532





                      share|improve this answer








                      New contributor




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









                      The most straightforward way is to use du -bc:



                      $ du -bc /etc/*.conf
                      5139 /etc/man_db.conf
                      393 /etc/nsswitch.conf
                      5532 total


                      If you need to extract only the number of bytes, pipe the output to awk:



                      $ du -bc /etc/*.conf | awk 'END { print $1 }'
                      5532






                      share|improve this answer








                      New contributor




                      Martin Frodl 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 answer



                      share|improve this answer






                      New contributor




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









                      answered 20 hours ago









                      Martin Frodl

                      691




                      691




                      New contributor




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





                      New contributor





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






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












                      • Hope ... OP doesn't require the grand total size of all files...
                        – msp9011
                        20 hours ago










                      • Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                        – Ruslan
                        12 hours ago












                      • @Ruslan The awk line also strips off the total
                        – Izkata
                        11 hours ago










                      • @Izkata oh, indeed, didn't notice this bit.
                        – Ruslan
                        10 hours ago


















                      • Hope ... OP doesn't require the grand total size of all files...
                        – msp9011
                        20 hours ago










                      • Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                        – Ruslan
                        12 hours ago












                      • @Ruslan The awk line also strips off the total
                        – Izkata
                        11 hours ago










                      • @Izkata oh, indeed, didn't notice this bit.
                        – Ruslan
                        10 hours ago
















                      Hope ... OP doesn't require the grand total size of all files...
                      – msp9011
                      20 hours ago




                      Hope ... OP doesn't require the grand total size of all files...
                      – msp9011
                      20 hours ago












                      Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                      – Ruslan
                      12 hours ago






                      Also note that this gives disk usage, not apparent sizes of the files. --apparent-size option may be needed to use apparent sizes.
                      – Ruslan
                      12 hours ago














                      @Ruslan The awk line also strips off the total
                      – Izkata
                      11 hours ago




                      @Ruslan The awk line also strips off the total
                      – Izkata
                      11 hours ago












                      @Izkata oh, indeed, didn't notice this bit.
                      – Ruslan
                      10 hours ago




                      @Izkata oh, indeed, didn't notice this bit.
                      – Ruslan
                      10 hours ago










                      up vote
                      -1
                      down vote













                      The easiest way to sum numbers from a shell is using num-utils, which is available as a package in Debian-based distros such as Ubuntu. Simply pipe the output into numsum.



                      Example:



                      $ printf "%dn" 1 2 3 | numsum
                      6


                      The package contains a bunch of other useful numeric utils as well. Quoting from the project page:




                      The 'num-utils' are a set of programs for dealing with numbers from the Unix command line. Much like the other Unix command line utilities like grep, awk, sort, cut, etc. these utilities work on data from both standard in and data from files.




                      • numaverage: A program for calculating the average of numbers.

                      • numbound: Finds the boundary numbers (min and max) of input.

                      • numinterval: Shows the numeric intervals between each number in a sequence.

                      • numnormalize: Normalizes a set of numbers between 0 and 1 by default.

                      • numgrep: Like normal grep, but for sets of numbers.

                      • numprocess: Do mathematical operations on numbers.

                      • numsum: Add up all the numbers.

                      • numrandom: Generate a random number from a given expression.

                      • numrange: Generate a set of numbers in a range expression.

                      • numround: Round each number according to its value.







                      share|improve this answer





















                      • This would be an answer if it showed how to use that to solve the problem at hand.
                        – xenoid
                        15 hours ago










                      • The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                        – Zoltan
                        15 hours ago










                      • and all answers provided some sample code...
                        – xenoid
                        14 hours ago















                      up vote
                      -1
                      down vote













                      The easiest way to sum numbers from a shell is using num-utils, which is available as a package in Debian-based distros such as Ubuntu. Simply pipe the output into numsum.



                      Example:



                      $ printf "%dn" 1 2 3 | numsum
                      6


                      The package contains a bunch of other useful numeric utils as well. Quoting from the project page:




                      The 'num-utils' are a set of programs for dealing with numbers from the Unix command line. Much like the other Unix command line utilities like grep, awk, sort, cut, etc. these utilities work on data from both standard in and data from files.




                      • numaverage: A program for calculating the average of numbers.

                      • numbound: Finds the boundary numbers (min and max) of input.

                      • numinterval: Shows the numeric intervals between each number in a sequence.

                      • numnormalize: Normalizes a set of numbers between 0 and 1 by default.

                      • numgrep: Like normal grep, but for sets of numbers.

                      • numprocess: Do mathematical operations on numbers.

                      • numsum: Add up all the numbers.

                      • numrandom: Generate a random number from a given expression.

                      • numrange: Generate a set of numbers in a range expression.

                      • numround: Round each number according to its value.







                      share|improve this answer





















                      • This would be an answer if it showed how to use that to solve the problem at hand.
                        – xenoid
                        15 hours ago










                      • The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                        – Zoltan
                        15 hours ago










                      • and all answers provided some sample code...
                        – xenoid
                        14 hours ago













                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      The easiest way to sum numbers from a shell is using num-utils, which is available as a package in Debian-based distros such as Ubuntu. Simply pipe the output into numsum.



                      Example:



                      $ printf "%dn" 1 2 3 | numsum
                      6


                      The package contains a bunch of other useful numeric utils as well. Quoting from the project page:




                      The 'num-utils' are a set of programs for dealing with numbers from the Unix command line. Much like the other Unix command line utilities like grep, awk, sort, cut, etc. these utilities work on data from both standard in and data from files.




                      • numaverage: A program for calculating the average of numbers.

                      • numbound: Finds the boundary numbers (min and max) of input.

                      • numinterval: Shows the numeric intervals between each number in a sequence.

                      • numnormalize: Normalizes a set of numbers between 0 and 1 by default.

                      • numgrep: Like normal grep, but for sets of numbers.

                      • numprocess: Do mathematical operations on numbers.

                      • numsum: Add up all the numbers.

                      • numrandom: Generate a random number from a given expression.

                      • numrange: Generate a set of numbers in a range expression.

                      • numround: Round each number according to its value.







                      share|improve this answer












                      The easiest way to sum numbers from a shell is using num-utils, which is available as a package in Debian-based distros such as Ubuntu. Simply pipe the output into numsum.



                      Example:



                      $ printf "%dn" 1 2 3 | numsum
                      6


                      The package contains a bunch of other useful numeric utils as well. Quoting from the project page:




                      The 'num-utils' are a set of programs for dealing with numbers from the Unix command line. Much like the other Unix command line utilities like grep, awk, sort, cut, etc. these utilities work on data from both standard in and data from files.




                      • numaverage: A program for calculating the average of numbers.

                      • numbound: Finds the boundary numbers (min and max) of input.

                      • numinterval: Shows the numeric intervals between each number in a sequence.

                      • numnormalize: Normalizes a set of numbers between 0 and 1 by default.

                      • numgrep: Like normal grep, but for sets of numbers.

                      • numprocess: Do mathematical operations on numbers.

                      • numsum: Add up all the numbers.

                      • numrandom: Generate a random number from a given expression.

                      • numrange: Generate a set of numbers in a range expression.

                      • numround: Round each number according to its value.








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 16 hours ago









                      Zoltan

                      217110




                      217110












                      • This would be an answer if it showed how to use that to solve the problem at hand.
                        – xenoid
                        15 hours ago










                      • The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                        – Zoltan
                        15 hours ago










                      • and all answers provided some sample code...
                        – xenoid
                        14 hours ago


















                      • This would be an answer if it showed how to use that to solve the problem at hand.
                        – xenoid
                        15 hours ago










                      • The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                        – Zoltan
                        15 hours ago










                      • and all answers provided some sample code...
                        – xenoid
                        14 hours ago
















                      This would be an answer if it showed how to use that to solve the problem at hand.
                      – xenoid
                      15 hours ago




                      This would be an answer if it showed how to use that to solve the problem at hand.
                      – xenoid
                      15 hours ago












                      The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                      – Zoltan
                      15 hours ago




                      The question description already shows a way of getting the sizes of the files and specifically asks about how to sum them.
                      – Zoltan
                      15 hours ago












                      and all answers provided some sample code...
                      – xenoid
                      14 hours ago




                      and all answers provided some sample code...
                      – xenoid
                      14 hours ago


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483135%2fadding-size-of-files-using-shell-script%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga