Adding size of files using shell script
up vote
2
down vote
favorite
I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:
$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112
shell-script shell
add a comment |
up vote
2
down vote
favorite
I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:
$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112
shell-script shell
5
you don't we simply usedu
?
– msp9011
yesterday
1
@msp9011,du
will calculate also subdirectories
– Romeo Ninov
yesterday
1
@RomeoNinov here we are checking only files...du -b /etc/*.conf
– msp9011
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:
$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112
shell-script shell
I want to add and echo the sum of several files using shell script. How do I start?
I have a list of them like that:
$ stat /etc/*.conf | grep Size | cut -f4 -d' '
123
456
789
101112
shell-script shell
shell-script shell
edited yesterday
muru
35.1k581154
35.1k581154
asked yesterday
C. Cristi
1577
1577
5
you don't we simply usedu
?
– msp9011
yesterday
1
@msp9011,du
will calculate also subdirectories
– Romeo Ninov
yesterday
1
@RomeoNinov here we are checking only files...du -b /etc/*.conf
– msp9011
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago
add a comment |
5
you don't we simply usedu
?
– msp9011
yesterday
1
@msp9011,du
will calculate also subdirectories
– Romeo Ninov
yesterday
1
@RomeoNinov here we are checking only files...du -b /etc/*.conf
– msp9011
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago
5
5
you don't we simply use
du
?– msp9011
yesterday
you don't we simply use
du
?– msp9011
yesterday
1
1
@msp9011,
du
will calculate also subdirectories– Romeo Ninov
yesterday
@msp9011,
du
will calculate also subdirectories– Romeo Ninov
yesterday
1
1
@RomeoNinov here we are checking only files...
du -b /etc/*.conf
– msp9011
yesterday
@RomeoNinov here we are checking only files...
du -b /etc/*.conf
– msp9011
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago
add a comment |
5 Answers
5
active
oldest
votes
up vote
2
down vote
accepted
You can do this …
total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done
Thanks! and if I want to output it in a file I do:echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?
– C. Cristi
yesterday
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
add a comment |
up vote
12
down vote
stat -c "%s" /etc/*.conf|paste -sd+|bc -l
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file:paste -sd$' ' inputfile
.
– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
add a comment |
up vote
11
down vote
Also something like can do the work (with awk
)
stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'
add a comment |
up vote
10
down vote
With bc
{ stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc
- The
stat
format adds a+
sign and a continuation character after each size - a
0
is appended at the end to close the dangling final+
add a comment |
up vote
7
down vote
The most straightforward way is to use du -bc
:
$ du -bc /etc/*.conf
5139 /etc/man_db.conf
393 /etc/nsswitch.conf
5532 total
If you need to extract only the number of bytes, pipe the output to awk
:
$ du -bc /etc/*.conf | awk 'END { print $1 }'
5532
New contributor
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.--apparent-size
option may be needed to use apparent sizes.
– Ruslan
yesterday
@Ruslan Theawk
line also strips off thetotal
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can do this …
total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done
Thanks! and if I want to output it in a file I do:echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?
– C. Cristi
yesterday
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
add a comment |
up vote
2
down vote
accepted
You can do this …
total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done
Thanks! and if I want to output it in a file I do:echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?
– C. Cristi
yesterday
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can do this …
total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done
You can do this …
total=0
for s in $(stat /etc/*.conf | grep Size | cut -f4 -d' '); do
total=$(expr $total + $s)
done
answered yesterday
Red Cricket
1,20431731
1,20431731
Thanks! and if I want to output it in a file I do:echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?
– C. Cristi
yesterday
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
add a comment |
Thanks! and if I want to output it in a file I do:echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?
– C. Cristi
yesterday
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
Thanks! and if I want to output it in a file I do:
echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?– C. Cristi
yesterday
Thanks! and if I want to output it in a file I do:
echo total > my_file.txt
, right? And what if I want to output the errors too what do I do then?– C. Cristi
yesterday
4
4
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
Don't use grep or cut on stat output. stat has format flags (%s) for this
– ohno
yesterday
5
5
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
Also, 'Size' is likely to assume an English or "C " locale.
– xenoid
yesterday
add a comment |
up vote
12
down vote
stat -c "%s" /etc/*.conf|paste -sd+|bc -l
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file:paste -sd$' ' inputfile
.
– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
add a comment |
up vote
12
down vote
stat -c "%s" /etc/*.conf|paste -sd+|bc -l
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file:paste -sd$' ' inputfile
.
– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
add a comment |
up vote
12
down vote
up vote
12
down vote
stat -c "%s" /etc/*.conf|paste -sd+|bc -l
stat -c "%s" /etc/*.conf|paste -sd+|bc -l
answered yesterday
Ipor Sircer
10.2k11024
10.2k11024
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file:paste -sd$' ' inputfile
.
– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
add a comment |
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file:paste -sd$' ' inputfile
.
– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)
paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile
.– TheDudeAbides
yesterday
Although effectively the same as @xenoid's solution, I prefer this because 1) less rigamarole with format strings and remembering to append the final "0"; and 2) while it costs a process, it hews closer to the "one thing well" philosophy. It's also a useful use of the (perhaps underappreciated)
paste
utility that can be applied to a larger class of problems: separating a bunch of stuff with a delimiter. Another example is "unwrapping" (removing the line breaks from) a text file: paste -sd$' ' inputfile
.– TheDudeAbides
yesterday
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
@TheDudeAbides, all those things are true about awk. And it use one process less. And IMO is much more readable
– Romeo Ninov
20 hours ago
add a comment |
up vote
11
down vote
Also something like can do the work (with awk
)
stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'
add a comment |
up vote
11
down vote
Also something like can do the work (with awk
)
stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'
add a comment |
up vote
11
down vote
up vote
11
down vote
Also something like can do the work (with awk
)
stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'
Also something like can do the work (with awk
)
stat -c "%s" /etc/*.conf|awk '{s+=$1} END {print s}'
answered yesterday
Romeo Ninov
4,92931627
4,92931627
add a comment |
add a comment |
up vote
10
down vote
With bc
{ stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc
- The
stat
format adds a+
sign and a continuation character after each size - a
0
is appended at the end to close the dangling final+
add a comment |
up vote
10
down vote
With bc
{ stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc
- The
stat
format adds a+
sign and a continuation character after each size - a
0
is appended at the end to close the dangling final+
add a comment |
up vote
10
down vote
up vote
10
down vote
With bc
{ stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc
- The
stat
format adds a+
sign and a continuation character after each size - a
0
is appended at the end to close the dangling final+
With bc
{ stat -c '%s+' /etc/*.conf ; echo 0 ; } | bc
- The
stat
format adds a+
sign and a continuation character after each size - a
0
is appended at the end to close the dangling final+
answered yesterday
xenoid
2,5881724
2,5881724
add a comment |
add a comment |
up vote
7
down vote
The most straightforward way is to use du -bc
:
$ du -bc /etc/*.conf
5139 /etc/man_db.conf
393 /etc/nsswitch.conf
5532 total
If you need to extract only the number of bytes, pipe the output to awk
:
$ du -bc /etc/*.conf | awk 'END { print $1 }'
5532
New contributor
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.--apparent-size
option may be needed to use apparent sizes.
– Ruslan
yesterday
@Ruslan Theawk
line also strips off thetotal
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
add a comment |
up vote
7
down vote
The most straightforward way is to use du -bc
:
$ du -bc /etc/*.conf
5139 /etc/man_db.conf
393 /etc/nsswitch.conf
5532 total
If you need to extract only the number of bytes, pipe the output to awk
:
$ du -bc /etc/*.conf | awk 'END { print $1 }'
5532
New contributor
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.--apparent-size
option may be needed to use apparent sizes.
– Ruslan
yesterday
@Ruslan Theawk
line also strips off thetotal
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
add a comment |
up vote
7
down vote
up vote
7
down vote
The most straightforward way is to use du -bc
:
$ du -bc /etc/*.conf
5139 /etc/man_db.conf
393 /etc/nsswitch.conf
5532 total
If you need to extract only the number of bytes, pipe the output to awk
:
$ du -bc /etc/*.conf | awk 'END { print $1 }'
5532
New contributor
The most straightforward way is to use du -bc
:
$ du -bc /etc/*.conf
5139 /etc/man_db.conf
393 /etc/nsswitch.conf
5532 total
If you need to extract only the number of bytes, pipe the output to awk
:
$ du -bc /etc/*.conf | awk 'END { print $1 }'
5532
New contributor
New contributor
answered yesterday
Martin Frodl
893
893
New contributor
New contributor
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.--apparent-size
option may be needed to use apparent sizes.
– Ruslan
yesterday
@Ruslan Theawk
line also strips off thetotal
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
add a comment |
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.--apparent-size
option may be needed to use apparent sizes.
– Ruslan
yesterday
@Ruslan Theawk
line also strips off thetotal
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Hope ... OP doesn't require the grand total size of all files...
– msp9011
yesterday
Also note that this gives disk usage, not apparent sizes of the files.
--apparent-size
option may be needed to use apparent sizes.– Ruslan
yesterday
Also note that this gives disk usage, not apparent sizes of the files.
--apparent-size
option may be needed to use apparent sizes.– Ruslan
yesterday
@Ruslan The
awk
line also strips off the total
– Izkata
yesterday
@Ruslan The
awk
line also strips off the total
– Izkata
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
@Izkata oh, indeed, didn't notice this bit.
– Ruslan
yesterday
add a comment |
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%2f483135%2fadding-size-of-files-using-shell-script%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
5
you don't we simply use
du
?– msp9011
yesterday
1
@msp9011,
du
will calculate also subdirectories– Romeo Ninov
yesterday
1
@RomeoNinov here we are checking only files...
du -b /etc/*.conf
– msp9011
yesterday
@msp9011 Not if there is a directory matching the pattern. It's unlikely but not impossible.
– BlackJack
yesterday
@msp9011 due to block sizes, etc, disk usage is not the same as total file size.
– OrangeDog
19 hours ago