Want to mv files form one folder to another by date

Hi All

Looking for some help here I want to query a file and mv all files that are older than seven days to an archive folder.

the archive folder already exists but I am having trouble with the date comparison.

list file is as follows

eg

dd2302071334.txt
dd2302071358.txt
dd2303061655.txt
dd2306061111.txt
dd2308061147.txt
dd2308061422.txt
dd2308061427.txt
dd2308071447.txt
dd2308071546.txt
dd2401081059.txt
dd2401081131.txt
dd2405061224.txt
dd2405071055.txt
dd2405071448.txt

I am trying to mv everything older than seven days, this will be run on a cron every night.

Cheers in advance

SummitElse

ls | {

while read x;
do
y = $(echo $x | cut -c3-8)

need to compare the output with the computer date

and if older than seven days move it as below

if [! -f $y];
then
echo "moving $x to $y" >> $ARCHIVE
mv $x $y 2>> $ARCHIVE
fi
done
}

any help appreciated.

find . -mtime +7 -exec mv {} dest_folder \;

Just the find command with its built-in exec function.

The {} stands in for whatever results come from find, and you have to use the \; to terminate the command.

Just a quick query does that work on the datestamp of the file and not the actual date format in the file name, I have created the copies of these files from one server to the other so the datestamp will be the same on all of them

GNUawk

awk  'BEGIN { 
 ## you do the maths...this may not be correct. calculate how many secs in 7 days
 onehr=60
 onemin=60
 oneday = ( onehr * onemin ) * 24
 sevendays = oneday * 7  
}
 day=substr($0,3,2)
 month=substr($0,5,2)
 year=substr($0,7,2)
 hr=substr($0,9,2)
 min=substr($0,11,2)
 t="20"year" "month" "day" "hr" "min" "00
 d=mktime(t)
 now=systime()
 if ( ( now - d ) >= sevendays ) {
  print $0
 }
}' file

Try this code:

#!/bin/ksh

GetDate()
{
# Exemple: GetDate -1 '+%Y%m%d'

typeset -i nDays=$1; format=$2
eval $(echo $TZ | sed '
s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
TZ=$zon1$((localOffset-24*nDays))$zon2 date $format
}

liste=$(ls dd*.txt)

ndate=$(GetDate -7 '+%y%m%d')

for i in $liste
do
        # Get the file date in YYMMDD format

        ydate=$(echo $i | cut -c7-8)
        mdate=$(echo $i | cut -c5-6)
        ddate=$(echo $i | cut -c3-4)

        zdate=${ydate}${mdate}${ddate}

        if [ $zdate -lt $ndate ]
        then
                echo "$i: file to move"
        else
                echo "$i: do not move"
        fi
done

Good luck ! :D:b:

That worked a treat mate you truly are a scripting guru, thanks again mate. think I'll need to study up on SED

But getting the list of files into a variable with an ls is not really the right way to do it.

for i in dd*.txt