File Cleanup Utility Bash Script

marius

Administrator
Staff member
Introduction:
This script automates cleaning up old files in a directory that have not been accessed for a specified number of days.

Code Snippet:
Bash:
#!/bin/bash
# File Cleanup Utility
if [ -z "$1" ] || [ -z "$2" ]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

find "$1" -type f -atime +$2 -exec rm {} \;
echo "Old files removed from $1"

Instructions:
Execute the script by providing the target directory and days threshold:
Code:
chmod +x file_cleanup.sh
./file_cleanup.sh /tmp 30

Example Output:
Code:
Old files removed from /tmp
 
Back
Top