Get the exact size of files retrieved by find outputHow to get folder size ignoring hard links?Using parameters in a scriptUsing find and sed to copy 20th line of many files into one fileHow to copy modified files while preserving folder structureHow to find files and act on them (find + exec)Script or command to list all top-level directories that contain sub-directories with modified files less than 30 days oldHow does Linux calculate the total block count when running /bin/ls -l?Escaping double quotes for variables in bash and qmake

Can a passenger predict that an airline or a tour operator is about to go bankrupt?

Lighthouse Alternatives

麦酒 (ばくしゅ) for "beer"

Would an object shot from earth fall into the sun?

I just compiled bitcoind and run for the first time. Do I already have a private key?

How do we know neutrons have no charge?

Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?

Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?

Get the exact size of files retrieved by find output

As a team leader is it appropriate to bring in fundraiser candy?

Why does `FindFit` fail so badly in this simple case?

Realistically, how much do you need to start investing?

What action is recommended if your accommodation refuses to let you leave without paying additional fees?

Meaning of "fin" in "fin dai tempi"

The answer is a girl's name (my future granddaughter) - can anyone help?

How is this situation not a checkmate?

How to level a picture frame hung on a single nail?

How to find places to store/land a private airplane?

How to say "respectively" in German when listing (enumerating) things

A word that refers to saying something in an attempt to anger or embarrass someone into doing something that they don’t want to do?

What's the global, general word that stands for "center tone of a song"?

Does Bank Manager's discretion still exist in Mortgage Lending

Why aren't faces sharp in my f/1.8 portraits even though I'm carefully using center-point autofocus?

Approximate the perfect fifth



Get the exact size of files retrieved by find output


How to get folder size ignoring hard links?Using parameters in a scriptUsing find and sed to copy 20th line of many files into one fileHow to copy modified files while preserving folder structureHow to find files and act on them (find + exec)Script or command to list all top-level directories that contain sub-directories with modified files less than 30 days oldHow does Linux calculate the total block count when running /bin/ls -l?Escaping double quotes for variables in bash and qmake






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









3















My shell engine is either Busybox 1.31.0 or bash 3.2



I need to get the size of the files retrieved from find command.



I've been trying to find only files, which have been modified more than 60 days ago and at the same time get the size of all those files SUMARIZED preferably in a one-liner in MB notation. Here's what I've tried.



find -type f -mtime +60 -print0 | xargs -0 du -smc


and



find -type f -mtime +60 -exec du -smc ;


The former retrieves line-by-line all files older than 60 days (no problem at all until here) but it weirdly calculates the size several times between all those lines and at the final line I get a "total" size that does not correspond to the actual total size of the output. Here's what it looks like.



.....
.....
0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
3275 total
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
12 total


The latter's output calculate the size of every coinciding file line-by-line with no total.



What I'm expecting is:



 0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
54 total


Or simply just the real size result without all the lines



54 total


Any help would be well received.










share|improve this question









New contributor



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





















  • Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

    – terdon
    8 hours ago












  • With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

    – mosvy
    7 hours ago


















3















My shell engine is either Busybox 1.31.0 or bash 3.2



I need to get the size of the files retrieved from find command.



I've been trying to find only files, which have been modified more than 60 days ago and at the same time get the size of all those files SUMARIZED preferably in a one-liner in MB notation. Here's what I've tried.



find -type f -mtime +60 -print0 | xargs -0 du -smc


and



find -type f -mtime +60 -exec du -smc ;


The former retrieves line-by-line all files older than 60 days (no problem at all until here) but it weirdly calculates the size several times between all those lines and at the final line I get a "total" size that does not correspond to the actual total size of the output. Here's what it looks like.



.....
.....
0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
3275 total
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
12 total


The latter's output calculate the size of every coinciding file line-by-line with no total.



What I'm expecting is:



 0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
54 total


Or simply just the real size result without all the lines



