To delete directories older than n days using a Bash script, you can use a combination of find and rm commands. Here's a step-by-step guide on how to achieve this:
#!/bin/bash # Directory to search for old directories DIRECTORY="/path/to/parent/directory" # Number of days DAYS=7 # Find directories older than specified days and delete them find $DIRECTORY -type d -mtime +$DAYS -exec rm -r {} + Variables:
DIRECTORY: Specify the directory path where you want to search for and delete old directories. Replace "/path/to/parent/directory" with your actual directory path.DAYS: Specify the number of days. Directories older than this number of days will be deleted. Change 7 to your desired number of days.find Command:
find $DIRECTORY -type d -mtime +$DAYS: Finds directories (-type d) within $DIRECTORY that are modified more than $DAYS days ago (-mtime +$DAYS).-exec rm -r {} +: Executes rm -r (recursive remove) command on each found directory ({}), recursively deleting its contents and the directory itself.find command without -exec rm -r {} + to verify which directories will be deleted.This script provides a straightforward way to delete directories older than a specified number of days using Bash and the find command. Adjust the DIRECTORY and DAYS variables as per your specific requirements and environment.
Bash script to delete directories older than n days
find command.#!/bin/bash find /path/to/directories -type d -mtime +n -exec rm -rf {} \; Replace /path/to/directories with the actual directory path and n with the number of days.Shell script to delete files and directories older than n days
#!/bin/bash find /path/to/location -mtime +n -exec rm -rf {} \; Replace /path/to/location with the target directory path and n with the number of days.Automating directory cleanup with a bash script
#!/bin/bash find /path/to/directories -type d -mtime +n -exec rm -rf {} \; # Add the script to cron for daily cleanup crontab -e # Add the following line to the crontab file 0 2 * * * /path/to/your_script.sh Replace /path/to/directories and n as needed, and save the script at /path/to/your_script.sh.Delete empty directories older than n days using bash
#!/bin/bash find /path/to/directories -type d -empty -mtime +n -exec rm -rf {} \; Replace /path/to/directories and n as needed.Bash script to remove old backup directories
#!/bin/bash BACKUP_DIR="/path/to/backups" DAYS=30 find "$BACKUP_DIR" -type d -mtime +$DAYS -exec rm -rf {} \; Replace /path/to/backups and DAYS as needed.Removing old log directories with a bash script
#!/bin/bash LOG_DIR="/path/to/logs" DAYS=7 find "$LOG_DIR" -type d -mtime +$DAYS -exec rm -rf {} \; Replace /path/to/logs and DAYS as needed.Delete user directories older than n days
#!/bin/bash USER_DIR="/home/user/specific_directories" DAYS=15 find "$USER_DIR" -type d -mtime +$DAYS -exec rm -rf {} \; Replace /home/user/specific_directories and DAYS as needed.Cleanup temporary directories older than n days
#!/bin/bash TEMP_DIR="/path/to/temp" DAYS=3 find "$TEMP_DIR" -type d -mtime +$DAYS -exec rm -rf {} \; Replace /path/to/temp and DAYS as needed.Delete directories recursively older than n days
#!/bin/bash ROOT_DIR="/path/to/root" DAYS=20 find "$ROOT_DIR" -type d -mtime +$DAYS -exec rm -rf {} \; Replace /path/to/root and DAYS as needed.Bash script to delete old directories with logging
#!/bin/bash DIR="/path/to/directories" DAYS=10 LOGFILE="/path/to/logfile.log" find "$DIR" -type d -mtime +$DAYS -exec rm -rf {} \; -exec echo "$(date): Deleted {}" >> "$LOGFILE" \; Replace /path/to/directories, DAYS, and /path/to/logfile.log as needed.event-bubbling preact shopify unity-container windows-phone-8 npm-start angular-router-guards system.net static database-connection