# Help with date in shell script

I wrote a brief bash shell script (one line) to archive a tarball of a specific directory tree:

cd /opt/GeneWeb/bases && tar cf - "Behne.gwb" | gzip -f9 > "/opt/GeneWeb/bases/BACKUPS/date +%d-%m-%Y-Behne.gwb.tar.gz"

This creates a tarball named like this: 16-08-2017-Behne.gwb.tar.gz

I use a cron job to execute this code once a day. It works fine, but I now want to test for tarballs which are eight days or older, and automatically delete them. That way I'll have snapshots of only the last seven days. The problem is that I have no idea how to accomplish this. Can anyone help with writing this script? TIA!

edit retag close merge delete

Sort by » oldest newest most voted

The easy way is to use the find command with the -mtime option

find /opt/GeneWeb/bases/BACKUPS/ -name '*Behne.gwb.tar.gz' -mtime +6 -exec ls -dl {} +


When you are satisfied that this produces a list of files you really want to delete, the run this:

find /opt/GeneWeb/bases/BACKUPS/ -name '*Behne.gwb.tar.gz' -mtime +6 -exec rm {} +


The -mtime +6 option select files modified more that 6 days ago, that is 7 or more days.

more

Beautiful solution! Thanks! Looks like it's working perfect, so I modified my script like this:

cd /opt/GeneWeb/bases &&\ tar cf - "Behne.gwb" | gzip -f9 > "/opt/GeneWeb/bases/BACKUPS/date +%d-%m-%Y-Behne.gwb.tar.gz" &&\ find /opt/GeneWeb/bases/BACKUPS/ -name '*Behne.gwb.tar.gz' -mtime +6 -exec rm {} +

The reason I'm tacking it on the end after the &&\ is so that if something causes my data directory to disappear again (thus failing to make any more backups) this script will simply stop deleting files until after I restore the directory. I'll have to study find more, it's very handy.

( 2017-08-16 12:41:54 -0500 )edit

What is the purpose of backslash after the double ampersand? Is that perhaps the result of the forum messing with your text?

( 2017-08-16 14:21:15 -0500 )edit

The double ampersand causes the remaining commands following it to be executed only if the preceding command ended successfully. The backslash is a delimiter for the carriage return: it allows you to continue a long string of commands on the next line without the bash processor acting on the carriage return.

#! /bin/bash
Command 1 && \
command 2 && \
last command

( 2017-08-17 04:38:38 -0500 )edit

# ! /bin/bash

Continuation without backslash will also work when the line ends with a logical operator.

#! /bin/bash
Command 1 &&
command 2 &&
last command


What I wondered was you had the backslash before space and followed by additional commands.

cd /opt/GeneWeb/bases &&\ tar cf - "Behne.gwb"

( 2017-08-17 04:51:52 -0500 )edit

It's because I'm still learning the peculiarities of this forum: I wasn't using the html pre /pre code, so the forum software stripped out the line feeds. I use the backslash even after operators as a matter of habit. Bad habit perhaps? ;-)

( 2017-08-17 05:06:37 -0500 )edit

Not sure what language or system you want to use to do this, like in bash again, or C, or what, but it seems like if you can get the date of the file, perhaps from its properties or by taking it from the filename that you can use it to do a standard sort of 'date arithmetic'. You'll also need to get the current system date programmatically.

With these two items in hand (probably assigned to variables), maybe this page on methods will help, https://unix.stackexchange.com/questi...

more