54 total


Any help would be well received.










share|improve this question









New contributor



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





















  • Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

    – terdon
    8 hours ago












  • With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

    – mosvy
    7 hours ago














3












3








3








My shell engine is either Busybox 1.31.0 or bash 3.2



I need to get the size of the files retrieved from find command.



I've been trying to find only files, which have been modified more than 60 days ago and at the same time get the size of all those files SUMARIZED preferably in a one-liner in MB notation. Here's what I've tried.



find -type f -mtime +60 -print0 | xargs -0 du -smc


and



find -type f -mtime +60 -exec du -smc ;


The former retrieves line-by-line all files older than 60 days (no problem at all until here) but it weirdly calculates the size several times between all those lines and at the final line I get a "total" size that does not correspond to the actual total size of the output. Here's what it looks like.



.....
.....
0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
3275 total
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
12 total


The latter's output calculate the size of every coinciding file line-by-line with no total.



What I'm expecting is:



 0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
54 total


Or simply just the real size result without all the lines



54 total


Any help would be well received.










share|improve this question









New contributor



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











My shell engine is either Busybox 1.31.0 or bash 3.2



I need to get the size of the files retrieved from find command.



I've been trying to find only files, which have been modified more than 60 days ago and at the same time get the size of all those files SUMARIZED preferably in a one-liner in MB notation. Here's what I've tried.



find -type f -mtime +60 -print0 | xargs -0 du -smc


and



find -type f -mtime +60 -exec du -smc ;


The former retrieves line-by-line all files older than 60 days (no problem at all until here) but it weirdly calculates the size several times between all those lines and at the final line I get a "total" size that does not correspond to the actual total size of the output. Here's what it looks like.



.....
.....
0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
3275 total
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
12 total


The latter's output calculate the size of every coinciding file line-by-line with no total.



What I'm expecting is:



 0 ./FOLDER 2018/Copy #183 of ~$DATABASE OTHERS - NOV.18N.xlsx
42 ./FOLDER 2018/F9C8A618.tmp
0 ./FOLDER 2018/Copy #166 of ~$DATABASE PORTFOLIO NOV.18.xlsx
10 ./FOLDER 2018/CFDC6981.tmp
2 ./FOLDER 2018/D5AAF4EB.tmp
0 ./LIFE INSURANCE/Copy #15 of ~$Copy of LIFE INSURANCE CLIENTS.xlsx
54 total


Or simply just the real size result without all the lines



54 total


Any help would be well received.







bash command-line find disk-usage busybox






share|improve this question









New contributor



aldehc99 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 question









New contributor



aldehc99 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 question




share|improve this question








edited 7 hours ago









Jeff Schaller

50.1k11 gold badges74 silver badges166 bronze badges




50.1k11 gold badges74 silver badges166 bronze badges






New contributor



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








asked 8 hours ago









aldehc99aldehc99

184 bronze badges




184 bronze badges




New contributor



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




New contributor




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

















  • Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

    – terdon
    8 hours ago












  • With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

    – mosvy
    7 hours ago


















  • Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

    – terdon
    8 hours ago












  • With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

    – mosvy
    7 hours ago

















Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

– terdon
8 hours ago






Do you want the size of the output of find, or the size of the files returned by find? Your commands are doing the second, but your question asks for the first. Also, what find is this? Do you have GNU tools? You mention busybox, so I guess this isn't a full GNU/Linux system?

– terdon
8 hours ago














With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

– mosvy
7 hours ago






With GNU find you can use -printf %b and get rid of du entirely: find . -mtime +60 -type f -printf '%bn' | awk 's+=$1ENDprint s/2048"M"'. With busybox find and du, you should use du -smc $(find ...) and hope for the best ;-) Whether your shell is bash or busybox matters very little.

– mosvy
7 hours ago











2 Answers
2






active

oldest

votes


















2
















Try pipe the output of find to du and specify the --files0-from - flag:



