Skip to content
Open
29 changes: 29 additions & 0 deletions Anjani Nandan/Assignment_1/Question_1/README.md
Original file line number Diff line number Diff line change
@@ -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)


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions Anjani Nandan/Assignment_1/Question_1/script.sh
Original file line number Diff line number Diff line change
@@ -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