010-160 Linux Command Line & Shell Scripting 4 — Questions and Answers
Question 1: What does the command tail -f /var/log/syslog do?
- Shows the first 10 lines of syslog
- Deletes the syslog after displaying it
- Continuously displays new lines appended to syslog (Correct answer)
- Shows syslog in reverse order
Correct answer: Continuously displays new lines appended to syslog
tail -f follows a file and prints new lines as they are written, useful for live log monitoring.
Question 2: In a bash for loop, which syntax correctly iterates over the values 1 2 3?
- for i in (1 2 3); do echo $i; done
- for i in 1 2 3; do echo $i; done (Correct answer)
- for (i=1; i<=3; i++); echo $i; done
- foreach i (1 2 3) echo $i
Correct answer: for i in 1 2 3; do echo $i; done
Bash for loops use the syntax: for variable in list; do commands; done.
Question 3: Which command prints the current working directory?
- cwd
- dir
- pwd (Correct answer)
- where
Correct answer: pwd
pwd (print working directory) outputs the absolute path of the current directory.
Question 4: How do you redirect standard error (stderr) to a file named errors.txt?
- command 1> errors.txt
- command 2> errors.txt (Correct answer)
- command &> errors.txt
- command >> errors.txt
Correct answer: command 2> errors.txt
File descriptor 2 refers to stderr, so 2> redirects error output to the specified file.
Question 5: What character begins a comment in a bash script?
- //
- --
- # (Correct answer)
- /*
Correct answer: #
In bash, everything after # on a line is treated as a comment and ignored by the shell.
Question 6: Which command makes a shell script named setup.sh executable?
- exec setup.sh
- chmod +x setup.sh (Correct answer)
- chown +x setup.sh
- run setup.sh
Correct answer: chmod +x setup.sh
chmod +x adds the execute permission bit for all users on the specified file.
Question 7: What does the command sort -r file.txt do?
- Removes duplicate lines from file.txt
- Sorts file.txt alphabetically in ascending order
- Sorts file.txt in reverse (descending) order (Correct answer)
- Sorts file.txt by file size
Correct answer: Sorts file.txt in reverse (descending) order
sort -r reverses the default sort order, displaying lines from Z to A (or largest to smallest).
What does the command tail -f /var/log/syslog do?