Redirection & Pipes Questions with Answers

20 practice questions on Redirection & Pipes (Linux & Shell) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

Which command appends output to an existing file?

  1. echo text > file.txt
  2. echo text < file.txt
  3. echo text >> file.txt
  4. echo text | file.txt

Answer: C. echo text >> file.txt

Explanation

>> appends; > overwrites.

Step-by-step solution

  1. Use double arrow.
  2. The file is opened in append mode.
  3. Existing content stays.

Why the other options are wrong

  • A. That overwrites.
  • B. < reads from a file.
  • D. A file is not a command.

Question 2Easy

What does a Linux file descriptor 0, 1 and 2 stand for?

  1. stdin, stdout, stderr
  2. stdout, stdin, stderr
  3. stderr, stdout, stdin
  4. root, user, group

Answer: A. stdin, stdout, stderr

Explanation

The three standard streams are numbered 0 (input), 1 (output) and 2 (error).

Step-by-step solution

  1. Linux file descriptor 0 is for standard input.
  2. File descriptor 1 is for standard output.
  3. File descriptor 2 is for standard error.
  4. So, 0 stands for stdin, 1 for stdout, 2 for stderr.
  5. Final answer: stdin, stdout, stderr.

Why the other options are wrong

  • B. Descriptor 0 is stdin, not stdout; the order is stdin, stdout, stderr.
  • C. Descriptor 0 is stdin and 2 is stderr, so this order is reversed.
  • D. These are user and group names, not the standard file descriptors.

Tip: Remember 0 is input, 1 is output, 2 is error.

Question 3Easy

What does > do in ls > out.txt?

  1. Appends the output to the end of out.txt
  2. Reads input from out.txt
  3. Writes the output to out.txt, overwriting it
  4. Pipes the output into another command

Answer: C. Writes the output to out.txt, overwriting it

Explanation

> truncates and writes; >> appends.

Step-by-step solution

  1. The > symbol redirects output to a file.
  2. > overwrites the contents of out.txt with new output.
  3. If out.txt exists, > removes its current content.
  4. If out.txt does not exist, > creates it and writes output there.
  5. This matches option C exactly.

Why the other options are wrong

  • A. Appending uses >>; > replaces the file contents with the new output.
  • B. Reading input from a file uses <; > sends the output of ls into out.txt.
  • D. Piping uses |; > writes the output into a file, not into another command.

Tip: Remember: > writes and overwrites; >> appends without deleting.

Question 4Easy

What does ls | wc -l count?

  1. The size of the directory in bytes
  2. The number of entries listed
  3. The number of words
  4. Hidden files only

Answer: B. The number of entries listed

Explanation

The pipe sends the listing to wc -l, which counts lines.

Step-by-step solution

  1. ls prints one name per line when piped.
  2. wc -l counts lines.
  3. Hidden files are not listed without -a.

Why the other options are wrong

  • A. Bytes use wc -c.
  • C. Words use wc -w.
  • D. Hidden files need ls -a.

Question 5Medium

What is the purpose of tee in cmd | tee out.txt | less?

  1. Splits large files into smaller pieces and saves them to disk
  2. Saves output to out.txt while passing it on to the next command
  3. Deletes duplicate lines
  4. Counts lines

Answer: B. Saves output to out.txt while passing it on to the next command

Explanation

tee copies stdin to a file and to stdout. tee -a appends.

Step-by-step solution

  1. tee is like a T-junction.
  2. One copy goes to the file.
  3. One goes to the pipe.

Why the other options are wrong

  • A. That is split.
  • C. That is uniq.
  • D. That is wc -l.

Question 6Medium

What does ls /nonexistent 2> err.txt do?

  1. Writes the error message to err.txt
  2. Writes the normal output of ls to err.txt
  3. Appends to err.txt
  4. Does nothing at all because ls fails

Answer: A. Writes the error message to err.txt

Explanation

2> redirects file descriptor 2 (stderr) to the file.

Step-by-step solution

  1. The command runs ls on /nonexistent which generates an error.
  2. The 2> operator redirects stderr to err.txt.
  3. This writes the error message to err.txt instead of the screen.
  4. The file is created if it doesn't exist, or overwritten if it does.
  5. So the error message is saved in err.txt.

Why the other options are wrong

  • B. stdout is not redirected; the normal output of ls would still go to the terminal.
  • C. 2> overwrites err.txt instead of appending; >> would be needed to append.
  • D. The command does run and writes the error to err.txt, so it is not a no-op.

Tip: Use 2> to save error messages to a file for troubleshooting.

Question 7Medium

What does cmd > out.txt 2>&1 do?

  1. Writes both stdout and stderr to out.txt
  2. Writes only errors to out.txt
  3. Writes stdout to the screen and stderr to a file
  4. Closes stderr

Answer: A. Writes both stdout and stderr to out.txt

Explanation

2>&1 points descriptor 2 to wherever 1 currently goes. Order matters: put it after > out.txt.

Step-by-step solution

  1. > out.txt sends stdout to the file.
  2. 2>&1 duplicates stdout onto stderr.
  3. Both end up in the file.

Why the other options are wrong

  • B. stdout is redirected too.
  • C. stdout goes to the file.
  • D. It is merged, not closed.

Question 8Medium

What is the output?

cat <<EOF
line1
line2
EOF
  1. EOF
  2. Nothing; EOF must be quoted
  3. line1
    line2

  4. line1

Answer: C.

Explanation

A here-document feeds the lines up to the delimiter to the command's stdin.

Step-by-step solution

  1. <<EOF starts a here-doc.
  2. Lines are collected until EOF.
  3. cat prints them.

Why the other options are wrong

  • A. The delimiter itself is not printed.
  • B. Quoting only turns off expansion.
  • D. Both lines print.

Question 9Hard

Two commands: A: ls /nope > o.txt 2>&1 and B: ls /nope 2>&1 > o.txt. Where does the error message of ls go?

  1. A: terminal; B: o.txt
  2. Both: o.txt
  3. Both: terminal
  4. A: into o.txt; B: to the terminal

Answer: D. A: into o.txt; B: to the terminal

Explanation

Redirections apply left to right. In B, stderr is pointed at the terminal (current stdout) first, and only then stdout moves to the file.

Step-by-step solution

  1. Handle redirections in order.
  2. A sends fd 1 to file, then fd 2 to the same place.
  3. B copies fd 1 (terminal) to fd 2 first.

Why the other options are wrong

  • A. Reversed.
  • B. B leaves stderr on the terminal.
  • C. A captures the error.

Tip: Put > file before 2>&1.

Question 10Hard

What does command > /dev/null 2>&1 achieve?

  1. Saves output in /dev/null for later
  2. Sends errors to the terminal only
  3. Appends output to a log
  4. Discards both stdout and stderr

Answer: D. Discards both stdout and stderr

Explanation

stdout goes to /dev/null and stderr is redirected to wherever stdout points.

Step-by-step solution

  1. Command > /dev/null redirects standard output to /dev/null.
  2. 2>&1 redirects standard error to the same place as standard output.
  3. Since standard output is already going to /dev/null, both outputs are discarded.
  4. This means neither stdout nor stderr is shown or saved.
  5. The final answer is Discards both stdout and stderr.

Why the other options are wrong

  • A. Output goes to /dev/null, which discards it; nothing is saved there for later.
  • B. stderr is also redirected to /dev/null, so no error text reaches the terminal.
  • C. Output is not appended to a log; > discards it into /dev/null.

Tip: Use > /dev/null 2>&1 to hide all command output in scripts.

Practise all 20 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Linux & Shell topics · All topics