How to I print out the byte size of each file in my bash script?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
My current code is like so:
scan.sh:
#!/bin/bash
while IFS= read -r line;
do
byte = $(stat -c%s "$line");
echo "$line : $byte";
done< <(ls *.$1)
The output would be like this:
./scan.sh cpp
./scan.sh: line 4: byte: command not found
arraysum.cpp :
./scan.sh: line 4: byte: command not found
countLines.cpp :
./scan.sh: line 4: byte: command not found
createtext.cpp :
./scan.sh: line 4: byte: command not found
multiproc1.cpp :
./scan.sh: line 4: byte: command not found
myWc.cpp :
./scan.sh: line 4: byte: command not found
test.cpp :
Basically my code will take 1 syntax and will search the directory based on that syntax, problem is I want it to print out "name of file" + "byte size of file" only I can't seem to get that working.
shell-script files size
New contributor
add a comment |Â
up vote
2
down vote
favorite
My current code is like so:
scan.sh:
#!/bin/bash
while IFS= read -r line;
do
byte = $(stat -c%s "$line");
echo "$line : $byte";
done< <(ls *.$1)
The output would be like this:
./scan.sh cpp
./scan.sh: line 4: byte: command not found
arraysum.cpp :
./scan.sh: line 4: byte: command not found
countLines.cpp :
./scan.sh: line 4: byte: command not found
createtext.cpp :
./scan.sh: line 4: byte: command not found
multiproc1.cpp :
./scan.sh: line 4: byte: command not found
myWc.cpp :
./scan.sh: line 4: byte: command not found
test.cpp :
Basically my code will take 1 syntax and will search the directory based on that syntax, problem is I want it to print out "name of file" + "byte size of file" only I can't seem to get that working.
shell-script files size
New contributor
It should bebyte=$(....
-- get rid of the spaces around the=
sign.
â Stephen Harris
30 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My current code is like so:
scan.sh:
#!/bin/bash
while IFS= read -r line;
do
byte = $(stat -c%s "$line");
echo "$line : $byte";
done< <(ls *.$1)
The output would be like this:
./scan.sh cpp
./scan.sh: line 4: byte: command not found
arraysum.cpp :
./scan.sh: line 4: byte: command not found
countLines.cpp :
./scan.sh: line 4: byte: command not found
createtext.cpp :
./scan.sh: line 4: byte: command not found
multiproc1.cpp :
./scan.sh: line 4: byte: command not found
myWc.cpp :
./scan.sh: line 4: byte: command not found
test.cpp :
Basically my code will take 1 syntax and will search the directory based on that syntax, problem is I want it to print out "name of file" + "byte size of file" only I can't seem to get that working.
shell-script files size
New contributor
My current code is like so:
scan.sh:
#!/bin/bash
while IFS= read -r line;
do
byte = $(stat -c%s "$line");
echo "$line : $byte";
done< <(ls *.$1)
The output would be like this:
./scan.sh cpp
./scan.sh: line 4: byte: command not found
arraysum.cpp :
./scan.sh: line 4: byte: command not found
countLines.cpp :
./scan.sh: line 4: byte: command not found
createtext.cpp :
./scan.sh: line 4: byte: command not found
multiproc1.cpp :
./scan.sh: line 4: byte: command not found
myWc.cpp :
./scan.sh: line 4: byte: command not found
test.cpp :
Basically my code will take 1 syntax and will search the directory based on that syntax, problem is I want it to print out "name of file" + "byte size of file" only I can't seem to get that working.
shell-script files size
shell-script files size
New contributor
New contributor
edited 27 mins ago
Jeff Schaller
33.6k851113
33.6k851113
New contributor
asked 37 mins ago
Li Wang
111
111
New contributor
New contributor
It should bebyte=$(....
-- get rid of the spaces around the=
sign.
â Stephen Harris
30 mins ago
add a comment |Â
It should bebyte=$(....
-- get rid of the spaces around the=
sign.
â Stephen Harris
30 mins ago
It should be
byte=$(....
-- get rid of the spaces around the =
sign.â Stephen Harris
30 mins ago
It should be
byte=$(....
-- get rid of the spaces around the =
sign.â Stephen Harris
30 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
In the syntax of Bourne-like shells like bash
, there must not be any space around the =
sign in assignments.
byte=value
Here though, parsing the output of ls
is a bad idea.
You can just write it:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
If you do need a loop, just write it:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Or if you have to use bash
:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
In the syntax of Bourne-like shells like bash
, there must not be any space around the =
sign in assignments.
byte=value
Here though, parsing the output of ls
is a bad idea.
You can just write it:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
If you do need a loop, just write it:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Or if you have to use bash
:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
add a comment |Â
up vote
3
down vote
In the syntax of Bourne-like shells like bash
, there must not be any space around the =
sign in assignments.
byte=value
Here though, parsing the output of ls
is a bad idea.
You can just write it:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
If you do need a loop, just write it:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Or if you have to use bash
:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
add a comment |Â
up vote
3
down vote
up vote
3
down vote
In the syntax of Bourne-like shells like bash
, there must not be any space around the =
sign in assignments.
byte=value
Here though, parsing the output of ls
is a bad idea.
You can just write it:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
If you do need a loop, just write it:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Or if you have to use bash
:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
In the syntax of Bourne-like shells like bash
, there must not be any space around the =
sign in assignments.
byte=value
Here though, parsing the output of ls
is a bad idea.
You can just write it:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
If you do need a loop, just write it:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Or if you have to use bash
:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
answered 29 mins ago
Stéphane Chazelas
287k53530867
287k53530867
add a comment |Â
add a comment |Â
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
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%2f473687%2fhow-to-i-print-out-the-byte-size-of-each-file-in-my-bash-script%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
It should be
byte=$(....
-- get rid of the spaces around the=
sign.â Stephen Harris
30 mins ago