Operating System - Linux
1828590 Members
5000 Online
109983 Solutions
New Discussion

scripting to move files to specific date

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

scripting to move files to specific date

Hello all,

I am attempting to write a script that searches an archive directory - finds files over 3 months old and create another directory then move the files to the specific month.

ie.

files listed in the directory will have dates such as march 1-31 feb 1-28/29 Jan 1 etc etc

so I do a find /Inarchive -mtime + 90 -exec ls -l {} \;

gives me my list then I wan to move these files to a specific date directory which is one dir up the tree with the original dirname then date ../Inarchive.yymm but this directory will need to be create from the script.

I have some idea's by creating lots of variables but am open to suggestions.

Thanks for you help chaps.

Mr L.
hello
6 REPLIES 6
RAC_1
Honored Contributor

Re: scripting to move files to specific date

You mean to say dir where files will be copied should have certain name and that dir be created on the fly? For exmaple if you move files for 2nd feb, dir will have name 2-FEB or something similar?
There is no substitute to HARDWORK
Chan 007
Honored Contributor

Re: scripting to move files to specific date

Hi,

Use this way to get the month & date for the files that are being moved.

NDATE=$(find /tmp -mtime +20 -exec ls -l {} \; |awk '{print $6 $7}')

I assume that you run this script once every day.

The 1st time you may get old files too.

Chan
Peter Godron
Honored Contributor

Re: scripting to move files to specific date

Mr L,

#!/usr/bin/sh
cd lnarchive
for a in `find . -mtime + 90 -type f`
do
month=`ll $a | awk -F' ' '{print $6}'`
file=`ll $a | awk -F' ' '{print $9}'`
if [ ! -d $month ]
then
echo "mkdir ../lnarchive.$month"
echo "mv $file ../$month/$file"
else
echo "mv $file ../$month/$file"
fi
done

This script pulls out the filedate month and then check if the directoryy exists.

If you are happy with the test run, remove the echo parts of the mkdir and mv lines.
Peter Godron
Honored Contributor
Solution

Re: scripting to move files to specific date

Mr L,

#!/usr/bin/sh
cd lnarchive
for a in `find . -mtime + 90 -type f`
do
month=`ll $a | awk -F' ' '{print $6}'`
file=`ll $a | awk -F' ' '{print $9}'`
if [ ! -d $month ]
then
echo "mkdir ../lnarchive.$month"
echo "mv $file ../lnarchive.$month/$file"
else
echo "mv $file ../lnarchive.$month/$file"
fi
done

This script pulls out the filedate month and then check if the directoryy exists.

If you are happy with the test run, remove the echo parts of the mkdir and mv lines.
Ninad_1
Honored Contributor

Re: scripting to move files to specific date

I think this should do it.
Write the following into a file and keep it in /Inarchive , goto Inacrchive and execute this.
I have put the echo for mkdir and mv currently so that you can test if it works to your satisfaction and then you cabn remove the echo.



find . -mtime +90 -type f | xargs ls -ld | awk '{print $6,$8,$NF}' > files.dat
grep ":" files.dat | awk '{print $1}' | sort -u | while read month
do
year=$(date +%y)
if test ! -d ../inarchive.$year$month
then
echo mkdir ../inarchive.$year$month
fi
files=$(grep "$month ..:" files.dat | awk '{print $NF}')
echo mv $files ../inarchive.$year$month
done
grep -v ":" files.dat | awk '{print $1,$2}' | sort -u | while read month year
do
year=$(echo $year | cut -c 3-4)
if test ! -d ../inarchive.$year$month
then
echo mkdir ../inarchive.$year$month
fi
files=$(grep "$month ..$year" files.dat | awk '{print $NF}')
echo mv $files ../inarchive.$year$month
done


Regards,
Ninad
lawrenzo_1
Super Advisor

Re: scripting to move files to specific date

Thats great guys, the script will be run every month via cron for this filesystem however I will use the syntax to create an archive policy and script for every directory on the server.

Thanks a million!
hello