Recall & Review
beginner
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?<br>
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:<br>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?
✗ Incorrect
The pattern
*/ matches only directories because the trailing slash indicates directories.What is the purpose of quoting variables like "$file" inside loops?
✗ 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?
✗ 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?
✗ Incorrect
The
find command is used to search files recursively and can be combined with loops.What does the wildcard * match in bash?
✗ 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.