Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the log file exists.
Bash Scripting
if [ -f [1] ]; then echo "Log file found." fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a directory path instead of a file path.
Checking a file that does not exist on the system.
✗ Incorrect
The -f option checks if the file exists and is a regular file. The log file is usually /var/log/syslog.
2fill in blank
mediumComplete the code to rename the current log file by adding a date suffix.
Bash Scripting
mv /var/log/syslog /var/log/syslog-[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that do not produce a date.
Forgetting the $ and parentheses around the command.
✗ Incorrect
Using $(date +%Y%m%d) appends the current date in YYYYMMDD format to the log file name.
3fill in blank
hardFix the error in the code to compress the rotated log file using gzip.
Bash Scripting
gzip [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to gzip the current log file which is still in use.
Trying to gzip a file that already has .gz extension.
✗ Incorrect
You must gzip the actual rotated log file with the date suffix, not the current log or the compressed file.
4fill in blank
hardFill both blanks to create a function that rotates and compresses a log file.
Bash Scripting
rotate_log() {
local logfile=[1]
mv "$logfile" "$logfile-$(date +[2])"
gzip "$logfile-$(date +[2])"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect date format that does not match the gzip command.
Using a different log file path inconsistent with the rest of the script.
✗ Incorrect
The function uses /var/log/syslog as the log file and %Y%m%d as the date format for the suffix.
5fill in blank
hardFill all three blanks to create a script that rotates, compresses, and deletes logs older than 7 days.
Bash Scripting
#!/bin/bash logfile=[1] rotate_date=$(date +[2]) mv "$logfile" "$logfile-$rotate_date" gzip "$logfile-$rotate_date" find /var/log -name "syslog-*" -mtime +[3] -delete
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong date format causing file name mismatches.
Deleting files older than wrong number of days.
✗ Incorrect
The script uses /var/log/syslog as the log file, %Y%m%d for date format, and deletes logs older than 7 days.