I need to keep files 4 months from now (3 previous month + 1 current month) in directory and tar the rest. But the problem is my files consist of different format.StockList_03-01-2015.txtRef_01-02-2016.txtData_2015-07-20.txtTest_2016-01-13.txtAs the above example, the date is in either dd-mm-yyyy or yyyy-mm-dd format, i need to extract the date from filename and compare if it is not in the (3 previous months + 1 current month) range, then will tar it.Today is 2nd of March, files from 1st of Dec 2015 until today will remain, but will tar the others.Expected tar files from example: StockList_03-01-2015.txtData_2015-07-20.txtI can write the regex for these 2 types date format, but i don't know how to combine them.Regex for yyyy-mm-dd:ls |grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'   Regex for dd-mm-yyyy:ls |grep -Eo '[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{4}'Compare date:files=($(ls | awk -v d=$(date -d '3 months ago' +%m-%Y.log) 'DATE_FROM_FILES< d {print;}' ))tar cvzf archive.tar.gz ${files[@]}
Compare and tar file for 2 date formats from filename
shell script;files;tar