Printing the sum-size of copied files in bash
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done
This bash
prints every second the size of the file being copied.
For example, in second 1
it copies file A
and prints the size of file A
. In second 2
it copies file B
and prints the size of file B
instead of the total size of files A+B
. In second 3
it copies the file C
and prints the size of file C
instead of the total size of files A+B+C
How can I do it in bash
specifically?
bash
add a comment |Â
up vote
6
down vote
favorite
I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done
This bash
prints every second the size of the file being copied.
For example, in second 1
it copies file A
and prints the size of file A
. In second 2
it copies file B
and prints the size of file B
instead of the total size of files A+B
. In second 3
it copies the file C
and prints the size of file C
instead of the total size of files A+B+C
How can I do it in bash
specifically?
bash
1
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
@ RalfFriedl. Thanks! probably just sleep0.1
?
– Goro
Sep 9 at 18:21
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done
This bash
prints every second the size of the file being copied.
For example, in second 1
it copies file A
and prints the size of file A
. In second 2
it copies file B
and prints the size of file B
instead of the total size of files A+B
. In second 3
it copies the file C
and prints the size of file C
instead of the total size of files A+B+C
How can I do it in bash
specifically?
bash
I would like from the code below to print the sum-size of the files being copied instead of the size of each copied file separately.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
for i in *.tiff; do
size=$(wc -c < $i)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
done
This bash
prints every second the size of the file being copied.
For example, in second 1
it copies file A
and prints the size of file A
. In second 2
it copies file B
and prints the size of file B
instead of the total size of files A+B
. In second 3
it copies the file C
and prints the size of file C
instead of the total size of files A+B+C
How can I do it in bash
specifically?
bash
bash
edited Sep 9 at 16:11
asked Sep 9 at 16:07
TNT
308111
308111
1
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
@ RalfFriedl. Thanks! probably just sleep0.1
?
– Goro
Sep 9 at 18:21
add a comment |Â
1
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
@ RalfFriedl. Thanks! probably just sleep0.1
?
– Goro
Sep 9 at 18:21
1
1
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
@ RalfFriedl. Thanks! probably just sleep
0.1
?– Goro
Sep 9 at 18:21
@ RalfFriedl. Thanks! probably just sleep
0.1
?– Goro
Sep 9 at 18:21
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
5
down vote
Just sum up the single sizes and echo
it afterwards.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done
echo $size_sum
add a comment |Â
up vote
1
down vote
size=$(wc -c < $i)
This isn't a very smart way to get the size of a file. Running wc
like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.
This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.
With GNU utilities, you can get the size of the file with stat -c %s "$filename"
. Sadly, the options stat
takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
Here, the pipeline seems odd. cp
doesn't produce any output (to standard output), at least not without -i
, so there's nothing to pipe. Even if it did, sleep
doesn't read anything so any output from cp
would be wasted. The same thing happens between sleep
and echo
. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.
If you want to sum the file sizes, the shell's arithmetic expansion $(( .. ))
is what you probably want. So, without any extra sleeps:
dest=/path/to/destination
total=0
for file in *.tiff; do
size=$(stat -c %s "$file");
printf "current %d total %dn" "$size" "$((total += size))"
cp -- "$file" "$dest"
done
add a comment |Â
up vote
0
down vote
To save on the arithmetic in your script, you can just take the size of the concatenation of the files:
cat *.tiff | wc
The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think this should bewc -c
.
– user1934428
2 days ago
add a comment |Â
up vote
0
down vote
It is possible to sum with +=
if the variable is declared as integer:
$ declare -i size
$ size+=100; echo "$size"
100
$ size+=200; echo "$size"
300
Your script (with variable expansions correctly quoted):
#!/bin/bash
input_folder=./a/b/c
output_folder=./d/e/f
declare -i size
for i in "$input_folder"/*; do
size+=$(wc -c < "$i")
cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
done
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Just sum up the single sizes and echo
it afterwards.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done
echo $size_sum
add a comment |Â
up vote
5
down vote
Just sum up the single sizes and echo
it afterwards.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done
echo $size_sum
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Just sum up the single sizes and echo
it afterwards.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done
echo $size_sum
Just sum up the single sizes and echo
it afterwards.
#!/bin/bash
input_folder=/a/b/c
output_folder=/d/e/f
cd $input_folder
size_sum=0
for i in *.tiff; do
size=$(wc -c < $i)
size_sum=$((size_sum + size))
cp -v $i $out | sleep 1
done
echo $size_sum
answered Sep 9 at 16:23
Thomas
3,62141124
3,62141124
add a comment |Â
add a comment |Â
up vote
1
down vote
size=$(wc -c < $i)
This isn't a very smart way to get the size of a file. Running wc
like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.
This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.
With GNU utilities, you can get the size of the file with stat -c %s "$filename"
. Sadly, the options stat
takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
Here, the pipeline seems odd. cp
doesn't produce any output (to standard output), at least not without -i
, so there's nothing to pipe. Even if it did, sleep
doesn't read anything so any output from cp
would be wasted. The same thing happens between sleep
and echo
. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.
If you want to sum the file sizes, the shell's arithmetic expansion $(( .. ))
is what you probably want. So, without any extra sleeps:
dest=/path/to/destination
total=0
for file in *.tiff; do
size=$(stat -c %s "$file");
printf "current %d total %dn" "$size" "$((total += size))"
cp -- "$file" "$dest"
done
add a comment |Â
up vote
1
down vote
size=$(wc -c < $i)
This isn't a very smart way to get the size of a file. Running wc
like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.
This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.
With GNU utilities, you can get the size of the file with stat -c %s "$filename"
. Sadly, the options stat
takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
Here, the pipeline seems odd. cp
doesn't produce any output (to standard output), at least not without -i
, so there's nothing to pipe. Even if it did, sleep
doesn't read anything so any output from cp
would be wasted. The same thing happens between sleep
and echo
. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.
If you want to sum the file sizes, the shell's arithmetic expansion $(( .. ))
is what you probably want. So, without any extra sleeps:
dest=/path/to/destination
total=0
for file in *.tiff; do
size=$(stat -c %s "$file");
printf "current %d total %dn" "$size" "$((total += size))"
cp -- "$file" "$dest"
done
add a comment |Â
up vote
1
down vote
up vote
1
down vote
size=$(wc -c < $i)
This isn't a very smart way to get the size of a file. Running wc
like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.
This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.
With GNU utilities, you can get the size of the file with stat -c %s "$filename"
. Sadly, the options stat
takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
Here, the pipeline seems odd. cp
doesn't produce any output (to standard output), at least not without -i
, so there's nothing to pipe. Even if it did, sleep
doesn't read anything so any output from cp
would be wasted. The same thing happens between sleep
and echo
. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.
If you want to sum the file sizes, the shell's arithmetic expansion $(( .. ))
is what you probably want. So, without any extra sleeps:
dest=/path/to/destination
total=0
for file in *.tiff; do
size=$(stat -c %s "$file");
printf "current %d total %dn" "$size" "$((total += size))"
cp -- "$file" "$dest"
done
size=$(wc -c < $i)
This isn't a very smart way to get the size of a file. Running wc
like that will involve reading the whole file, which really is completely unnecessary since all you want is the size which the filesystem already knows and can tell you.
This is mitigated by the fact that you copy the file afterwards, and so need to read it in any case. A moderately-sized file will probably stay in the cache for the duration, so the reads don't actually hit the disk. But still, it would seem sensible to just read the size.
With GNU utilities, you can get the size of the file with stat -c %s "$filename"
. Sadly, the options stat
takes are different on other systems, and I don't think there's a better, more portable way (apart from running Perl)
cp -v $i $out | sleep 1 | echo -ne "$size%33[0Kr"
Here, the pipeline seems odd. cp
doesn't produce any output (to standard output), at least not without -i
, so there's nothing to pipe. Even if it did, sleep
doesn't read anything so any output from cp
would be wasted. The same thing happens between sleep
and echo
. This is basically the total opposite of what pipelines are usually used for, but it does have the effect that all the commands run in parallel, and the shell waits for all of them to complete, so that pipeline takes at least one second to run, regardless of how fast the copy would otherwise be.
If you want to sum the file sizes, the shell's arithmetic expansion $(( .. ))
is what you probably want. So, without any extra sleeps:
dest=/path/to/destination
total=0
for file in *.tiff; do
size=$(stat -c %s "$file");
printf "current %d total %dn" "$size" "$((total += size))"
cp -- "$file" "$dest"
done
answered 2 days ago


ilkkachu
50.8k678140
50.8k678140
add a comment |Â
add a comment |Â
up vote
0
down vote
To save on the arithmetic in your script, you can just take the size of the concatenation of the files:
cat *.tiff | wc
The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think this should bewc -c
.
– user1934428
2 days ago
add a comment |Â
up vote
0
down vote
To save on the arithmetic in your script, you can just take the size of the concatenation of the files:
cat *.tiff | wc
The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think this should bewc -c
.
– user1934428
2 days ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
To save on the arithmetic in your script, you can just take the size of the concatenation of the files:
cat *.tiff | wc
The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
To save on the arithmetic in your script, you can just take the size of the concatenation of the files:
cat *.tiff | wc
The UN*X kernel will conspire with you to make this just as efficient as taking the sizes of the files separately.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
muru
33.6k577144
33.6k577144
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
Rob Arthan
101
101
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Rob Arthan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think this should bewc -c
.
– user1934428
2 days ago
add a comment |Â
I think this should bewc -c
.
– user1934428
2 days ago
I think this should be
wc -c
.– user1934428
2 days ago
I think this should be
wc -c
.– user1934428
2 days ago
add a comment |Â
up vote
0
down vote
It is possible to sum with +=
if the variable is declared as integer:
$ declare -i size
$ size+=100; echo "$size"
100
$ size+=200; echo "$size"
300
Your script (with variable expansions correctly quoted):
#!/bin/bash
input_folder=./a/b/c
output_folder=./d/e/f
declare -i size
for i in "$input_folder"/*; do
size+=$(wc -c < "$i")
cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
done
add a comment |Â
up vote
0
down vote
It is possible to sum with +=
if the variable is declared as integer:
$ declare -i size
$ size+=100; echo "$size"
100
$ size+=200; echo "$size"
300
Your script (with variable expansions correctly quoted):
#!/bin/bash
input_folder=./a/b/c
output_folder=./d/e/f
declare -i size
for i in "$input_folder"/*; do
size+=$(wc -c < "$i")
cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
It is possible to sum with +=
if the variable is declared as integer:
$ declare -i size
$ size+=100; echo "$size"
100
$ size+=200; echo "$size"
300
Your script (with variable expansions correctly quoted):
#!/bin/bash
input_folder=./a/b/c
output_folder=./d/e/f
declare -i size
for i in "$input_folder"/*; do
size+=$(wc -c < "$i")
cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
done
It is possible to sum with +=
if the variable is declared as integer:
$ declare -i size
$ size+=100; echo "$size"
100
$ size+=200; echo "$size"
300
Your script (with variable expansions correctly quoted):
#!/bin/bash
input_folder=./a/b/c
output_folder=./d/e/f
declare -i size
for i in "$input_folder"/*; do
size+=$(wc -c < "$i")
cp -v "$i" "$output_folder" | sleep 1 | echo -ne "$size33[0Kr"
done
answered 2 days ago


Isaac
6,9221834
6,9221834
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467876%2fprinting-the-sum-size-of-copied-files-in-bash%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
You should note that it doesn't print the size every second, it sleeps one second between echo file. If it takes long enough that you want to display some progress, you probably don't want an extra delay.
– RalfFriedl
Sep 9 at 16:31
@ RalfFriedl. Thanks! probably just sleep
0.1
?– Goro
Sep 9 at 18:21