find -type f -mtime +60 -print0 | du -shc --files0-from -


This should give you a grand total at the end



To get just the total, pipe that output to tail -n1:



find -type f -mtime +60 -print0 | du -shc --files0-from - | tail -n1



I should mention that I actually just tested this with gnu linux, not busybox. Looking at the busybox page, it does not look like du supports the --files0-from option.



You can change the above command to this to have it work on busybox:



du -ch $(find -type f -mtime +60) | tail -n1





share|improve this answer






















  • 1





    Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

    – aldehc99
    4 hours ago











  • @aldehc99 glad to hear it. What was the error you get with the last command?

    – smac89
    4 hours ago











  • I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

    – aldehc99
    4 hours ago



















2
















In principle this is easy: just tell find to run du on a bunch of files at once.
find . -type f -mtime +60 -exec du -smc +

Unfortunately this doesn't work reliably, because -exec … + can execute the command multiple times, it only tries to group the arguments, and it cannot possibly group all the arguments if their total length would go over the system's command line length limit. And in fact BusyBox find (at least the version I tested just now) doesn't try grouping at all: -exec … + processes one argument at a time, line -exec … ;. There's no way to be sure to get a single total line.



GNU du can be told to read an arbitrarily long list of file names with --files0-from, but other versions of du, in particular the one in BusyBox, can only take file names from the command line.



So if you can't assume that you have GNU du, there's no way to avoid running du multiple times, and this means you need another tool to do the summing, which in turn requires that du doesn't round the sizes. The summing is simple with awk if the output of du is parseable.



If you can assume that there are no newlines in file names, or you're ok with excluding paths that contain newlines, the output of du is easy to parse: just one file per line.



