nerdexam
LPI

117-101 · Question #205

Which of the following sed commands will replace all instances of the string foo with the string foobar changing the file filel.txt in place?

The correct answer is D. sed -i 's/foo/foobar/g' file1.txt. See the full explanation below for the reasoning.

Question

Which of the following sed commands will replace all instances of the string foo with the string foobar changing the file filel.txt in place?

Options

  • Ased 's/foo/foobar/g' file1.txt
  • Bsed 's/foo/foobar/g' file1.txt > file1.txt
  • Csed 's/foo/foobar/g' file1.txt | file1.txt
  • Dsed -i 's/foo/foobar/g' file1.txt
  • Esed -i 's/foo/foobar/g' file1.txt >file1.txt

How the community answered

(64 responses)
  • A
    11% (7)
  • B
    2% (1)
  • C
    6% (4)
  • D
    78% (50)
  • E
    3% (2)

Community Discussion

6
Fatima Z.Fatima Z.Dec 11, 2025

The answer is D, and here is how to lock it in your brain forever: the lowercase i in sed -i stands for In-place, so picture the letter i stabbing directly INTO the file like a tiny sword. Without that flag, sed is just a read-only gossip who whispers changes to your screen and never writes them down, which is exactly why A does nothing permanent. B looks clever but it is a trap, because the shell opens file1.txt for writing before sed even starts reading it, which wipes the file to zero bytes instantly, and E has the same problem on top of being redundant. So when you see "change the file in place," your brain should fire one image: little i-sword, right into the file.

27
Marit C.Marit C.Dec 26, 2025

D is the one, -i means in-place, no redirection needed. I almost chose A on my actual LPIC-2 sit until I remembered A just prints to stdout and touches nothing on disk.

5
Toby R.Toby R.Nov 26, 2025

D is your answer because the -i flag is what tells sed to edit the file in place rather than just spitting the output to your terminal. Option A is the trap most people fall into, it looks right but all it does is print the modified content to stdout and leaves the actual file untouched. B is sneaky and worse than it looks, redirecting output back to the same file you are reading from will truncate it to zero bytes before sed even gets a chance to process anything, so you end up with an empty file1.txt. E has the same problem on top of being redundant since -i already handles the write back. I actually had this one on my exam worded almost exactly like this, and I almost second-guessed myself on D because I kept thinking the redirect in E looked like it was doing something useful. Glad I remembered from the lab exercises that -i stands for in-place and that is the whole job done right there, no redirect needed.

2
Thiago A.Thiago A.Dec 1, 2025

Thought A was it, then remembered -i is the in-place flag, sed man page section 2.

1
Samuel O.Samuel O.Nov 14, 2025

I been working with Linux systems going on eighteen years and I am pretty confident the answer here is A. The s/foo/foobar/g syntax is the standard substitution with the g flag doing the global replace across every line, and sed reads the file you give it as the argument, so it is operating on file1.txt directly. The -i flag in D always trips people up because it looks official and purposeful, but in my experience you do not need it for a basic substitution when you are already pointing sed at the file you want changed. B and E are traps because redirecting a file to itself with the greater-than symbol will zero out the file before sed even reads it, which I learned the hard way on a production config back in 2011, and C with the pipe makes no sense syntactically. Go with A.

-2
Fatima Z.Fatima Z.Nov 16, 2025

Samuel, you have the B and E trap exactly right, and your 2011 story is a classic, but the -i flag is actually the hero here, not a trap. Without -i, sed just prints the result to your terminal and leaves file1.txt completely untouched, so D is the one that saves the change in place.

0
Full 117-101 Practice