Bird
0
0

You want to count how many files (not directories) are in the current folder using a bash loop. Which script correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - Loops
You want to count how many files (not directories) are in the current folder using a bash loop. Which script correctly does this?
Acount=0 for item in *; do if [ -f "$item" ]; then count=$((count + 1)) fi done echo $count
Bcount=0 for item in */; do count=$((count + 1)) done echo $count
Ccount=0 for item in *; do if [ -d "$item" ]; then count=$((count + 1)) fi done echo $count
Dcount=0 for item in *; do count=$((count + 1)) done echo $count
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to count files only, so we must check each item with [ -f "$item" ].
  2. Step 2: Check each option

    count=0 for item in *; do if [ -f "$item" ]; then count=$((count + 1)) fi done echo $count correctly loops over all items, increments count only if item is a file, then prints count.
  3. Final Answer:

    count=0 for item in *; do if [ -f "$item" ]; then count=$((count + 1)) fi done echo $count -> Option A
  4. Quick Check:

    Use -f test and increment count [OK]
Quick Trick: Increment count only if -f test passes [OK]
Common Mistakes:
MISTAKES
  • Counting directories instead of files
  • Counting all items without checks
  • Using */ pattern which matches directories only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes