Courseiva
Shells, Scripting and Data ManagementeasyMultiple ChoiceObjective-mapped

LPIC-1 Shells, Scripting and Data Management Practice Question

An administrator needs to replace all occurrences of 'old_host' with 'new_host' in the file /etc/hosts. Which sed command should be used?

⚠ Common exam trap

The trap here is that candidates often forget the `g` flag for global replacement or omit the `-i` flag for in-place editing, mistakenly thinking sed modifies files by default.

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

sed -i 's/old_host/new_host/g' /etc/hosts

The `-i` flag enables in-place editing of the file, and the `g` flag (global) ensures all occurrences on each line are replaced, not just the first. The command `sed -i 's/old_host/new_host/g' /etc/hosts` modifies the file directly, replacing every instance of 'old_host' with 'new_host' throughout the file.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • sed -n 's/old_host/new_host/gp' /etc/hosts

    Why it's wrong here

    Prints only replaced lines but does not edit the file.

  • sed -i 's/old_host/new_host/' /etc/hosts

    Why it's wrong here

    Edits in-place but only replaces the first occurrence per line due to missing g flag.

  • sed -i 's/old_host/new_host/g' /etc/hosts

    Why this is correct

    Edits /etc/hosts in-place, replacing all occurrences globally.

  • sed 's/old_host/new_host/g' /etc/hosts

    Why it's wrong here

    Performs substitution but does not edit the file in-place; output goes to stdout.

About these practice questions

This LPIC-1 question is part of Courseiva's 527-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 LPIC-1 practice question is part of Courseiva's free LPI 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 LPIC-1 exam.