How to I print out the byte size of each file in my bash script?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question









New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It should be byte=$(.... -- get rid of the spaces around the = sign.
    – Stephen Harris
    30 mins ago














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.










share|improve this question









New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It should be byte=$(.... -- get rid of the spaces around the = sign.
    – Stephen Harris
    30 mins ago












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.










share|improve this question









New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 27 mins ago









Jeff Schaller

33.6k851113




33.6k851113






New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 37 mins ago









Li Wang

111




111




New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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















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










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





share|improve this answer




















    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Li Wang is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 29 mins ago









        Stéphane Chazelas

        287k53530867




        287k53530867




















            Li Wang is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Confectionery