CompTIA Linux+ Exam XK0-004 Control Mode Commands | Answering Your Linux+ Exam Question

Using Control Mode Commands to Substitute /dev/sdc with /dev/sdd in vi | CompTIA Linux+ Exam XK0-004

Question

Using which of the following control mode commands is the correct way to substitute all occurrences of /dev/sdc with /dev/sdd while editing a file in vi?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The correct command to substitute all occurrences of /dev/sdc with /dev/sdd while editing a file in vi is option D: :%s//dev/sdc//dev/sdd.

Here's a breakdown of the command and its components:

  • The colon (:) puts vi into command mode.
  • The percent sign (%) specifies that the substitution should be performed on all lines in the file.
  • The "s" indicates that a substitution should be performed.
  • The first forward slash (/) is the delimiter character used to separate the parts of the command. It is immediately followed by the search string, /dev/sdc, which is enclosed in forward slashes.
  • The second forward slash (/) is the delimiter character again, followed by the replacement string, /dev/sdd, also enclosed in forward slashes.
  • The final forward slash (/) is another delimiter character, followed by the "g" character, which means that the substitution should be performed globally (i.e., on all occurrences in each line).

Option A, :s//dev/sdc//dev/sdd, is incorrect because it omits the delimiter characters and specifies an empty search string. It would result in a syntax error.

Option B, :s//dev/sdc//dev/sdd/g, is correct except that it specifies the global substitution flag (the "g" at the end) unnecessarily. The global flag is implied by the use of the percent sign (%), which specifies that the substitution should be performed on all lines in the file.

Option C, :s//dev/sdc//dev/sdd, is correct except that it only performs the substitution on the current line. Without the global flag (the "g" at the end), it would only substitute the first occurrence on each line.