Script with Dates

Hi from Uruguay.

Im having a problem with a scripts using dates, this is the problem:

I have a folder for each day, like : 20160711 for yesterday, 20160712 for today, and i want to mv to a backup folder the folders who exceed the year of antiquity (365 days from today) and that script execute every day on my server.

Now, i dont know how to compare every folder with the date from 1 year ago

date1yearago=$(date --date="1 year ago" +%Y%m%d)

this command give me in STRING the date of 1 year ago from today no ?

How i can compare that with every folder in my principal folder?

Sorry for my english

Cya and thannks for the help.

Piero.

Try

find /path/to/dirtree -type d -mtime +365

to get a list of your outdated directories.

Thanxs for the answer but i cant use find because i need to evaluate the folders by the name, not by the modificated or accesed times.

The following is untested, but should come close to what you're trying to do:

#!/bin/ksh
date1yearago=$(date --date="1 year ago" +%Y%m%d)
cd /path/to/principal_directory
ls [12][0-9][0-9][0-9][01][0-9][0-3][0-9] | while read dir
do	[ $((dir > date1yearago)) -eq 1 ] && break
	mv $dir /path/to/backup/directory
done

This code assumes that you're using a shell that uses the syntax specified by the POSIX standards for shell variable assignment and arithmetic expansions (such as [ksh , bash , ash , zsh , etc.; but not a pure Bourne shell and not a derivative of csh ). Obviously, you'll have to change the directory names shown in red to be absolute pathnames to your principal directory and your backup directory, respectively.

Note also that as written, this script will stop working in a little over 883 years. And, it assumes that you don't have any directories created before the year 1000.