RHCSA RHCSA Essential Command-Line Tools 2 — Questions and Answers
Question 1: Which command displays the last 20 lines of a file named 'syslog'?
- tail -20 syslog (Correct answer)
- head -20 syslog
- cat -20 syslog
- less -20 syslog
Correct answer: tail -20 syslog
The tail command with -N flag outputs the last N lines of a file.
Question 2: What does the command 'wc -l filename' report?
- Number of lines in the file (Correct answer)
- Number of words in the file
- Number of characters in the file
- File size in bytes
Correct answer: Number of lines in the file
The wc -l option counts and displays the number of newline characters (lines) in a file.
Question 3: Which command would you use to search for the string 'error' recursively in all files under /var/log?
- grep -r 'error' /var/log (Correct answer)
- find /var/log -name 'error'
- locate error /var/log
- search -r 'error' /var/log
Correct answer: grep -r 'error' /var/log
grep -r performs a recursive search through directories for a pattern.
Question 4: What is the effect of running 'sort -u file.txt'?
- Sorts the file and removes duplicate lines (Correct answer)
- Sorts the file in reverse order
- Sorts the file and counts unique lines
- Updates the file with sorted output in place
Correct answer: Sorts the file and removes duplicate lines
The sort -u option sorts lines and suppresses duplicate output lines, keeping only unique entries.
Question 5: Which sed command replaces only the FIRST occurrence of 'foo' with 'bar' on each line of input?
- sed 's/foo/bar/' file (Correct answer)
- sed 's/foo/bar/g' file
- sed 's/foo/bar/1g' file
- sed 'g/foo/bar/' file
Correct answer: sed 's/foo/bar/' file
Without the g flag, sed's s command replaces only the first match on each line by default.
Question 6: What does the 'cut -d: -f1' command do when applied to /etc/passwd?
- Extracts the first field using colon as delimiter (Correct answer)
- Cuts the first character of each line
- Removes the first colon from each line
- Extracts the first line of the file
Correct answer: Extracts the first field using colon as delimiter
cut -d: -f1 uses colon as the field delimiter and extracts field number 1, which is the username in /etc/passwd.
Question 7: Which command can be used to view a file page by page, allowing both forward and backward navigation?
- less (Correct answer)
- more
- cat
- head
Correct answer: less
less allows forward and backward navigation through a file, unlike more which only scrolls forward.
Which command displays the last 20 lines of a file named 'syslog'?