How to move files from a directory which falls between Date Range?

Hi All,

I am trying to to move files from a directory to another which falls from Current day - 7 days. The files are in zipped format with dates appended on it.

Can you pls help me as this came as a immediate change before the production Release planned next week.

Pls let me know if you have any questions.

Thanks,
Freddie

in what format the files have date. Also which OS and shell your are using

do the dates reflect the file mtime? If so

touch -t 201205160000 /tmp/dummy
cd /old_directory
find . -type f -name '*zip*'  -newer  /tmp/dummy |
while read fname 
do
#test
  echo " $fname will become /new_directory/$fname
#real
#  mv $fname  /new_directory/$fname
done

See the #real part? run the code as is to verify if this is what you want. It is harmless. Do this FIRST.

If OK: Then edit the script and remove the # in front of the word mv

find -type f '!' -mtime +7 /path/to/folder | while read FILE
do
        echo mv "$FILE" /path/to/dest/
done

Remove the 'echo' once you've tested to make sure it does what you want.

Hi Shailesh,

The date is in 20120131 format. It is LINUX & ksh.

Thanks,
Freddie

Could you show an entire filename for reference?

you can use this:

d=$(date '+%Y%m%d')
i=6
while [ $i -ge 0 ]
 do 
 printf "mv filename_$(expr $d - $i) /move/path/\n"
 i=$(expr $i - 1)
done

replace printf with mv command if this code does get what you want.

#!/bin/bash

DAYSBACK=7
WORKINGDIR="/tmp"
TARGETDIR="/tmp/mvd"
ACTION="cp"     # change this to mv 

for day in `seq 1 ${DAYSBACK}`
do
        ${ACTION} ${WORKINGDIR}/`date -d "${day} days ago" +%Y%m%d`.zip ${TARGETDIR}
done                                                                                                                                          
dir=/aas/home/testdir1/final
for fname in `find . -name "*.zip" -mtime -7`
do
mv $fname $dir
done

The only problem i see with using find is that it relies on the fact that the files have correct modification time.

@dsfreddie - will the files always have correct metadata? Modified time etc?

Hi Shailesh,

Thanks for your inputs.

Pls find below what I have tried so far,

However, it is giving errors like below,

Can you pls help
Thanks,

Freddie

Thanks All for the inputs.

The code below worked for me :b:

Thanks
Freddie