Sample questions
Question 1Easy
Which command searches every file under src/ for the text TODO?
- grep TODO src/ -x
- grep -r TODO src/
- grep -v TODO src/
- find TODO src/
Answer: B. grep -r TODO src/
Explanation
-r recurses into directories. Without it, grep complains that src/ is a directory.
Step-by-step solution
- Directory needs
-r. - grep scans each file.
- Matches print with file name.
Why the other options are wrong
- A.
-x matches whole lines only. - C.
-v inverts the match. - D.
find looks for names, not content.
Question 2Easy
Which command shows the first 10 lines of a file by default?
- tail
- cat -n
- less
- head
Answer: D. head
Explanation
head prints the beginning of a file; tail prints the end.
Step-by-step solution
- Head displays the first few lines of a file by default.
- It shows the first 10 lines without any extra options needed.
- Other commands like tail show the end, and cat -n numbers all lines.
- Less opens a file for scrolling but doesn't display lines by default.
- The correct command is head as it matches the question's requirement.
- Final answer: head.
Why the other options are wrong
- A. tail shows the last 10 lines by default, not the first.
- B. cat -n prints all lines with numbers, not only the first 10.
- C. less opens a file for scrolling page by page, not just the first 10 lines.
Tip: Use head for fast viewing of a file's beginning.
Question 3Easy
Which grep option prints the line number of each match?
- -l
- -c
- -v
- -n
Answer: D. -n
Explanation
grep -n pattern file prefixes each matching line with its line number.
Step-by-step solution
- Run grep with
-n. - Each match shows
number:line. - Useful for jumping to a line.
Why the other options are wrong
- A.
-l lists only file names. - B.
-c counts matching lines. - C.
-v inverts the match.
Question 4Easy
What does echo "a,b,c" | cut -d, -f2 print?
- a
- b
- c
- b,c
Answer: B. b
Explanation
-d, sets the delimiter to a comma and -f2 selects the second field.
Step-by-step solution
- The command splits the input by commas because of -d,.
- Cut -f2 picks the second part after splitting by commas.
- The input is "a,b,c" so splitting gives ["a", "b", "c"]
- Second field is "b" so output is b.
- Final answer is B (b).
Why the other options are wrong
- A. a is field 1; -f2 selects the second comma-separated field, which is b.
- C. c is field 3; -f2 picks the second field, which is b.
- D. cut -f2 returns a single field, not two; b,c would need -f2-3.
Tip: Remember -d defines the separator and -f picks the field number.
Question 5Medium
Which command prints only the names of files that contain a match?
- grep -c pattern *
- grep -l pattern *
- grep -n pattern *
- grep -h pattern *
Answer: B. grep -l pattern *
Explanation
-l lists files with at least one match.
Step-by-step solution
-l stands for files with matches.- Output has one name per file.
- Counts and lines are omitted.
Why the other options are wrong
- A.
-c prints counts. - C.
-n prints line numbers. - D.
-h hides file names.
Question 6Medium
Which command displays the last lines of a file and keeps following new lines?
- head -f
- tail -f
- cat -f
- less +n
Answer: B. tail -f
Explanation
tail -f is the usual way to watch a growing log.
Step-by-step solution
- Tail -f follows a file and shows new lines as they are added.
- It displays the last lines of a file by default.
- The -f option makes tail keep following new lines after the initial display.
- This matches the requirement to follow new lines in a file.
- Therefore, the correct command is tail -f.
Why the other options are wrong
- A. head shows the first lines of a file and has no -f follow option.
- C. cat has no -f follow option; it prints the whole file once and stops.
- D. less +n opens a file at line n; it does not follow new lines as they are added.
Tip: Use tail -f to watch logs or files that grow over time.
Question 7Medium
Which command converts lowercase text to uppercase from standard input?
- tr a-z A-Z
- sed -u with no arguments
- upper (a standard Unix filter)
- sort -u
Answer: A. tr a-z A-Z
Explanation
tr a-z A-Z translates each lowercase character to uppercase.
Step-by-step solution
- Tr translates characters from one set to another.
- Tr a-z A-Z maps each lowercase letter to its uppercase letter.
- So the answer is option A.
Why the other options are wrong
- B. Sed with no script is an error, and -u only changes output buffering; it does not change case.
- C. There is no standard Unix filter named upper.
- D. Sort -u sorts and removes duplicate lines; it does not change case.
Tip: Tr SET1 SET2 maps characters one to one.
Question 8Medium
What does sort file | uniq -c produce?
- Each distinct line prefixed by how many times it occurs
- Only the lines that appear exactly once in the file
- The file sorted in reverse
- The total count of lines in the file
Answer: A. Each distinct line prefixed by how many times it occurs
Explanation
uniq needs sorted input; -c prefixes counts.
Step-by-step solution
- Sort command arranges lines alphabetically.
- Uniq -c counts identical consecutive lines after sorting.
- Each line is prefixed by its occurrence count.
- This shows how many times each distinct line appears.
- Final output lists each unique line with its count.
Why the other options are wrong
- B. That is uniq -u; uniq -c shows counts for all distinct lines.
- C. sort alone gives ascending order; uniq -c adds counts, not reverse order.
- D. The total count is wc -l; uniq -c prints a count for each distinct line.
Tip: Remember: uniq requires sorted input to count duplicates correctly.
Question 9Hard
What does grep -E 'cat|dog' pets.txt use -E for?
- Exact whole-line matching, so only complete lines can be matched
- Excluding matches
- Extended regular expressions, so | means OR
- Ignoring errors
Answer: C. Extended regular expressions, so | means OR
Explanation
In basic regex | is literal; -E (egrep) enables |, +, ?, () as operators.
Step-by-step solution
- Basic regex treats
| literally. -E enables alternation.- Lines with cat or dog match.
Why the other options are wrong
- A. That is
-x. - B. That is
-v. - D. That is
-s.
Question 10Hard
What does awk '{print $2}' file print?
- The second line of the file
- The second character of every line
- The last whitespace-separated field of each line
- The second whitespace-separated field of each line
Answer: D. The second whitespace-separated field of each line
Explanation
awk splits each line into fields $1, $2, ... by whitespace.
Step-by-step solution
- Awk splits each line into fields $1, $2, ... by whitespace.
print $2 outputs the second field of each line.- Each line's fields are separated by spaces or tabs.
- So
awk '{print $2}' file prints the second whitespace-separated field of each line. - The answer is the second whitespace-separated field of each line.
Why the other options are wrong
- A. awk prints fields from each line, not a line from the file; the second line would need NR == 2.
- B. $2 is a whitespace-separated field, not a character position, so characters are not counted.
- C. The last field is $NF in awk; $2 is the second field counted from the start of the line.
Tip: Remember $n gives the nth field in awk, useful for parsing structured text.
Practise all 28 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 →