newline='
'
find . ! -path "*$newline*" -type f -mtime +60 -exec du -k + |
awk 'kB += $1 END printf "%d MBn", (kB + 512) / 1024'





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/4.0/"u003ecc by-sa 4.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
    );



    );







    aldehc99 is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f543676%2fget-the-exact-size-of-files-retrieved-by-find-output%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2
















    Try pipe the output of find to du and specify the --files0-from - flag:



    find -type f -mtime +60 -print0 | du -shc --files0-from -


    This should give you a grand total at the end



    To get just the total, pipe that output to tail -n1:



    find -type f -mtime +60 -print0 | du -shc --files0-from - | tail -n1



    I should mention that I actually just tested this with gnu linux, not busybox. Looking at the busybox page, it does not look like du supports the --files0-from option.



    You can change the above command to this to have it work on busybox:



    du -ch $(find -type f -mtime +60) | tail -n1





    share|improve this answer






















    • 1





      Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

      – aldehc99
      4 hours ago











    • @aldehc99 glad to hear it. What was the error you get with the last command?

      – smac89
      4 hours ago











    • I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

      – aldehc99
      4 hours ago
















    2
















    Try pipe the output of find to du and specify the --files0-from - flag:



    find -type f -mtime +60 -print0 | du -shc --files0-from -


    This should give you a grand total at the end



    To get just the total, pipe that output to tail -n1:



    find -type f -mtime +60 -print0 | du -shc --files0-from - | tail -n1



    I should mention that I actually just tested this with gnu linux, not busybox. Looking at the busybox page, it does not look like du supports the --files0-from option.



    You can change the above command to this to have it work on busybox:



    du -ch $(find -type f -mtime +60) | tail -n1





    share|improve this answer






















    • 1





      Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

      – aldehc99
      4 hours ago











    • @aldehc99 glad to hear it. What was the error you get with the last command?

      – smac89
      4 hours ago











    • I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

      – aldehc99
      4 hours ago














    2














    2










    2









    Try pipe the output of find to du and specify the --files0-from - flag:



    find -type f -mtime +60 -print0 | du -shc --files0-from -


    This should give you a grand total at the end



    To get just the total, pipe that output to tail -n1:



    find -type f -mtime +60 -print0 | du -shc --files0-from - | tail -n1



    I should mention that I actually just tested this with gnu linux, not busybox. Looking at the busybox page, it does not look like du supports the --files0-from option.



    You can change the above command to this to have it work on busybox:



    du -ch $(find -type f -mtime +60) | tail -n1





    share|improve this answer















    Try pipe the output of find to du and specify the --files0-from - flag:



    find -type f -mtime +60 -print0 | du -shc --files0-from -


    This should give you a grand total at the end



    To get just the total, pipe that output to tail -n1:



    find -type f -mtime +60 -print0 | du -shc --files0-from - | tail -n1



    I should mention that I actually just tested this with gnu linux, not busybox. Looking at the busybox page, it does not look like du supports the --files0-from option.



    You can change the above command to this to have it work on busybox:



    du -ch $(find -type f -mtime +60) | tail -n1






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 hours ago

























    answered 8 hours ago









    smac89smac89

    2821 gold badge2 silver badges10 bronze badges




    2821 gold badge2 silver badges10 bronze badges










    • 1





      Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

      – aldehc99
      4 hours ago











    • @aldehc99 glad to hear it. What was the error you get with the last command?

      – smac89
      4 hours ago











    • I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

      – aldehc99
      4 hours ago













    • 1





      Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

      – aldehc99
      4 hours ago











    • @aldehc99 glad to hear it. What was the error you get with the last command?

      – smac89
      4 hours ago











    • I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

      – aldehc99
      4 hours ago








    1




    1





    Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

    – aldehc99
    4 hours ago





    Great contribution @smac89 Your first two commands worked just fine. They piped correctly the output and executed what I needed. You're right, in the Busybox page there is not support for --files0-from in du but they got the job done. Contrary to this, the latter command didn't work, it printed some du:invalid option exceptions.

    – aldehc99
    4 hours ago













    @aldehc99 glad to hear it. What was the error you get with the last command?

    – smac89
    4 hours ago





    @aldehc99 glad to hear it. What was the error you get with the last command?

    – smac89
    4 hours ago













    I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

    – aldehc99
    4 hours ago






    I have tested it with several files and realized it cannot handle filenames containing spaces. It seems to split said filenames, making the other part of them look like other commands: du: cannot access `./TESTINGFILE': No such file or directory When the filename does not contain spaces it works as expected.

    – aldehc99
    4 hours ago














    2
















    In principle this is easy: just tell find to run du on a bunch of files at once.
    find . -type f -mtime +60 -exec du -smc +

    Unfortunately this doesn't work reliably, because -exec … + can execute the command multiple times, it only tries to group the arguments, and it cannot possibly group all the arguments if their total length would go over the system's command line length limit. And in fact BusyBox find (at least the version I tested just now) doesn't try grouping at all: -exec … + processes one argument at a time, line -exec … ;. There's no way to be sure to get a single total line.



    GNU du can be told to read an arbitrarily long list of file names with --files0-from, but other versions of du, in particular the one in BusyBox, can only take file names from the command line.



    So if you can't assume that you have GNU du, there's no way to avoid running du multiple times, and this means you need another tool to do the summing, which in turn requires that du doesn't round the sizes. The summing is simple with awk if the output of du is parseable.



    If you can assume that there are no newlines in file names, or you're ok with excluding paths that contain newlines, the output of du is easy to parse: just one file per line.



    newline='
    '
    find . ! -path "*$newline*" -type f -mtime +60 -exec du -k + |
    awk 'kB += $1 END printf "%d MBn", (kB + 512) / 1024'





    share|improve this answer





























      2
















      In principle this is easy: just tell find to run du on a bunch of files at once.
      find . -type f -mtime +60 -exec du -smc +

      Unfortunately this doesn't work reliably, because -exec … + can execute the command multiple times, it only tries to group the arguments, and it cannot possibly group all the arguments if their total length would go over the system's command line length limit. And in fact BusyBox find (at least the version I tested just now) doesn't try grouping at all: -exec … + processes one argument at a time, line -exec … ;. There's no way to be sure to get a single total line.



      GNU du can be told to read an arbitrarily long list of file names with --files0-from, but other versions of du, in particular the one in BusyBox, can only take file names from the command line.



      So if you can't assume that you have GNU du, there's no way to avoid running du multiple times, and this means you need another tool to do the summing, which in turn requires that du doesn't round the sizes. The summing is simple with awk if the output of du is parseable.



      If you can assume that there are no newlines in file names, or you're ok with excluding paths that contain newlines, the output of du is easy to parse: just one file per line.



      newline='
      '
      find . ! -path "*$newline*" -type f -mtime +60 -exec du -k + |
      awk 'kB += $1 END printf "%d MBn", (kB + 512) / 1024'





      share|improve this answer



























        2














        2










        2









        In principle this is easy: just tell find to run du on a bunch of files at once.
        find . -type f -mtime +60 -exec du -smc +

        Unfortunately this doesn't work reliably, because -exec … + can execute the command multiple times, it only tries to group the arguments, and it cannot possibly group all the arguments if their total length would go over the system's command line length limit. And in fact BusyBox find (at least the version I tested just now) doesn't try grouping at all: -exec … + processes one argument at a time, line -exec … ;. There's no way to be sure to get a single total line.



        GNU du can be told to read an arbitrarily long list of file names with --files0-from, but other versions of du, in particular the one in BusyBox, can only take file names from the command line.



        So if you can't assume that you have GNU du, there's no way to avoid running du multiple times, and this means you need another tool to do the summing, which in turn requires that du doesn't round the sizes. The summing is simple with awk if the output of du is parseable.



        If you can assume that there are no newlines in file names, or you're ok with excluding paths that contain newlines, the output of du is easy to parse: just one file per line.



        newline='
        '
        find . ! -path "*$newline*" -type f -mtime +60 -exec du -k + |
        awk 'kB += $1 END printf "%d MBn", (kB + 512) / 1024'





        share|improve this answer













        In principle this is easy: just tell find to run du on a bunch of files at once.
        find . -type f -mtime +60 -exec du -smc +

        Unfortunately this doesn't work reliably, because -exec … + can execute the command multiple times, it only tries to group the arguments, and it cannot possibly group all the arguments if their total length would go over the system's command line length limit. And in fact BusyBox find (at least the version I tested just now) doesn't try grouping at all: -exec … + processes one argument at a time, line -exec … ;. There's no way to be sure to get a single total line.



        GNU du can be told to read an arbitrarily long list of file names with --files0-from, but other versions of du, in particular the one in BusyBox, can only take file names from the command line.



        So if you can't assume that you have GNU du, there's no way to avoid running du multiple times, and this means you need another tool to do the summing, which in turn requires that du doesn't round the sizes. The summing is simple with awk if the output of du is parseable.



        If you can assume that there are no newlines in file names, or you're ok with excluding paths that contain newlines, the output of du is easy to parse: just one file per line.



        newline='
        '
        find . ! -path "*$newline*" -type f -mtime +60 -exec du -k + |
        awk 'kB += $1 END printf "%d MBn", (kB + 512) / 1024'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        GillesGilles

        577k140 gold badges1190 silver badges1702 bronze badges




        577k140 gold badges1190 silver badges1702 bronze badges
























            aldehc99 is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded

















            aldehc99 is a new contributor. Be nice, and check out our Code of Conduct.












            aldehc99 is a new contributor. Be nice, and check out our Code of Conduct.











            aldehc99 is a new contributor. Be nice, and check out our Code of Conduct.














            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%2f543676%2fget-the-exact-size-of-files-retrieved-by-find-output%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

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單