Printing the sum-size of copied files in bash

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











up vote
6
down vote

favorite












I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.



#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f

cd $input_folder

for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done


This bash prints every second the size of the file being copied.



For example, in second 1 it copies file A and prints the size of file A. In second 2 it copies file B and prints the size of file B instead of the total size of files A+B. In second 3 it copies the file C and prints the size of file C instead of the total size of files A+B+C



How can I do it in bash specifically?










share|improve this question



















  • 1




    You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
    – RalfFriedl
    Sep 9 at 16:31










  • @ RalfFriedl. Thanks! probably just sleep 0.1 ?
    – Goro
    Sep 9 at 18:21














up vote
6
down vote

favorite












I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.



#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f

cd $input_folder

for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done


This bash prints every second the size of the file being copied.



For example, in second 1 it copies file A and prints the size of file A. In second 2 it copies file B and prints the size of file B instead of the total size of files A+B. In second 3 it copies the file C and prints the size of file C instead of the total size of files A+B+C



How can I do it in bash specifically?










share|improve this question



















  • 1




    You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
    – RalfFriedl
    Sep 9 at 16:31










  • @ RalfFriedl. Thanks! probably just sleep 0.1 ?
    – Goro
    Sep 9 at 18:21












up vote
6
down vote

favorite









up vote
6
down vote

favorite











I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.



#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f

cd $input_folder

for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done


This bash prints every second the size of the file being copied.



For example, in second 1 it copies file A and prints the size of file A. In second 2 it copies file B and prints the size of file B instead of the total size of files A+B. In second 3 it copies the file C and prints the size of file C instead of the total size of files A+B+C



How can I do it in bash specifically?










share|improve this question















I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.



#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f

cd $input_folder

for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done


This bash prints every second the size of the file being copied.



For example, in second 1 it copies file A and prints the size of file A. In second 2 it copies file B and prints the size of file B instead of the total size of files A+B. In second 3 it copies the file C and prints the size of file C instead of the total size of files A+B+C



How can I do it in bash specifically?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 9 at 16:11

























asked Sep 9 at 16:07









TNT

308111




308111







  • 1




    You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
    – RalfFriedl
    Sep 9 at 16:31










  • @ RalfFriedl. Thanks! probably just sleep 0.1 ?
    – Goro
    Sep 9 at 18:21












  • 1




    You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
    – RalfFriedl
    Sep 9 at 16:31










  • @ RalfFriedl. Thanks! probably just sleep 0.1 ?
    – Goro
    Sep 9 at 18:21







1




1




You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31




You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31












@ RalfFriedl. Thanks! probably just sleep 0.1 ?
– Goro
Sep 9 at 18:21




@ RalfFriedl. Thanks! probably just sleep 0.1 ?
– Goro
Sep 9 at 18:21










4 Answers
4






active

oldest

votes

















up vote
5
down vote













Just sum up the single sizes and echo it afterwards.



#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f

cd $input_folder

size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done

echo $size_sum





