What command is commonly used in bash to loop over all files in a directory?
The for loop is commonly used with a wildcard pattern like for file in *; do ... done to loop over all files in the current directory.
Click to reveal answer
beginner
How do you loop over only directories in bash?
Use a pattern that matches directories, for example: for dir in */; do ... done. The trailing slash / ensures only directories are matched.
Click to reveal answer
beginner
What does the following script do?
for file in *.txt; do echo "$file"; done
It loops over all files ending with .txt in the current directory and prints each filename on its own line.
Click to reveal answer
intermediate
Why should you quote variables like "$file" inside loops?
Quoting variables prevents word splitting and preserves spaces or special characters in filenames, avoiding errors during processing.
Click to reveal answer
intermediate
How can you loop over files recursively in bash?
You can use the find command with a loop, for example: find . -type f -name "*.txt" -exec bash -c 'echo "$0"' {} \; or use a while read loop with find and read.
Click to reveal answer
Which pattern matches only directories in a bash for loop?
A*/
B*
C*.txt
D.*
✗ Incorrect
The pattern */ matches only directories because the trailing slash indicates directories.
What is the purpose of quoting variables like "$file" inside loops?
ATo prevent word splitting and handle spaces in filenames
BTo make the script run faster
CTo convert filenames to uppercase
DTo ignore hidden files
✗ Incorrect
Quoting variables prevents word splitting and preserves spaces or special characters in filenames.
How do you loop over all .txt files in the current directory in bash?
Awhile read file; do ... done < *.txt
Bfor file in *; do if file ends with .txt; done
Cloop *.txt
Dfor file in *.txt; do ... done
✗ Incorrect
The for file in *.txt; do ... done syntax loops over all files ending with .txt.
Which command helps to loop over files recursively in bash?
Als -R
Bfind
Cgrep
Dcat
✗ Incorrect
The find command is used to search files recursively and can be combined with loops.
What does the wildcard * match in bash?
AOnly files starting with a dot
BOnly files with extensions
CAll files and directories except hidden ones
DOnly directories
✗ Incorrect
The wildcard * matches all files and directories except hidden ones (those starting with a dot).
Explain how to loop over all files in a directory using bash scripting.
Think about using a for loop with * and why quoting matters.
You got /4 concepts.
Describe how to loop over directories only and why the pattern differs from looping over files.
Directories end with a slash in patterns.
You got /3 concepts.
Practice
(1/5)
1. What does the following bash loop do? for file in *; do echo "$file"; done
easy
A. It runs the echo command once without any output.
B. It deletes all files in the current folder.
C. It lists only directories in the current folder.
D. It prints the names of all files and directories in the current folder.
Solution
Step 1: Understand the loop variable and pattern
The loop uses * which matches all files and directories in the current folder.
Step 2: Analyze the command inside the loop
Each matched item is stored in file and printed with echo.
Final Answer:
It prints the names of all files and directories in the current folder. -> Option D
Quick Check:
Loop over * prints all names [OK]
Hint: Remember * matches all files and folders [OK]
Common Mistakes:
Thinking * matches only files, not directories
Assuming the loop deletes files
Believing echo runs only once
2. Which of the following is the correct syntax to loop over all directories only in the current folder?
easy
A. for dir in *; do if [ -f "$dir" ]; then echo "$dir"; fi; done
B. for dir in *; do echo "$dir"; done
C. for dir in */; do echo "$dir"; done
D. for dir in /*; do echo "$dir"; done
Solution
Step 1: Understand the pattern for directories
The pattern */ matches only directories in the current folder because directories end with a slash.
Step 2: Check the loop and echo command
The loop iterates over directories and prints their names correctly.
Final Answer:
for dir in */; do echo "$dir"; done -> Option C
Quick Check:
*/ matches directories only [OK]
Hint: Use */ pattern to match directories only [OK]
Common Mistakes:
Using * matches files and directories, not only directories
Using -f to check for directories instead of -d
Using absolute path /* instead of current folder
3. What will be the output of this script if the current folder contains files: file1.txt, file2.txt and a directory docs/?
for item in *; do
if [ -f "$item" ]; then
echo "File: $item"
else
echo "Not a file: $item"
fi
done
medium
A. File: file1.txt
File: file2.txt
Not a file: docs
B. File: file1.txt
File: file2.txt
File: docs
C. Not a file: file1.txt
Not a file: file2.txt
Not a file: docs
D. File: file1.txt
Not a file: file2.txt
Not a file: docs
Solution
Step 1: Identify file and directory checks
The script uses [ -f "$item" ] to check if the item is a file.
Step 2: Apply checks to each item
file1.txt and file2.txt are files, so print "File: ..."; docs is a directory, so print "Not a file: docs".
Final Answer:
File: file1.txt
File: file2.txt
Not a file: docs -> Option A
Quick Check:
-f true for files only [OK]
Hint: Use -f to test files, else it's not a file [OK]
Common Mistakes:
Assuming directories pass -f test
Confusing -f with -d
Expecting directory names with trailing slash
4. Find the error in this script that tries to print only directories:
for item in *; do
if [ -f "$item" ]; then
echo "$item"
fi
done
medium
A. The echo command is missing quotes around $item.
B. It uses -f to check files instead of -d for directories.
C. The loop pattern * does not match any files.
D. The script will print directories correctly.
Solution
Step 1: Understand the test used
The script uses [ -f "$item" ] which checks if the item is a file, not a directory.
Step 2: Identify correct test for directories
To check directories, [ -d "$item" ] should be used instead.
Final Answer:
It uses -f to check files instead of -d for directories. -> Option B
Quick Check:
Use -d to test directories [OK]
Hint: Use -d to check directories, not -f [OK]
Common Mistakes:
Confusing -f and -d tests
Assuming * matches only directories
Ignoring quotes around variables
5. You want to count how many files (not directories) are in the current folder using a bash loop. Which script correctly does this?
hard
A. count=0
for item in *; do
if [ -f "$item" ]; then
count=$((count + 1))
fi
done
echo $count
B. count=0
for item in */; do
count=$((count + 1))
done
echo $count
C. count=0
for item in *; do
if [ -d "$item" ]; then
count=$((count + 1))
fi
done
echo $count
D. count=0
for item in *; do
count=$((count + 1))
done
echo $count
Solution
Step 1: Understand the goal
We want to count files only, so we must check each item with [ -f "$item" ].
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.
Final Answer:
count=0
for item in *; do
if [ -f "$item" ]; then
count=$((count + 1))
fi
done
echo $count -> Option A
Quick Check:
Use -f test and increment count [OK]
Hint: Increment count only if -f test passes [OK]
Common Mistakes:
Counting directories instead of files
Counting all items without checks
Using */ pattern which matches directories only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.