awk + sum all numbers
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
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago
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 9 hours ago
PRY
2,58831026
2,58831026
asked 9 hours ago
yaelyael
2,66422571
2,66422571
du -bs /tmp
would get you the answer too
– roaima
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago
add a comment |
du -bs /tmp
would get you the answer too
– roaima
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago
du -bs /tmp
would get you the answer too– roaima
9 hours ago
du -bs /tmp
would get you the answer too– roaima
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago
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, correctly too:
du -cb /tmp/*
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print 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 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print 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
9 hours ago
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%2fawk-sum-all-numbers%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, 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, 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, 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, correctly too:
du -cb /tmp/*
edited 7 hours ago
answered 9 hours ago
Stephen KittStephen Kitt
174k24398473
174k24398473
add a comment |
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print 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 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print 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
9 hours ago
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print 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 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print 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
9 hours ago
add a comment |
It is simple you can use:
du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print 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 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print 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 'BEGIN{i=0} {i=i+$1} END{print 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 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print 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 9 hours ago
answered 9 hours ago
PRYPRY
2,58831026
2,58831026
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
9 hours ago
add a comment |
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
9 hours ago
1
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
9 hours ago
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
9 hours ago
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 9 hours ago
jimmijjimmij
32k874108
32k874108
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%2fawk-sum-all-numbers%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
9 hours ago
See How can I quickly sum all numbers in a file?
– glenn jackman
9 hours ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
9 hours ago