share|improve this answer



























    up vote
    1
    down vote













    size=$(wc -c < $i) 


    This isn't a very smart way to get the size of a file. Running wc like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.



    This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.



    With GNU utilities, you can get the size of the file with stat -c %s "$filename". Sadly, the options stat takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)



    cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"


    Here, the pipeline seems odd. cp doesn't produce any output (to standard output), at least not without -i, so there's nothing to pipe. Even if it did, sleep doesn't read anything so any output from cp would be wasted. The same thing happens between sleep and echo. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.



    If you want to sum the file sizes, the shell's arithmetic expansion $(( .. )) is what you probably want. So, without any extra sleeps:



    dest=/path/to/destination
    total=0
    for file in *.tiff; do
    size=$(stat -c %s "$file");
    printf "current %d total %dn" "$size" "$((total += size))"
    cp -- "$file" "$dest"
    done





    share|improve this answer



























      up vote
      0
      down vote













      To save on the arithmetic in your script, you can just take the size of the concatenation of the files:



      cat *.tiff | wc


      The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.






      share|improve this answer










      New contributor




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

















      • I think this should be wc -c.
        – user1934428
        2 days ago

















      up vote
      0
      down vote













      It is possible to sum with += if the variable is declared as integer:



      $ declare -i size
      $ size+=100; echo "$size"
      100
      $ size+=200; echo "$size"
      300


      Your script (with variable expansions correctly quoted):



      #!/bin/bash
      input_folder=./a/b/c
      output_folder=./d/e/f
      declare -i size

      for i in "$input_folder"/*; do
      size+=$(wc -c < "$i")
      cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
      done





      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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467876%2fprinting-the-sum-size-of-copied-files-in-bash%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        5
        down vote













        Just sum up the single sizes and echo it afterwards.



        #!/bin/bash
        input_folder=/a/b/c
        output_folder=/d/e/f

        cd $input_folder

        size_sum=0
        for i in *.tiff; do
        size=$(wc -c < $i)
        size_sum=$((size_sum + size))
        cp -v $i $out | sleep 1
        done

        echo $size_sum





        share|improve this answer
























          up vote
          5
          down vote













          Just sum up the single sizes and echo it afterwards.



          #!/bin/bash
          input_folder=/a/b/c
          output_folder=/d/e/f

          cd $input_folder

          size_sum=0
          for i in *.tiff; do
          size=$(wc -c < $i)
          size_sum=$((size_sum + size))
          cp -v $i $out | sleep 1
          done

          echo $size_sum





          share|improve this answer






















            up vote
            5
            down vote










            up vote
            5
            down vote









            Just sum up the single sizes and echo it afterwards.



            #!/bin/bash
            input_folder=/a/b/c
            output_folder=/d/e/f

            cd $input_folder

            size_sum=0
            for i in *.tiff; do
            size=$(wc -c < $i)
            size_sum=$((size_sum + size))
            cp -v $i $out | sleep 1
            done

            echo $size_sum





            share|improve this answer












            Just sum up the single sizes and echo it afterwards.



            #!/bin/bash
            input_folder=/a/b/c
            output_folder=/d/e/f

            cd $input_folder

            size_sum=0
            for i in *.tiff; do
            size=$(wc -c < $i)
            size_sum=$((size_sum + size))
            cp -v $i $out | sleep 1
            done

            echo $size_sum






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 9 at 16:23









            Thomas

            3,62141124




            3,62141124






















                up vote
                1
                down vote













                size=$(wc -c < $i) 


                This isn't a very smart way to get the size of a file. Running wc like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.



                This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.



                With GNU utilities, you can get the size of the file with stat -c %s "$filename". Sadly, the options stat takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)



                cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"


                Here, the pipeline seems odd. cp doesn't produce any output (to standard output), at least not without -i, so there's nothing to pipe. Even if it did, sleep doesn't read anything so any output from cp would be wasted. The same thing happens between sleep and echo. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.



                If you want to sum the file sizes, the shell's arithmetic expansion $(( .. )) is what you probably want. So, without any extra sleeps:



                dest=/path/to/destination
                total=0
                for file in *.tiff; do
                size=$(stat -c %s "$file");
                printf "current %d total %dn" "$size" "$((total += size))"
                cp -- "$file" "$dest"
                done





                share|improve this answer
























                  up vote
                  1
                  down vote













                  size=$(wc -c < $i) 


                  This isn't a very smart way to get the size of a file. Running wc like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.



                  This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.



                  With GNU utilities, you can get the size of the file with stat -c %s "$filename". Sadly, the options stat takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)



                  cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"


                  Here, the pipeline seems odd. cp doesn't produce any output (to standard output), at least not without -i, so there's nothing to pipe. Even if it did, sleep doesn't read anything so any output from cp would be wasted. The same thing happens between sleep and echo. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.



                  If you want to sum the file sizes, the shell's arithmetic expansion $(( .. )) is what you probably want. So, without any extra sleeps:



                  dest=/path/to/destination
                  total=0
                  for file in *.tiff; do
                  size=$(stat -c %s "$file");
                  printf "current %d total %dn" "$size" "$((total += size))"
                  cp -- "$file" "$dest"
                  done





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    size=$(wc -c < $i) 


                    This isn't a very smart way to get the size of a file. Running wc like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.



                    This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.



                    With GNU utilities, you can get the size of the file with stat -c %s "$filename". Sadly, the options stat takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)



                    cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"


                    Here, the pipeline seems odd. cp doesn't produce any output (to standard output), at least not without -i, so there's nothing to pipe. Even if it did, sleep doesn't read anything so any output from cp would be wasted. The same thing happens between sleep and echo. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.



                    If you want to sum the file sizes, the shell's arithmetic expansion $(( .. )) is what you probably want. So, without any extra sleeps:



                    dest=/path/to/destination
                    total=0
                    for file in *.tiff; do
                    size=$(stat -c %s "$file");
                    printf "current %d total %dn" "$size" "$((total += size))"
                    cp -- "$file" "$dest"
                    done





                    share|improve this answer












                    size=$(wc -c < $i) 


                    This isn't a very smart way to get the size of a file. Running wc like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.



                    This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.



                    With GNU utilities, you can get the size of the file with stat -c %s "$filename". Sadly, the options stat takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)



                    cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"


                    Here, the pipeline seems odd. cp doesn't produce any output (to standard output), at least not without -i, so there's nothing to pipe. Even if it did, sleep doesn't read anything so any output from cp would be wasted. The same thing happens between sleep and echo. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.



                    If you want to sum the file sizes, the shell's arithmetic expansion $(( .. )) is what you probably want. So, without any extra sleeps:



                    dest=/path/to/destination
                    total=0
                    for file in *.tiff; do
                    size=$(stat -c %s "$file");
                    printf "current %d total %dn" "$size" "$((total += size))"
                    cp -- "$file" "$dest"
                    done






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 days ago









                    ilkkachu

                    50.8k678140




                    50.8k678140




















                        up vote
                        0
                        down vote













                        To save on the arithmetic in your script, you can just take the size of the concatenation of the files:



                        cat *.tiff | wc


                        The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.






                        share|improve this answer










                        New contributor




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

















                        • I think this should be wc -c.
                          – user1934428
                          2 days ago














                        up vote
                        0
                        down vote













                        To save on the arithmetic in your script, you can just take the size of the concatenation of the files:



                        cat *.tiff | wc


                        The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.






                        share|improve this answer










                        New contributor




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

















                        • I think this should be wc -c.
                          – user1934428
                          2 days ago












                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        To save on the arithmetic in your script, you can just take the size of the concatenation of the files:



                        cat *.tiff | wc


                        The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.






                        share|improve this answer










                        New contributor




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









                        To save on the arithmetic in your script, you can just take the size of the concatenation of the files:



                        cat *.tiff | wc


                        The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.







                        share|improve this answer










                        New contributor




                        Rob Arthan 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








                        edited 2 days ago









                        muru

                        33.6k577144




                        33.6k577144






                        New contributor




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









                        answered 2 days ago









                        Rob Arthan

                        101




                        101




                        New contributor




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





                        New contributor





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






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











                        • I think this should be wc -c.
                          – user1934428
                          2 days ago
















                        • I think this should be wc -c.
                          – user1934428
                          2 days ago















                        I think this should be wc -c.
                        – user1934428
                        2 days ago




                        I think this should be wc -c.
                        – user1934428
                        2 days ago










                        up vote
                        0
                        down vote













                        It is possible to sum with += if the variable is declared as integer:



                        $ declare -i size
                        $ size+=100; echo "$size"
                        100
                        $ size+=200; echo "$size"
                        300


                        Your script (with variable expansions correctly quoted):



                        #!/bin/bash
                        input_folder=./a/b/c
                        output_folder=./d/e/f
                        declare -i size

                        for i in "$input_folder"/*; do
                        size+=$(wc -c < "$i")
                        cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
                        done





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          It is possible to sum with += if the variable is declared as integer:



                          $ declare -i size
                          $ size+=100; echo "$size"
                          100
                          $ size+=200; echo "$size"
                          300


                          Your script (with variable expansions correctly quoted):



                          #!/bin/bash
                          input_folder=./a/b/c
                          output_folder=./d/e/f
                          declare -i size

                          for i in "$input_folder"/*; do
                          size+=$(wc -c < "$i")
                          cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
                          done





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            It is possible to sum with += if the variable is declared as integer:



                            $ declare -i size
                            $ size+=100; echo "$size"
                            100
                            $ size+=200; echo "$size"
                            300


                            Your script (with variable expansions correctly quoted):



                            #!/bin/bash
                            input_folder=./a/b/c
                            output_folder=./d/e/f
                            declare -i size

                            for i in "$input_folder"/*; do
                            size+=$(wc -c < "$i")
                            cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
                            done





                            share|improve this answer












                            It is possible to sum with += if the variable is declared as integer:



                            $ declare -i size
                            $ size+=100; echo "$size"
                            100
                            $ size+=200; echo "$size"
                            300


                            Your script (with variable expansions correctly quoted):



                            #!/bin/bash
                            input_folder=./a/b/c
                            output_folder=./d/e/f
                            declare -i size

                            for i in "$input_folder"/*; do
                            size+=$(wc -c < "$i")
                            cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
                            done






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 days ago









                            Isaac

                            6,9221834




                            6,9221834



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467876%2fprinting-the-sum-size-of-copied-files-in-bash%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery