[TriLUG] Some script help

William Sutton via TriLUG trilug at trilug.org
Fri May 8 10:44:30 EDT 2015


Someone once said that if you have a problem and you decide to use regex, 
you now have two problems (and I say that as a Perl guy with lots of regex 
experience).

Might I suggest another approach:  chop the filename up with awk and 
arrange it according to YYYYMMDD, which will sort numerically.  Pull 
today's date in the same format, and your numeric comparison will work 
fine.

William Sutton

On Fri, 8 May 2015, Brian Blater via TriLUG wrote:

> I'm trying to get a basic bash script put together to copy some backup
> files from one location to another. I've got the basics done, I think, but
> one section could use some regex or similar and my regex abilities are bad.
>
> Here is the script:
>
> #!/bin/bash
>
> ## Establish DATE ##
> NOW=$(date +"%F")
> DELDATE=$(date --date="7 days ago" +"%F")
>
> ## Remote Server ##
> ## RSERVER="servername"
> RPATH="/path/to/remote/dir/"
>
> ## Local Path ##
> LPATH="/path/to/local/dir/"
>
> ## Copy files ##
> for i in $( ls $RPATH ); do
> ## some regex or ? to get only the date from file        ##
> ## assign that to $FILE                                  ##
> ## file looks like: backup_2014-06-13_7-00-07_14.1R1.tgz ##
> ## only want to compare the 2014-06-13 of file with $NOW ##
>
> ## if it is today's file copy it ##
> if [ "$FILE" = "$NOW" ]; then
> cp $i $LPATH
> else
> ## if it is older than 7 days mv it ##
> if [ "$FILE" < "$DELDATE" ]; then
> mv $i $LPATH
> fi
> fi
> done
>
> For now, my issue is in the Copy Files section. As the note says I need to
> look at each file and compare just the date portion. If it matches $NOW I
> want to copy it.
>
> The other concern is the next IF in that section. I basically want to leave
> a weeks worth of files in that directory before copying them to remote
> storage. Can I just do a < there, or do I need some more magic to do that
> comparison?
>
> Thank you for your help.
> Brian
> -- 
> This message was sent to: William <william at trilug.org>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web	: http://www.trilug.org/mailman/options/trilug/william%40trilug.org
> Welcome to TriLUG: http://trilug.org/welcome


More information about the TriLUG mailing list