Operating System - Linux
1752781 Members
6555 Online
108789 Solutions
New Discussion юеВ

Re: script to move files by month

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

script to move files by month

Hi guys,

I am working on a script that will move and comppress files that are from 3 months ago.

what I need is a solution to find all files in a directory that are from say december which is 3 months previous. I know find and mtime will identify all files from 90days however this may not capture all files.

any help would be great

Thanks.
hello
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: script to move files by month

How about using touch to create a reference file of a certain date and then using the -newer option to move anything older than that?

Something like:

touch ref_file mmddyy, etc.
find /start_dir ! -newer ref_file


Pete

Pete
Pete Randall
Outstanding Contributor

Re: script to move files by month

I should also add that you need to consult the man pages for exact syntax for the touch and find commands.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: script to move files by month

Hi:

> I know find and mtime will identify all files from 90days however this may not capture all files.

Why not? The 'find' options offer '-mtime' as well as '-newer'. With '-mtime' you have the ability to specify exactly, greater than or less than. With '-newer' you can specify ranges of time by creating two reference files and comparing greater than one and not greater than the second.

Of course, limit all of your 'find' selections to files only ('-type f') and don't cross mountpoints ('-xdev').

See the 'find' manpages for more information.

Regards!

...JRF...
lawrenzo
Trusted Contributor

Re: script to move files by month

Thanks Pete but I am looking to automate the move so the script can work out all files in the directory that where created in December.

how can I make the script determine all files from december?

Thanks

Chris
hello
James R. Ferguson
Acclaimed Contributor
Solution

Re: script to move files by month

Hi (again) Lawrenzo:

There is no such thing in Unix as a creation date, only a modification date that coincidently equals a creation timestamp because there was one and only one instantiation.

That said, this should work sufficiently well:

# touch -mt 200612010000 /tmp/ref1
# touch -mt 200612312359 /tmp/ref2

# find /path -xdev -type f -newer /tmp/ref1 -a ! -newer /tmp/ref2

Regards!

...JRF...
lawrenzo
Trusted Contributor

Re: script to move files by month

I have reopened this thread because I require an automated solution within a script, can someone advise ....

Basically I have a directory with 3 months of data in, at month 4 I have to cat the files to one large file and gzip, then when a file is required I have the code that extracts the data and recreates the original file ( Thanks JRF )

The problem I am having is determining how to create the archive file archive.mprcout.<0701> ie:

would I be best to setup a load of variables to determine the month

date=`date +%m`

if [ $date -eq 04 ] ; then
date=01
elif [ $date -eq 05 ] ; then
date=02
etc
etc

then the time stamp on the files will eqaul a letter string ie Jan therefore to match all files with Jan and concatenate to archive.mprcout.<0701> I would:

date=`date +%m`

if [ $date -eq 04 ] ; then
date=01
month=Jan
elif [ $date -eq 05 ] ; then
date=02
month=feb
etc
etc

again is there a simpler method to achieve this>

many thanks

Chris
hello
Hein van den Heuvel
Honored Contributor

Re: script to move files by month

Just use math, notably the modulus operator % !

The alternative is to subtract and if zero or less re-add 12.
(Or add 8 (12-4) and subtract 12 when gt 12)

$ current=05
$ let backup=$current+12-4
$# yes, you can use +8, but this is more clear.
$ echo $backup
13
$ let backup=$backup%12
$ echo $backup
1
$ zbackup=$(printf "%02d" $backup)
$ echo $zbackup
01

I suppose you can also use typeset to force the formatting.

Personnaly i would use proper date math (perl?) and not muck with the year/month transitions myself

Also... you may want to consider to retain your refrence files in a fixed location such that you know what was done last. Then recalculate just 1 endpoint, and mv ref2 ref1.

Finally.. what if magically a 5 month old file shows up? Do you want it captured? In the current cycle seperate. If it can go to the current cycle then just use olde reference file, not the newer.

Regards
Hein van den Heuvel

Hein van den Heuvel
Honored Contributor

Re: script to move files by month

On the typeset suggestion...

$ WINDOW=3
$ YEAR=$(date +%Y)
$ let MONTH=$(date +%m)-$WINDOW
$ if [ $MONTH -le 1 ]; then let MONTH=$MONTH+12; let YEAR=$YEAR-1; fi
$ typeset -Z2 ZMONTH=$MONTH
$ NAME=${YEAR}${MONTH}
$ echo $NAME

Hein.

Hein van den Heuvel
Honored Contributor

Re: script to move files by month

correction

>> $ if [ $MONTH -le 1 ];

That should of course be -lt 1

Regards,
Hein.
[ 0 point for this ]