nerdexam
LPI

101-500 · Question #504

What is true regarding the command ls > files if files does not exist?

The correct answer is B. files is created and contains the output of ls. You've hit your Sonnet limit · resets Jun 8, 9am (UTC)

Shells and Shell Scripting

Question

What is true regarding the command ls > files if files does not exist?

Options

  • AThe output of ls is printed to the terminal
  • Bfiles is created and contains the output of ls
  • CAn error message is shown and ls is not executed
  • DThe command files is executed and receives the output of ls
  • EAny output of ls is discarded

How the community answered

(43 responses)
  • A
    5% (2)
  • B
    93% (40)
  • C
    2% (1)

Explanation

You've hit your Sonnet limit · resets Jun 8, 9am (UTC)

Topics

#shell redirection#output redirection#file creation#stdout

Community Discussion

4
Toby R.Toby R.Jun 27, 2026

B is correct. The > operator redirects stdout to a file, and if that file does not exist the shell creates it, so you end up with a new file called files containing whatever ls would have printed.

13
Anjali D.Anjali D.Jun 21, 2026

B is right, the shell creates files automatically when it does not exist.

5
Devraj B.Devraj B.Jun 27, 2026

Objective 103.4, "Use streams, pipes and redirects," carries a weight of 4 on the 101-500 blueprint, meaning you will see multiple questions that test exactly this behavior, so nail it cold. The shell itself handles the redirection before ls ever runs, and part of that setup is opening the target file for writing, which means creating it when it does not yet exist. That is why C is wrong: there is no error, and ls does execute. D would require the syntax to be a pipe, written with the vertical bar, not the greater-than sign, so that trap is there for people who mix up the two operators. I sat the 101 in Hyderabad back in 2019 and I remember staring at a nearly identical item where the filename looked like a real command, which is exactly what D is banking on. I caught myself, remembered that greater-than always means file redirection and never command execution, marked B, and moved on. Saved me maybe thirty seconds of second-guessing. The memory hook I use is "greater-than points INTO a file," and if the file is not there yet, the shell builds the box before it puts anything inside it.

3
Viktor S.Viktor S.Jun 27, 2026

My first instinct was D, because I read "files" and my brain jumped to "that sounds like a command," which is exactly the kind of trap this question sets for people who are skimming instead of thinking. Then I remembered who actually handles the redirect operator, and it is not ls, it is the shell itself. The shell parses the greater-than sign, creates the file if it does not exist (or truncates it if it does), wires up that file descriptor to stdout, and only then hands control to ls. So B is correct because the file creation happens before ls runs a single line of code, which also explains why E is wrong, the output is not discarded, it goes right into the new file.

0
Full 101-500 Practice