Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please show me how to write a bash script and a loop to count the files Script:

ID: 3885749 • Letter: P

Question

Please show me how to write a bash script and a loop to count the files

Script: Count the files in the directory Goals: Write a bash script Use linux shell commands within your script Provide user feedback We have a lot of files in this archive. Create a script to count each file in the archive using a loop. Name the script filecounter.sh The script should take a target directory and output: Number of files in :: However, this is not the goal. The goal is to work with Bash script to accomplish this. If your script is simple this command, you will not gain full points. Yes - I realize I'm asking you to do something "less efficient" - this is for the sake of practice. Use CHMOD to make the script executable. Run it and take a screen shot of the result and put it in your answers file.

Explanation / Answer

The bash script required is given below:

#!/bin/bash

LOC=$1
FCOUNT=0
DIRECTORYCOUNT=0

#TOTALDIRS=$(find $LOC -type d)
#TOTALFILES=$(find $LOC -type f)

#for d in $TOTALDIRS
#do
#   DIRECTORYCOUNT=$[$DIRECTORYCOUNT+1]
#done

#for f in $TOTALFILES
#do
#   FCOUNT=$[$FCOUNT+1]
#done

for item in $LOC
do
if [ -f "$item" ]
    then
         FCOUNT=$[$FCOUNT+1]
    elif [ -d "$item" ]
        then
         DIRECTORYCOUNT=$[$DIRECTORYCOUNT+1]
fi
done

echo "Total File count: " $FCOUNT
echo "Total Directory count: " $DIRECTORYCOUNT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote