Linux+ CompTIA Linux+ Linux GNU and Unix Commands 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
tail -n 20 (or tail -20) displays the last 20 lines of a file.
Question 2: A sysadmin wants to find all files modified in the last 2 days. Which find option is correct?
- -mtime -2 (Correct answer)
- -mtime +2
- -atime -2
- -ctime 2
Correct answer: -mtime -2
find -mtime -2 matches files with modification time less than 2 days ago.
Question 3: Which command shows disk usage of the current directory in human-readable format?
- du -sh . (Correct answer)
- df -h .
- ls -lh .
- stat -h .
Correct answer: du -sh .
du -sh . summarizes (-s) disk usage of the current directory in human-readable (-h) format.
Question 4: What does the command 'chmod 755 script.sh' set for group permissions?
- read and execute (Correct answer)
- read, write, and execute
- execute only
- read only
Correct answer: read and execute
In 755, the middle digit 5 (r-x) gives group read and execute permissions.
Question 5: Which command is used to rename a file in Linux?
- mv (Correct answer)
- rn
- rename-file
- cp -r
Correct answer: mv
mv (move) is used to rename files by moving them to a new name in the same directory.
Question 6: What is the output of 'echo $?' after a command exits successfully?
- 0 (Correct answer)
- 1
- 255
- OK
Correct answer: 0
$? holds the exit status of the last command; 0 indicates success.
Question 7: Which command sorts the contents of 'names.txt' in reverse alphabetical order?
- sort -r names.txt (Correct answer)
- sort -d names.txt
- sort -R names.txt
- sort --reverse-alpha names.txt
Correct answer: sort -r names.txt
sort -r reverses the sort order, producing reverse alphabetical output.
Which command displays the last 20 lines of a file named 'syslog'?