0
0
Bash Scriptingscripting~10 mins

Log rotation script in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A/home/user/data.txt
B/etc/passwd
C/var/log/syslog
D/tmp/tempfile
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.
2fill in blank
medium

Complete 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'
A$(date +%Y%m%d)
B$(whoami)
C$(pwd)
D$(hostname)
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that do not produce a date.
Forgetting the $ and parentheses around the command.
3fill in blank
hard

Fix 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'
A/var/log/syslog-$(date +%Y%m%d)
B/var/log/syslog-$(date +%Y%m%d).gz
C/var/log/syslog
D/var/log/syslog-20230101
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.
4fill in blank
hard

Fill 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'
A/var/log/syslog
B%H%M%S
C%Y%m%d
D/var/log/auth.log
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.
5fill in blank
hard

Fill 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'
A/var/log/syslog
B%Y%m%d
C7
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong date format causing file name mismatches.
Deleting files older than wrong number of days.