diff --git a/Anjani Nandan/Assignment_1/Question_1/README.md b/Anjani Nandan/Assignment_1/Question_1/README.md new file mode 100644 index 0000000..40b209f --- /dev/null +++ b/Anjani Nandan/Assignment_1/Question_1/README.md @@ -0,0 +1,29 @@ +# Assignment 1 - Log Cleanup Script + +## 🧠 Problem Statement +As a developer, I often accumulate a lot of temporary `.log` files in my `~/projects` directory. Manually deleting these files is tedious and error-prone. I want a simple Bash script that automatically finds and deletes all `.log` files older than 7 days, keeping my workspace clean and saving disk space. + +--- + +## 💻 Solution + +I created a Bash script called `clean_logs.sh` that: + +- Searches for `.log` files older than 7 days in the target directory +- Deletes them automatically +- Can be scheduled to run daily using a cron job + +### ✅ Script Contents +```bash +#!/bin/bash + +TARGET_DIR="/DSG" + +find "$TARGET_DIR" -type f -name "*.log" -mtime +7 -print -delete + +echo "Deleted .log files older than 7 days in $TARGET_DIR" + + +![Script Output](https://github.com/anjaninandan001/open-lecture-assignments-y25/raw/main/Anjani%20Nandan/Assignment_1/Question_1/Screenshot%202025-06-03%20121407.png) + + diff --git a/Anjani Nandan/Assignment_1/Question_1/Screenshot 2025-06-03 121407.png b/Anjani Nandan/Assignment_1/Question_1/Screenshot 2025-06-03 121407.png new file mode 100644 index 0000000..fece0b0 Binary files /dev/null and b/Anjani Nandan/Assignment_1/Question_1/Screenshot 2025-06-03 121407.png differ diff --git a/Anjani Nandan/Assignment_1/Question_1/script.sh b/Anjani Nandan/Assignment_1/Question_1/script.sh new file mode 100644 index 0000000..38581a0 --- /dev/null +++ b/Anjani Nandan/Assignment_1/Question_1/script.sh @@ -0,0 +1,21 @@ +#Created a Bash Script for Log Cleanup +#!/bin/bash + +TARGET_DIR="/DSG" + +find "$TARGET_DIR" -type f -name "*.log" -mtime +7 -print -delete + +echo "Deleted .log files older than 7 days in $TARGET_DIR" + + +# Made the Script Executable +chmod +x /root/clean_logs.sh + +#Tested the Script +bash /root/clean_logs.sh + +#Set Up a Cron Job for Automation +crontab -e +0 8 * * * /root/clean_logs.sh >> /root/clean_logs.log 2>&1 +sudo service cron start +