hardMultiple Choice
XK0-006 Practice Question: A script needs to iterate over all .txt files in…
A script needs to iterate over all .txt files in a directory. Which loop structure correctly implements this?
⚠ Common exam trap
Test-takers frequently confuse `while read` (which processes lines of text) with iterating over files, or think `select` is a general-purpose loop, when in fact only `for` with a glob pattern directly matches the requirement of iterating over all .txt files.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
for f in *.txt; do
The `for f in *.txt; do` loop is correct because it uses shell globbing to expand `*.txt` into a list of all .txt filenames in the current directory, then iterates over each filename. This is the standard and most efficient way to process a set of files matching a pattern in Bash and POSIX shell scripting.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
while read line; do
Why it's wrong here
while read line consumes stdin line by line, so it iterates text content rather than filenames matching *.txt. It is tempting because read loops are the standard idiom for processing file contents line by line, which is the correct choice when parsing a file's data.
- ✗
select option; do
Why it's wrong here
The select loop reads positional parameters or piped input, not directory entries, so it cannot enumerate .txt files directly. It is tempting because select builds interactive numbered menus from a list, which suits prompting a user to choose an option, not globbing filenames.
- ✗
until condition; do
Why it's wrong here
until repeats while its condition evaluates false, so it provides no mechanism to expand a glob into successive filenames. It is tempting because until is a genuine loop construct, and it would be correct when you must repeat a command until a specific test succeeds.
- ✓
for f in *.txt; do
Why this is correct
The glob *.txt expands to every matching filename in the current directory, and for iterates over that list, assigning each name to f in turn. This is the standard shell construct for processing a directory's .txt files without external commands.
Go deeper
Related to this question
Learn chapter
Basic Shell Scripting
Key term
Shell
A shell is a computer program that provides a user interface to access an operating system's services, typically by accepting text commands.
Key term
Bash
Bash is a command-line interpreter that lets users interact with an operating system by typing text commands instead of clicking icons.
About these practice questions
This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.