awk + sum all numbersIs there a way to sum up the size of files listed?How can I quickly sum all numbers in a...
Who is this Ant Woman character in this image alongside the Wasp?
Porting Linux to another platform requirements
Can a hotel cancel a confirmed reservation?
Can we use the stored gravitational potential energy of a building to produce power?
Can I write a book of my D&D game?
Why exactly do action photographers need high fps burst cameras?
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
Eww, those bytes are gross
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Traveling through the asteriod belt?
Why would Pakistan closing its air space cancel flights not headed to Pakistan itself?
Can I string the D&D Starter Set campaign into another module, keeping the same characters?
How did Ancient Greek 'πυρ' become English 'fire?'
Costs/Income from potential craftsmen
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
How should I handle players who ignore the session zero agreement?
Cat is tipping over bed-side lamps during the night
Difference between i++ and (i)++ in C
In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?
Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?
Intern applicant asking for compensation equivalent to that of permanent employee
Graph with overlapping labels
finding the number of complex number
What are "industrial chops"?
awk + sum all numbers
Is there a way to sum up the size of files listed?How can I quickly sum all numbers in a file?AWK sum column in file specify as a argumentHow to sum many numbers inside 2D array using awkawk + count field separator in csv and print line numberHow to join duplicates and sum their numbers with awkawk to sum the numbers(floating) and group it on a unique keySumming rows in a new column using sed, awk and perl?Check which process is using most memory and summary total used memoryAWK how to count sumSum and count in for loop
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
15 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins 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 14 mins ago
Jeff Schaller
42.6k1159136
42.6k1159136
asked 19 mins ago
yaelyael
2,63022571
2,63022571
du -bs /tmp
would get you the answer too
– roaima
15 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins ago
add a comment |
du -bs /tmp
would get you the answer too
– roaima
15 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins ago
du -bs /tmp
would get you the answer too– roaima
15 mins ago
du -bs /tmp
would get you the answer too– roaima
15 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins ago
add a comment |
2 Answers
2
active
oldest
votes
In AWK:
{ sum += $1 }
END { print sum }
So
du -b /tmp/* | awk '{ sum += $1 } END { print sum }'
du -s
will also calculate the sum for you (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
I think output of du also contains entrysize .
, so we should avoid that.
– PRY
19 secs ago
add a comment |
It is simple you can use:
du | awk 'BEGIN{i=0} {i=i+$1} END{print i}'
But wait, it will not work because output of du
is like:
1 file
2 filee
2 .
So we need to avoid the last entry because it tells about total size of current directory, and the calculated sum is also equals to that, so instead of that use:
du | 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
13 mins ago
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
2 Answers
2
active
oldest
votes
2 Answers
2
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 }'
du -s
will also calculate the sum for you (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
I think output of du also contains entrysize .
, so we should avoid that.
– PRY
19 secs ago
add a comment |
In AWK:
{ sum += $1 }
END { print sum }
So
du -b /tmp/* | awk '{ sum += $1 } END { print sum }'
du -s
will also calculate the sum for you (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
I think output of du also contains entrysize .
, so we should avoid that.
– PRY
19 secs ago
add a comment |
In AWK:
{ sum += $1 }
END { print sum }
So
du -b /tmp/* | awk '{ sum += $1 } END { print sum }'
du -s
will also calculate the sum for you (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
In AWK:
{ sum += $1 }
END { print sum }
So
du -b /tmp/* | awk '{ sum += $1 } END { print sum }'
du -s
will also calculate the sum for you (on all subdirectories and files in /tmp
, including hidden ones):
du -sb /tmp
edited 10 mins ago
answered 15 mins ago
Stephen KittStephen Kitt
174k24397472
174k24397472
I think output of du also contains entrysize .
, so we should avoid that.
– PRY
19 secs ago
add a comment |
I think output of du also contains entrysize .
, so we should avoid that.
– PRY
19 secs ago
I think output of du also contains entry
size .
, so we should avoid that.– PRY
19 secs ago
I think output of du also contains entry
size .
, so we should avoid that.– PRY
19 secs ago
add a comment |
It is simple you can use:
du | awk 'BEGIN{i=0} {i=i+$1} END{print i}'
But wait, it will not work because output of du
is like:
1 file
2 filee
2 .
So we need to avoid the last entry because it tells about total size of current directory, and the calculated sum is also equals to that, so instead of that use:
du | 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
13 mins ago
add a comment |
It is simple you can use:
du | awk 'BEGIN{i=0} {i=i+$1} END{print i}'
But wait, it will not work because output of du
is like:
1 file
2 filee
2 .
So we need to avoid the last entry because it tells about total size of current directory, and the calculated sum is also equals to that, so instead of that use:
du | 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
13 mins ago
add a comment |
It is simple you can use:
du | awk 'BEGIN{i=0} {i=i+$1} END{print i}'
But wait, it will not work because output of du
is like:
1 file
2 filee
2 .
So we need to avoid the last entry because it tells about total size of current directory, and the calculated sum is also equals to that, so instead of that use:
du | 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 | awk 'BEGIN{i=0} {i=i+$1} END{print i}'
But wait, it will not work because output of du
is like:
1 file
2 filee
2 .
So we need to avoid the last entry because it tells about total size of current directory, and the calculated sum is also equals to that, so instead of that use:
du | 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 1 min ago
answered 15 mins ago
PRYPRY
2,57831026
2,57831026
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
13 mins ago
add a comment |
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
13 mins ago
1
1
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
13 mins ago
variables initialize to zero, if you'd like to golf some bytes off :)
– Jeff Schaller
13 mins ago
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
15 mins ago
See How can I quickly sum all numbers in a file?
– glenn jackman
13 mins ago
See also Is there a way to sum up the size of files listed?
– Jeff Schaller
12 mins ago