Courseiva
hardMultiple ChoiceObjective-mapped

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 is typically used for reading lines from a file, not for iterating over files.

  • select option; do

    Why it's wrong here

    select is used to create a menu; it does not iterate over files automatically.

  • until condition; do

    Why it's wrong here

    until loops until a condition becomes true; not designed for file iteration.

  • for f in *.txt; do

    Why this is correct

    This bash loop iterates over each .txt file in the current directory.

About these practice questions

This XK0-006 question is part of Courseiva's 979-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 →

How Courseiva writes practice questions · Editorial policy

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.