Sample questions
Question 1Easy
Which command appends output to an existing file?
- echo text > file.txt
- echo text < file.txt
- echo text >> file.txt
- echo text | file.txt
Answer: C. echo text >> file.txt
Explanation
>> appends; > overwrites.
Step-by-step solution
- Use double arrow.
- The file is opened in append mode.
- 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?
- stdin, stdout, stderr
- stdout, stdin, stderr
- stderr, stdout, stdin
- 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
- Linux file descriptor 0 is for standard input.
- File descriptor 1 is for standard output.
- File descriptor 2 is for standard error.
- So, 0 stands for stdin, 1 for stdout, 2 for stderr.
- 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?
- Appends the output to the end of out.txt
- Reads input from out.txt
- Writes the output to out.txt, overwriting it
- 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
- The
> symbol redirects output to a file. > overwrites the contents of out.txt with new output.- If out.txt exists,
> removes its current content. - If out.txt does not exist,
> creates it and writes output there. - 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?
- The size of the directory in bytes
- The number of entries listed
- The number of words
- 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
ls prints one name per line when piped.wc -l counts lines.- 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?
- Splits large files into smaller pieces and saves them to disk
- Saves output to out.txt while passing it on to the next command
- Deletes duplicate lines
- 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
tee is like a T-junction.- One copy goes to the file.
- 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?
- Writes the error message to err.txt
- Writes the normal output of ls to err.txt
- Appends to err.txt
- 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
- The command runs ls on /nonexistent which generates an error.
- The
2> operator redirects stderr to err.txt. - This writes the error message to err.txt instead of the screen.
- The file is created if it doesn't exist, or overwritten if it does.
- 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?
- Writes both stdout and stderr to out.txt
- Writes only errors to out.txt
- Writes stdout to the screen and stderr to a file
- 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
> out.txt sends stdout to the file.2>&1 duplicates stdout onto stderr.- 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
- EOF
- Nothing; EOF must be quoted
line1
line2
- line1
Answer: C.
Explanation
A here-document feeds the lines up to the delimiter to the command's stdin.
Step-by-step solution
<<EOF starts a here-doc.- Lines are collected until EOF.
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?
- A: terminal; B: o.txt
- Both: o.txt
- Both: terminal
- 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
- Handle redirections in order.
- A sends fd 1 to file, then fd 2 to the same place.
- 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?
- Saves output in /dev/null for later
- Sends errors to the terminal only
- Appends output to a log
- 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
- Command > /dev/null redirects standard output to /dev/null.
- 2>&1 redirects standard error to the same place as standard output.
- Since standard output is already going to /dev/null, both outputs are discarded.
- This means neither stdout nor stderr is shown or saved.
- 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 →