Echo/print the files in a Directory, echo the files size and echo the qty of files
Clash 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
bash
add a comment |Â
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
bash
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 withstat
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
add a comment |Â
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
bash
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
bash
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 withstat
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
add a comment |Â
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 withstat
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
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered Mar 14 at 16:20
Jeff Schaller
31.2k846105
31.2k846105
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%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
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
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