Echo/print the files in a Directory, echo the files size and echo the qty of files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Please help. I am a student and I found this question online, trying to practice and learn.



I am creating a program that will echo each entry in a given directory. If the entry in the directory is a file, it will echo its size. If the entry is a directory it will echo how many items are in that directory. My program below will echo the files in the given directory. My question is how to echo the size of the files and echo how many items in the directory. My program below



Thank you!



#! /bin/bash
# Files Directory
read -p "Enter a Directory or File: " entry
target="$entry"
let count=0
for f in "$target"/*
do
echo $(basename $f)
let count=count+1
done
echo "Files in the Directory: $count"
exit 0






share|improve this question






















  • Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
    – glenn jackman
    Mar 14 at 16:07










  • You can find the size of a file with stat or `wc.
    – glenn jackman
    Mar 14 at 16:07










  • Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
    – glenn jackman
    Mar 14 at 16:09










  • There are several questions on this very site about counting files. Search.
    – glenn jackman
    Mar 14 at 16:11






  • 1




    Use shellcheck.net to check your code for errors and improvements.
    – glenn jackman
    Mar 14 at 16:11














up vote
0
down vote

favorite












Please help. I am a student and I found this question online, trying to practice and learn.



I am creating a program that will echo each entry in a given directory. If the entry in the directory is a file, it will echo its size. If the entry is a directory it will echo how many items are in that directory. My program below will echo the files in the given directory. My question is how to echo the size of the files and echo how many items in the directory. My program below



Thank you!



#! /bin/bash
# Files Directory
read -p "Enter a Directory or File: " entry
target="$entry"
let count=0
for f in "$target"/*
do
echo $(basename $f)
let count=count+1
done
echo "Files in the Directory: $count"
exit 0






share|improve this question






















  • Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
    – glenn jackman
    Mar 14 at 16:07










  • You can find the size of a file with stat or `wc.
    – glenn jackman
    Mar 14 at 16:07










  • Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
    – glenn jackman
    Mar 14 at 16:09










  • There are several questions on this very site about counting files. Search.
    – glenn jackman
    Mar 14 at 16:11






  • 1




    Use shellcheck.net to check your code for errors and improvements.
    – glenn jackman
    Mar 14 at 16:11












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Please help. I am a student and I found this question online, trying to practice and learn.



I am creating a program that will echo each entry in a given directory. If the entry in the directory is a file, it will echo its size. If the entry is a directory it will echo how many items are in that directory. My program below will echo the files in the given directory. My question is how to echo the size of the files and echo how many items in the directory. My program below



Thank you!



#! /bin/bash
# Files Directory
read -p "Enter a Directory or File: " entry
target="$entry"
let count=0
for f in "$target"/*
do
echo $(basename $f)
let count=count+1
done
echo "Files in the Directory: $count"
exit 0






share|improve this question














Please help. I am a student and I found this question online, trying to practice and learn.



I am creating a program that will echo each entry in a given directory. If the entry in the directory is a file, it will echo its size. If the entry is a directory it will echo how many items are in that directory. My program below will echo the files in the given directory. My question is how to echo the size of the files and echo how many items in the directory. My program below



Thank you!



#! /bin/bash
# Files Directory
read -p "Enter a Directory or File: " entry
target="$entry"
let count=0
for f in "$target"/*
do
echo $(basename $f)
let count=count+1
done
echo "Files in the Directory: $count"
exit 0








share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 15:06









Stéphane Chazelas

280k53515847




280k53515847










asked Mar 14 at 15:03









user280678

1




1











  • Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
    – glenn jackman
    Mar 14 at 16:07










  • You can find the size of a file with stat or `wc.
    – glenn jackman
    Mar 14 at 16:07










  • Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
    – glenn jackman
    Mar 14 at 16:09










  • There are several questions on this very site about counting files. Search.
    – glenn jackman
    Mar 14 at 16:11






  • 1




    Use shellcheck.net to check your code for errors and improvements.
    – glenn jackman
    Mar 14 at 16:11
















  • Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
    – glenn jackman
    Mar 14 at 16:07










  • You can find the size of a file with stat or `wc.
    – glenn jackman
    Mar 14 at 16:07










  • Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
    – glenn jackman
    Mar 14 at 16:09










  • There are several questions on this very site about counting files. Search.
    – glenn jackman
    Mar 14 at 16:11






  • 1




    Use shellcheck.net to check your code for errors and improvements.
    – glenn jackman
    Mar 14 at 16:11















Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
– glenn jackman
Mar 14 at 16:07




Have a look at Bash Conditional Expressions in the bash manual, particularly -f and -d.
– glenn jackman
Mar 14 at 16:07












You can find the size of a file with stat or `wc.
– glenn jackman
Mar 14 at 16:07




You can find the size of a file with stat or `wc.
– glenn jackman
Mar 14 at 16:07












Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
Mar 14 at 16:09




Quote your variables: Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
Mar 14 at 16:09












There are several questions on this very site about counting files. Search.
– glenn jackman
Mar 14 at 16:11




There are several questions on this very site about counting files. Search.
– glenn jackman
Mar 14 at 16:11




1




1




Use shellcheck.net to check your code for errors and improvements.
– glenn jackman
Mar 14 at 16:11




Use shellcheck.net to check your code for errors and improvements.
– glenn jackman
Mar 14 at 16:11










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Since you're learning, here is some guidance on your questions, as opposed to complete script.



There's more than one way to get the size of a file; on Linux systems, the stat utility is a great choice because you can ask it for the bytes directly:



bytes=$(stat -c %s -- "$f")


another option is the wc utility:



bytes=$(wc -c < "$f")


Note that you want to redirect the file into wc as opposed to the instinctual wc -c "$f", as the latter form will also output the filename, while the former (since it does not know the filename), simply outputs the byte count.



To test whether a given file is a regular file or a directory, the usual test is:



if [ -d "$f" ]
then
echo It is a directory
else
echo It is not a directory
fi


For counting the number of (non-hidden) files in a directory, you have a few options:




  • use the POSIX-specified set utility:



    set -- /path/to/dir/*; echo "$#"



  • use an array (since you tagged bash):



    files=( /path/to/dir/* )
    echo "There are $#files[@] files in there"


These will count the number of "files" (files and directories) in that given directory. The default behavior in most shells is to omit dot-files (e.g. .bashrc) when expanding the * glob; you can adjust that with bash by running shopt -s dotglob or shopt -u dotglob, and check it with shopt dotglob.






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
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430194%2fecho-print-the-files-in-a-directory-echo-the-files-size-and-echo-the-qty-of-fil%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
    0
    down vote













    Since you're learning, here is some guidance on your questions, as opposed to complete script.



    There's more than one way to get the size of a file; on Linux systems, the stat utility is a great choice because you can ask it for the bytes directly:



    bytes=$(stat -c %s -- "$f")


    another option is the wc utility:



    bytes=$(wc -c < "$f")


    Note that you want to redirect the file into wc as opposed to the instinctual wc -c "$f", as the latter form will also output the filename, while the former (since it does not know the filename), simply outputs the byte count.



    To test whether a given file is a regular file or a directory, the usual test is:



    if [ -d "$f" ]
    then
    echo It is a directory
    else
    echo It is not a directory
    fi


    For counting the number of (non-hidden) files in a directory, you have a few options:




    • use the POSIX-specified set utility:



      set -- /path/to/dir/*; echo "$#"



    • use an array (since you tagged bash):



      files=( /path/to/dir/* )
      echo "There are $#files[@] files in there"


    These will count the number of "files" (files and directories) in that given directory. The default behavior in most shells is to omit dot-files (e.g. .bashrc) when expanding the * glob; you can adjust that with bash by running shopt -s dotglob or shopt -u dotglob, and check it with shopt dotglob.






    share|improve this answer
























      up vote
      0
      down vote













      Since you're learning, here is some guidance on your questions, as opposed to complete script.



      There's more than one way to get the size of a file; on Linux systems, the stat utility is a great choice because you can ask it for the bytes directly:



      bytes=$(stat -c %s -- "$f")


      another option is the wc utility:



      bytes=$(wc -c < "$f")


      Note that you want to redirect the file into wc as opposed to the instinctual wc -c "$f", as the latter form will also output the filename, while the former (since it does not know the filename), simply outputs the byte count.



      To test whether a given file is a regular file or a directory, the usual test is:



      if [ -d "$f" ]
      then
      echo It is a directory
      else
      echo It is not a directory
      fi


      For counting the number of (non-hidden) files in a directory, you have a few options:




      • use the POSIX-specified set utility:



        set -- /path/to/dir/*; echo "$#"



      • use an array (since you tagged bash):



        files=( /path/to/dir/* )
        echo "There are $#files[@] files in there"


      These will count the number of "files" (files and directories) in that given directory. The default behavior in most shells is to omit dot-files (e.g. .bashrc) when expanding the * glob; you can adjust that with bash by running shopt -s dotglob or shopt -u dotglob, and check it with shopt dotglob.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Since you're learning, here is some guidance on your questions, as opposed to complete script.



        There's more than one way to get the size of a file; on Linux systems, the stat utility is a great choice because you can ask it for the bytes directly:



        bytes=$(stat -c %s -- "$f")


        another option is the wc utility:



        bytes=$(wc -c < "$f")


        Note that you want to redirect the file into wc as opposed to the instinctual wc -c "$f", as the latter form will also output the filename, while the former (since it does not know the filename), simply outputs the byte count.



        To test whether a given file is a regular file or a directory, the usual test is:



        if [ -d "$f" ]
        then
        echo It is a directory
        else
        echo It is not a directory
        fi


        For counting the number of (non-hidden) files in a directory, you have a few options:




        • use the POSIX-specified set utility:



          set -- /path/to/dir/*; echo "$#"



        • use an array (since you tagged bash):



          files=( /path/to/dir/* )
          echo "There are $#files[@] files in there"


        These will count the number of "files" (files and directories) in that given directory. The default behavior in most shells is to omit dot-files (e.g. .bashrc) when expanding the * glob; you can adjust that with bash by running shopt -s dotglob or shopt -u dotglob, and check it with shopt dotglob.






        share|improve this answer












        Since you're learning, here is some guidance on your questions, as opposed to complete script.



        There's more than one way to get the size of a file; on Linux systems, the stat utility is a great choice because you can ask it for the bytes directly:



        bytes=$(stat -c %s -- "$f")


        another option is the wc utility:



        bytes=$(wc -c < "$f")


        Note that you want to redirect the file into wc as opposed to the instinctual wc -c "$f", as the latter form will also output the filename, while the former (since it does not know the filename), simply outputs the byte count.



        To test whether a given file is a regular file or a directory, the usual test is:



        if [ -d "$f" ]
        then
        echo It is a directory
        else
        echo It is not a directory
        fi


        For counting the number of (non-hidden) files in a directory, you have a few options:




        • use the POSIX-specified set utility:



          set -- /path/to/dir/*; echo "$#"



        • use an array (since you tagged bash):



          files=( /path/to/dir/* )
          echo "There are $#files[@] files in there"


        These will count the number of "files" (files and directories) in that given directory. The default behavior in most shells is to omit dot-files (e.g. .bashrc) when expanding the * glob; you can adjust that with bash by running shopt -s dotglob or shopt -u dotglob, and check it with shopt dotglob.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 14 at 16:20









        Jeff Schaller

        31.2k846105




        31.2k846105






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430194%2fecho-print-the-files-in-a-directory-echo-the-files-size-and-echo-the-qty-of-fil%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?