nerdexam
CompTIA

XK0-004 · Question #218

A junior Linux administrator needs to create 16 empty files quickly that will contain sales information for each quarter for the past four years. Which of the following commands will meet this…

The correct answer is A. touch {2015,2016,2017,2018}.{q1,q2,q3,q4}. Bash brace expansion with comma-separated values inside curly braces generates all combinations of the listed tokens. Using the syntax {a,b}.{c,d} creates a Cartesian product of the two lists as filenames passed to touch.

Scripting, Containers and Automation

Question

A junior Linux administrator needs to create 16 empty files quickly that will contain sales information for each quarter for the past four years. Which of the following commands will meet this requirement?

Options

  • Atouch {2015,2016,2017,2018}.{q1,q2,q3,q4}
  • Btouch [2015,2016,2017,2018].[q1,q2,q3,q4]
  • Ctouch 2015,2016,2017,2018.q1,q2,q3,q4
  • Dtouch {2015.2016.2017.2018}.{q1.q2.q3.q4}

How the community answered

(57 responses)
  • A
    79% (45)
  • B
    11% (6)
  • C
    7% (4)
  • D
    4% (2)

Why each option

Bash brace expansion with comma-separated values inside curly braces generates all combinations of the listed tokens. Using the syntax {a,b}.{c,d} creates a Cartesian product of the two lists as filenames passed to touch.

Atouch {2015,2016,2017,2018}.{q1,q2,q3,q4}Correct

Bash brace expansion with comma-separated values - such as {2015,2016,2017,2018}.{q1,q2,q3,q4} - generates all 16 combinations (e.g., 2015.q1, 2015.q2, ... 2018.q4), and 'touch' creates an empty file for each one. This is the correct syntax for producing a Cartesian product of named values in bash.

Btouch [2015,2016,2017,2018].[q1,q2,q3,q4]

Square brackets in shell globbing define character classes or ranges (e.g., [a-z]), not comma-separated lists for Cartesian expansion, so this syntax would not produce 16 distinct files.

Ctouch `2015,2016,2017,2018`.`q1,q2,q3,q4`

Backticks are used for command substitution - they execute a command and return its output - not for brace-style list expansion, so this syntax would fail or produce unexpected results.

Dtouch {2015.2016.2017.2018}.{q1.q2.q3.q4}

Brace expansion requires comma-separated values inside curly braces; using periods as separators inside braces treats the entire string as a single literal token rather than expanding into separate values.

Concept tested: Bash brace expansion for bulk file creation

Source: https://www.gnu.org/software/bash/manual/bash.html#Brace-Expansion

Topics

#brace expansion#touch command#shell scripting#file creation

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice