-1

I have created a script which should delete the directories and it's contents older then 2 days. The script is working fine but the directory is not getting deleted. Kindly help, it will be much appreciated.

Script details:

export BACKUP_SETS=/backupset/
if [ ! -d "$BACKUP_SETS" ]; then
  # Control will enter here if $DIRECTORY doesn't exist.
  printf "\nFailure when removing BACKUP_SETS, Exiting \n"
  exit 1
 else
        two_days=$(date -d "2 days ago" +%Y_%b_%d)
                for f in $BACKUP_SETS[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]; do
                [ -d "$f" ] || continue
                (( $f < $two_days )) && echo rm -r "$f"
                done
       printf "\n BACKUP_SETS was removed successfully.\n"

fi

The directory contains folder as mentioned below.

[oracle@ backupset]$ ls -lrt
total 12
drwxr-xr-x 2 oracle dba 12288 Jul 23 18:32 2018_Aug_23

Output of the script:

[oracle@ backupset]$ ./backupset.sh

 BACKUP_SETS was removed successfully.
  • 2
    You’re echoing the rm command instead of calling it. – Jeff Schaller Aug 29 '18 at 12:18
  • @JeffSchaller there's no rm -f ... written in the sample run, though – roaima Aug 29 '18 at 12:20
  • That’s a very good point; the script doesn’t match the output... – Jeff Schaller Aug 29 '18 at 12:20
  • 1
    Also very funny: beginning of february,, you will remove files that are recent too, since "2019 Feb ..." is smaller than "2019 Jan ...". – Gerard H. Pille Aug 29 '18 at 12:22
3

Your $BACKUP_SETS[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] wildcard does not match your directory name 2018_Aug_23.

You should name your directory 20180823 instead, then it will be seen by your script. Also, named months do not compare correctly (Apr < Mar).

To make the directory name better readable, you could name your directories like 2018-08-23 and use the wildcard $BACKUP_SETS[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].

1

As mentioned in a comment, you cannot use < to compare month names:

(( '2018-Aug-29' < '2018-Feb-28' )) && echo 'Uh oh, August is before February' || echo 'Sanity prevails'
Uh oh, August is before February

If you want to compare date strings lexically I would suggest you use YYYY-MM-DD format.

Better though, might be to use find to delete older directories regardless of the naming convention:

find "$BACKUP_SETS" -mindepth 1 -maxdepth 1 -type d -mtime +2 -print -exec echo rm -rf {} \;

Needless to say, remove the echo when you want it to run for real.

  • I wanted that my script should run, but i got the concept why it is not deleting the directory. Thanks everyone for the quick reply. – James Aug 29 '18 at 13:42
1
find "$BACKUP_SETS" -type f -mtime +2 -exec rm {} \;
find "$BACKUP_SETS" -type d -mindepth 1 -mtime +2 -exec rmdir {} \;

The directories will be removed a couple of days later than their latest file.

  • @roaima Don't know if the OP needs it, but this find will not try to remove a higher directory in which a lower one has recently been removed. There will be errors when trying to remove a directory with subs when nothing was changed for two days (unlikely), but no harm will be done. – Gerard H. Pille Aug 29 '18 at 12:36
  • And the -depth qualifier will stop it generating those errors. As you say, no harm done, though. – roaima Aug 29 '18 at 12:39
  • You think so? You should try it, replacing mtime by mmin (unless you're very patient).. – Gerard H. Pille Aug 29 '18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.