Recursiveli delete in Bash Script
I have created a little BASH script to do some auto backing up (well really two scripts - one interactive and one automatic for crontabs) my $HOME and /etc directories to an external drive.
The script creates a new folder everday for each backup naming it homedir-date or etc-date respectively. That way I have multiple backups to refer to in case of an accident. What I am looking to do is add some code that will check for directories TODAY - xdays and rm -rf any directories older than that output. I am not really familiar with BASH scripting and I am pretty sure this qualifies as a noob question, but can anyone help me out with code for that? Would adding Code:
find $BASEDIR/home-backups/ -type d -mtime +7 -exec rm -rf {} \; Just so you can see, I am providing the interactive version of the script below. The only real difference in the two scripts is removal of the read -p's and replacing them with echos that are logged instead. Thanks for any help. Code:
BASEDIR=/media/brain |
I recommend you reading some manuals and also not letting untested code to remove anything. What you did wrong (look in the 'man find' for details) is that find is recursive. You need to specify -maxdepth 1, or it will can be able to kill one-day-old backup of a year-old directory.
|
Yea, I was reading man pages, etc and added a -maxdepth 0 to the line as such:
Code:
find $BASEDIR/home-backups/ -maxdepth 0 -type d -mtime +7 -exec rm -rf {} \; I changed the structure a little bit so that it actually does $BASEDIR/home-backups/$DATE/$LOC2 and the find should delete everything at the $DATE level older than 7 days, correct? A maxdepth of 1 would delete everything under $DATE but leave $DATE directory intact, correct? As to "not letting untested code to remove anything," I understand. I am running the script in test directories right now till I get everything working and I am not relying on this script for the backups. I have an older, much simpler variant that I am using to do the backup to a different location. |
All times are GMT -5. The time now is 01:44 AM. |