010-160 - LPI Linux Essentials Searching and Extracting Data Questions and Answers — Questions and Answers
Question 1: A system administrator needs to find all files in the `/var/log` directory that have been modified in the last 7 days. Which of the following commands will accomplish this task?
- find /var/log -mtime -7 (Correct answer)
- grep -r -mtime 7 /var/log
- find /var/log --modified 7
- search /var/log -time 7
Correct answer: find /var/log -mtime -7
The `find` command is used to search for files and directories. The `/var/log` argument specifies the starting directory for the search. The `-mtime` option filters files based on their last modification time in days, and `-7` specifically looks for files modified within the last 7 days.
Question 2: Which command is used to display the first 10 lines of a text file named `report.txt`?
- cat report.txt | top -n 10
- less -n 10 report.txt
- head report.txt (Correct answer)
- tail report.txt
Correct answer: head report.txt
The `head` command, by default, displays the first 10 lines of a specified file. The `tail` command shows the last lines, `cat` displays the entire file, and `less` is a pager for viewing files page by page.
Question 3: A user wants to search for the case-insensitive string "error" within the `system.log` file. Which of the following commands is the correct way to perform this search?
- find "error" system.log
- grep -i "error" system.log (Correct answer)
- search --ignore-case "error" system.log
- cat system.log | filter -i "error"
Correct answer: grep -i "error" system.log
The `grep` command is the standard tool for searching text using patterns. The `-i` option makes the search case-insensitive, so it will match "error", "Error", "ERROR", etc.
Question 4: You have a comma-separated value (CSV) file named `data.csv` and you need to extract only the third column. Which command will achieve this?
- grep -c 3 data.csv
- head -f 3 data.csv
- split -c 3 data.csv
- cut -d ',' -f 3 data.csv (Correct answer)
Correct answer: cut -d ',' -f 3 data.csv
The `cut` command is used to remove sections from each line of files. The `-d ','` option specifies that the delimiter is a comma, and `-f 3` specifies that the third field (column) should be extracted.
Question 5: To count the number of lines containing the word "warning" in the file `application.log`, which command should be used?
- grep -c "warning" application.log (Correct answer)
- wc "warning" application.log
- grep -n "warning" application.log
- count "warning" < application.log
Correct answer: grep -c "warning" application.log
While `grep` is used for searching, the `-c` option modifies its behavior to output a count of matching lines instead of the lines themselves. `wc -l` would count all lines, and `grep -n` would show line numbers with the matches.
Question 6: A user needs to combine two commands. They want to list all files in the current directory and then use the output of that command to search for files containing the word "config". Which character should be used to connect the two commands?
- >
- &&
- | (Correct answer)
- <
Correct answer: |
The pipe `|` symbol is used to create a pipeline between commands. It takes the standard output of the command on its left (e.g., `ls`) and uses it as the standard input for the command on its right (e.g., `grep config`).
A system administrator needs to find all files in the `/var/log` directory that have been modified in the last 7 days.
Which of the following commands will accomplish this task?