exam questions

Exam XK0-005 All Questions

View all questions & answers for the XK0-005 exam

Exam XK0-005 topic 1 question 7 discussion

Actual exam question from CompTIA's XK0-005
Question #: 7
Topic #: 1
[All XK0-005 Questions]

DRAG DROP -
As a Systems Administrator, to reduce disk space, you were tasked to create a shell script that does the following:
Add relevant content to /tmp/script.sh, so that it finds and compresses related files in /var/log without recursion.
INSTRUCTIONS:
Drag and drop snippets to fill the blanks to build a script that performs the actual compression of rotated log files.
If at any time you would like to bring back the initial state of the simulation, please click the Reset All button.

Show Suggested Answer Hide Answer
Suggested Answer:

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
linux_admin
Highly Voted 1 year, 8 months ago
The command "grep ".log[1-6]$" is a regular expression that is used to search for files with a ".log1" to ".log6" extension. The grep command is used to search for text patterns in files, and the regular expression provided as an argument is used to specify the pattern to search for. The regular expression ".log[1-6]$" matches any string that ends with ".log1", ".log2", ".log3", ".log4", ".log5", or ".log6". The . character matches any single character, the log matches the characters "log", and the [1-6] matches any single digit in the range 1 to 6. The $ symbol matches the end of the string, ensuring that the match only occurs if the ".log[1-6]" pattern is at the end of the string.
upvoted 5 times
...
TheMichael
Most Recent 2 months ago
So the question is asking to compress individual rotating log files without recursion which sounds confusing, but it really just means it wants you to compress any file found in the log directory and no other directories within it.
upvoted 1 times
TheMichael
2 months ago
They then write -maxdepth 1 which fulfills the "without recursion" portion of the question for you, so you can really just focus on just compressing the files in the /var/ log directory. So if you search (grep) for "$log.[1-6]$" you are searching for a variable called log... which isn't a variable that exists in our code. If you search for "log.[1-6]$" you are only searching for the files that end in log.1 log.2 log.3... to log.6. Obviously you want to search for any file in the directory, so we use "$1" to be able to search for the first thing we write after executing the code. "$6" would be searching for the 6th word we input after putting in the code, which doesn't really make sense to do when we can just search for the first term after typing the code in to execute it:
upvoted 1 times
TheMichael
2 months ago
ex: script.sh yes no maybe so today tomorrow would search for the first term out of the options yes no maybe so today tomorrow. Thus we would search for yes if this were to be entered exactly like this. "$6" would search for the 6th term so that would be searching for tomorrow if entered this way. the "for filename in" part is saying that we are creating a variable called filename, and FOR the variable we are putting IN this word: we then write a cat command to make the filename variable be every word in the /tmp/ tempfile that we wrote the output of our grep search earlier to.
upvoted 1 times
TheMichael
2 months ago
The final do gzip command then just reads the variable we created called filename and compresses it for each term that it changes to from our for filename in command. This creates a single zipped file for each term found that matches our search within /var/ log, which is essentially known as compressing rotated log files without recursion aka we gzipped files we searched for only in the /var/ log directory and no additional directories within that. since the wording could be interpreted as finding specifically .log files within the /var/ log directory, the "$1" command allows us to specifically search for only those files if we so choose. The log.[1-6] option restricts us to only the files that end in log.[1-6] which wouldn't allow us to compress a file if it was log.11 or log.7, which doesn't make sense to do as the question doesn't ask us to specifically look for log files between 1 and 6, but rather log files in general.
upvoted 1 times
TheMichael
2 months ago
#!/bin/bash #name: script.sh find /var/log -type f -maxdepth 1 | grep "$1" > /tmp/tempfile for filename in $(cat /tmp/tempfile) do gzip $filename done Is the final answer.
upvoted 2 times
...
...
...
...
...
DRVision
10 months, 3 weeks ago
#!/bin/bash #name: script.sh find /var/log -type f -maxdepth 1 | grep "$1" > /tmp/tempfile for filename in $(cat /tmp/tempfile) do gzip $filename done The $1 takes the first command line argument so it allows you to search what you're actually looking for through the logs, i.e. "related files". Otherwise,even if the syntax was a typo for "log.[1-6]$", because it should be ".log[1-6]$", all you're doing is searching for logs, and it doesn't allow you to specify what you're looking
upvoted 2 times
DRVision
10 months, 3 weeks ago
thus allowing you to run the script and provide the search term into $1 via command line
upvoted 2 times
...
...
Damon54
1 year ago
#!/bin/bash #name: script.sh find /var/log -type f -maxdepth 1 | grep "$1" > /tmp/tempfile for filename in $(cat /tmp/tempfile) do gzip $filename done
upvoted 2 times
...
Alizadeh
1 year, 2 months ago
#!/bin/bash #name: script.sh find /var/log -type f -maxdepth 1 | grep "log.[1-6]$" > /tmp/tempfile for filename in $(cat /tmp/tempfile); do gzip $filename done
upvoted 1 times
...
linux_admin
1 year, 8 months ago
#!/bin/bash # Find files in /var/log directory without recursion find /var/log -type f -maxdepth 1 | grep ".log[1-6]$" > /tmp/tempfile # Loop through each file in /tmp/tempfile for filename in $(cat /tmp/tempfile) do # Compress the file using gzip gzip $filename done
upvoted 2 times
linux_admin
1 year, 8 months ago
1. Finds files in the /var/log directory without recursion: The find command is used to search for files in the /var/log directory. The -type f option limits the search to regular files, and the -maxdepth 1 option specifies that the search should not go deeper than the first level of subdirectories. The grep ".log.[1-6]$" option filters the results to only include files with a ".log.1" to ".log.6" extension. The filtered results are redirected to /tmp/tempfile for processing. 2. Loops through each file in /tmp/tempfile: The for loop is used to iterate through each file listed in /tmp/tempfile. The $filename variable holds the name of the current file in each iteration of the loop. 3. Compresses each file using gzip: The gzip command is used to compress each file. The file name is passed as an argument to the gzip command, and the compressed file will have a .gz extension added to its original name.
upvoted 3 times
linux_admin
1 year, 8 months ago
The script will compress all files with the ".log.1" to ".log.6" extensions in the /var/log directory, and it will not go deeper than the first level of subdirectories. The compressed files will be stored in the same directory as the original files.
upvoted 1 times
Mr_Marcus
1 year, 8 months ago
Which is all fabulous, except you missed several critical points in your analysis. First, the question does not ask for files with "log" in the name (it doesn't even ask for log files - it says "related files"). Second, some of the files in /var/log do not have log or .log anywhere in the filename (e.g. messages, secure, spooler, etc.) and would be missed using your syntax. Third, ".log[1-6]$", with a leading dot (.), is not even an option to select. Your choices are "log.[1-6]$" or "$log.[1-6]$", without a leading dot. Your version will (likely) create an empty tempfile and compress nothing. And, yes, I've tested your solution (and mine). The correct answer is: #!/bin/bash #name: script.sh find /var/log -type f -maxdepth 1 | grep "$1" > /tmp/tempfile for filename in $(cat /tmp/tempfile) do gzip $filename done
upvoted 7 times
nabalauski
10 months, 1 week ago
after doing some research myself I can say with confidence that Mr_Marcus is correct here
upvoted 2 times
...
...
...
...
...
Lwarder1
1 year, 9 months ago
Why is it not grep "$1" > /temp/tempfile
upvoted 1 times
...
TheRealManish
1 year, 10 months ago
I can't figure out why we are grepping for "log[1-6]$" i dont see anything in the qusetion about 1-6.. but if we grep for "$log" we get all of the log files..
upvoted 1 times
CodeMaestro
1 year, 6 months ago
For that we have a $log and not log
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago