sum all numbers from “du”
Clash Royale CLAN TAG#URR8PPP
We want to calculate the first numbers that we get from du
du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn
so final sum will be
sum=6+668669+6+42456+32786+6+32786+262244
echo $sum
How we can do it by awk or perl one liners?
linux shell-script awk perl disk-usage
add a comment |
We want to calculate the first numbers that we get from du
du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn
so final sum will be
sum=6+668669+6+42456+32786+6+32786+262244
echo $sum
How we can do it by awk or perl one liners?
linux shell-script awk perl disk-usage
du -bs /tmp
would get you the answer too
– roaima
Feb 28 at 17:55
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59
add a comment |
We want to calculate the first numbers that we get from du
du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn
so final sum will be
sum=6+668669+6+42456+32786+6+32786+262244
echo $sum
How we can do it by awk or perl one liners?
linux shell-script awk perl disk-usage
We want to calculate the first numbers that we get from du
du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn
so final sum will be
sum=6+668669+6+42456+32786+6+32786+262244
echo $sum
How we can do it by awk or perl one liners?
linux shell-script awk perl disk-usage
linux shell-script awk perl disk-usage
edited Mar 8 at 17:33
Jeff Schaller♦
44k1161142
44k1161142
asked Feb 28 at 17:51
yaelyael
2,78132777
2,78132777
du -bs /tmp
would get you the answer too
– roaima
Feb 28 at 17:55
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59
add a comment |
du -bs /tmp
would get you the answer too
– roaima
Feb 28 at 17:55
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59
du -bs /tmp
would get you the answer too– roaima
Feb 28 at 17:55
du -bs /tmp
would get you the answer too– roaima
Feb 28 at 17:55
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59
add a comment |
3 Answers
3
active
oldest
votes
In AWK:
sum += $1
END print sum
So
du -b /tmp/* | awk ' sum += $1 END print sum '
Note that the result won’t be correct if the directories under /tmp
have subdirectories themselves, because du
produces running totals on directories and their children.
du -s
will calculate the sum for you correctly (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
and du -c
will calculate the sum of the listed directories and files, correctly too:
du -cb /tmp/*
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'
If you are not using wildcard, if you are using directory name like /tmp
, then you need to avoid the last entry because output of du -b /tmp
is like:
size1 file1
size2 file2
size_total .
So now you should avoid this last entry, so use:
du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'
However you can also use -s
option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:
du -s directory
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
add a comment |
You can also produce a total sum of selected files with du -c
. This works even if an argument of du
is not a directory, what is not the case of du -s
:
$ du -sb file1 file2
17 file1
18 file2
$ du -cb file1 file2
17 file1
18 file2
35 total
BTW, for interactive use I recommend adding -h
option instead of -b
or any other multiplier of block-size. This will print the size in human readable unit format.
$ du -ch file1 file2
4.0K file1
4.0K file2
8.0K total
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503601%2fsum-all-numbers-from-du%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In AWK:
sum += $1
END print sum
So
du -b /tmp/* | awk ' sum += $1 END print sum '
Note that the result won’t be correct if the directories under /tmp
have subdirectories themselves, because du
produces running totals on directories and their children.
du -s
will calculate the sum for you correctly (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
and du -c
will calculate the sum of the listed directories and files, correctly too:
du -cb /tmp/*
add a comment |
In AWK:
sum += $1
END print sum
So
du -b /tmp/* | awk ' sum += $1 END print sum '
Note that the result won’t be correct if the directories under /tmp
have subdirectories themselves, because du
produces running totals on directories and their children.
du -s
will calculate the sum for you correctly (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
and du -c
will calculate the sum of the listed directories and files, correctly too:
du -cb /tmp/*
add a comment |
In AWK:
sum += $1
END print sum
So
du -b /tmp/* | awk ' sum += $1 END print sum '
Note that the result won’t be correct if the directories under /tmp
have subdirectories themselves, because du
produces running totals on directories and their children.
du -s
will calculate the sum for you correctly (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
and du -c
will calculate the sum of the listed directories and files, correctly too:
du -cb /tmp/*
In AWK:
sum += $1
END print sum
So
du -b /tmp/* | awk ' sum += $1 END print sum '
Note that the result won’t be correct if the directories under /tmp
have subdirectories themselves, because du
produces running totals on directories and their children.
du -s
will calculate the sum for you correctly (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
and du -c
will calculate the sum of the listed directories and files, correctly too:
du -cb /tmp/*
edited Mar 8 at 19:23
Matthew
4162616
4162616
answered Feb 28 at 17:55
Stephen KittStephen Kitt
178k24405481
178k24405481
add a comment |
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'
If you are not using wildcard, if you are using directory name like /tmp
, then you need to avoid the last entry because output of du -b /tmp
is like:
size1 file1
size2 file2
size_total .
So now you should avoid this last entry, so use:
du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'
However you can also use -s
option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:
du -s directory
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'
If you are not using wildcard, if you are using directory name like /tmp
, then you need to avoid the last entry because output of du -b /tmp
is like:
size1 file1
size2 file2
size_total .
So now you should avoid this last entry, so use:
du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'
However you can also use -s
option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:
du -s directory
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'
If you are not using wildcard, if you are using directory name like /tmp
, then you need to avoid the last entry because output of du -b /tmp
is like:
size1 file1
size2 file2
size_total .
So now you should avoid this last entry, so use:
du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'
However you can also use -s
option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:
du -s directory
It is simple you can use:
du -b /tmp/* | awk 'BEGINi=0 i=i+$1 ENDprint i'
If you are not using wildcard, if you are using directory name like /tmp
, then you need to avoid the last entry because output of du -b /tmp
is like:
size1 file1
size2 file2
size_total .
So now you should avoid this last entry, so use:
du -b /tmp | awk 'BEGINi=0 if( $2 != "." )i=i+$1 ENDprint i'
However you can also use -s
option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:
du -s directory
edited Feb 28 at 18:19
answered Feb 28 at 17:56
Prvt_YadvPrvt_Yadv
2,92531327
2,92531327
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
add a comment |
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
1
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller♦
Feb 28 at 17:57
add a comment |
You can also produce a total sum of selected files with du -c
. This works even if an argument of du
is not a directory, what is not the case of du -s
:
$ du -sb file1 file2
17 file1
18 file2
$ du -cb file1 file2
17 file1
18 file2
35 total
BTW, for interactive use I recommend adding -h
option instead of -b
or any other multiplier of block-size. This will print the size in human readable unit format.
$ du -ch file1 file2
4.0K file1
4.0K file2
8.0K total
add a comment |
You can also produce a total sum of selected files with du -c
. This works even if an argument of du
is not a directory, what is not the case of du -s
:
$ du -sb file1 file2
17 file1
18 file2
$ du -cb file1 file2
17 file1
18 file2
35 total
BTW, for interactive use I recommend adding -h
option instead of -b
or any other multiplier of block-size. This will print the size in human readable unit format.
$ du -ch file1 file2
4.0K file1
4.0K file2
8.0K total
add a comment |
You can also produce a total sum of selected files with du -c
. This works even if an argument of du
is not a directory, what is not the case of du -s
:
$ du -sb file1 file2
17 file1
18 file2
$ du -cb file1 file2
17 file1
18 file2
35 total
BTW, for interactive use I recommend adding -h
option instead of -b
or any other multiplier of block-size. This will print the size in human readable unit format.
$ du -ch file1 file2
4.0K file1
4.0K file2
8.0K total
You can also produce a total sum of selected files with du -c
. This works even if an argument of du
is not a directory, what is not the case of du -s
:
$ du -sb file1 file2
17 file1
18 file2
$ du -cb file1 file2
17 file1
18 file2
35 total
BTW, for interactive use I recommend adding -h
option instead of -b
or any other multiplier of block-size. This will print the size in human readable unit format.
$ du -ch file1 file2
4.0K file1
4.0K file2
8.0K total
answered Feb 28 at 18:28
jimmijjimmij
32.3k875109
32.3k875109
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503601%2fsum-all-numbers-from-du%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
du -bs /tmp
would get you the answer too– roaima
Feb 28 at 17:55
See How can I quickly sum all numbers in a file?
– glenn jackman
Feb 28 at 17:57
See also Is there a way to sum up the size of files listed?
– Jeff Schaller♦
Feb 28 at 17:59