Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-05-2008, 08:55 PM   #1
LQ Newbie
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
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
find $BASEDIR/home-backups/ -type d -mtime +7 -exec rm -rf {} \;
at the appropriate point work?

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.

DATE=$(date +%G-%m-%d)

#TEST for presence of drive/dir
if test -d "$BASEDIR"; then
	#1 If $LOC2$DATE exists delete it
	  if test -d "$BASEDIR/home-backups/$LOC2$DATE"; then
		 read -p "Delete Old Home Backup for $DATE - $LOC2$DATE? <y/n> "
		 if [ "$REPLY" == "y" ]; then
			rm -rf $BASEDIR/home-backups/$LOC2$DATE
			read -p "Old Home Backup for $DATE Deleted! Continue Backup? <y/n> "   
				 if [ "$REPLY" == "n" ]; then
					 echo "Backup Aborted.  Exiting..."
					 exit 1

			 echo "Directory Kept Intact. Exiting..."

						echo "Creating Backup Home Directory for $DATE"
				 # VERBOSE MODE cp -PRuv $HOME /media/brain
				 #cp -a same as cp -dpPR (preserve attrib, no follow symlinks, recursive)
				 cp -a $HOME $BASEDIR/home-backups/$LOC2$DATE
				 echo "Done Backing Up Home Directory to $BASEDIR/$LOC2$DATE!!"
	  echo "Directory $BASEDIR not present.  Is the Drive Plugged in and mounted?" 
Old 02-07-2008, 02:22 PM   #2
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (
Posts: 1,900

Rep: Reputation: 68
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.
Old 02-08-2008, 09:47 AM   #3
LQ Newbie
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Yea, I was reading man pages, etc and added a -maxdepth 0 to the line as such:

find $BASEDIR/home-backups/ -maxdepth 0 -type d -mtime +7 -exec rm -rf {} \;
that way it would erase the first level under $BASEDIR/home-backups/ which is what I wanted.

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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 03:16 AM
Why I am not figuring the bash script? acwbrat Programming 1 11-02-2007 01:37 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
delete all .bash* swmok Linux - Software 2 12-01-2003 09:17 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration