Write a shell script program called “countf.sh” that will count how many files o
ID: 3663069 • Letter: W
Question
Write a shell script program called “countf.sh” that will count how many files or directories recursively. It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide –h option to print out how to use your countf.sh program.
Write a shell script program called “countf.sh” that will count how many files or directories recursively. It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide –h option to print out how to use your countf.sh program.
% ./countf.sh
% ./countf.sh dir1 dir2 ....
%./countf.sh –h
hints: I would write a script function to process a count for each directory and return a number of files to be added up. You should have your linux or linux-like environment, either using virtual machine or have your own linux box
% ./countf.sh % ./countf.sh dir1 dir2 .... %./countf.sh –h hints: I would write a script function to process a count for each directory and return a number of files to be added up. You should have your linux or linux-like environment, either using virtual machine or have your own linux box
Explanation / Answer
#!/bin/bash
#countf.sh
declare -i FilesCounter="0"
declare -i directoryCounter="0"
for file in /*
do
# if files found then incrementing file counter
if [ -f $file ]
then
((FilesCounter++))
# if directory found then incrementing directory counter
elif [ -d $file ]
then
((directoryCounter++))
fi
done
echo The files total "$FilesCounter"
echo The directories total "$directoryCounter"
echo $?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.