1849468 Members
5766 Online
104044 Solutions
New Discussion

seript help !! cron job

 
SOLVED
Go to solution
mw_4
Frequent Advisor

seript help !! cron job

Hi,
I have been getting good tips owe to every body, Thanks again...

I want to sort files and mv that sorted files into designated directory.
Files are sorted to created date(not day but month)

cronjob must end of month

ie.)
files created Jan 2002,Feb 2002, Mar 2002...
mv JAN2002,FEB2002,MAR2002,...
designamed directory)

what am I suppose to do?
help me please
Step by step
8 REPLIES 8
Stefan Farrelly
Honored Contributor

Re: seript help !! cron job


Tough one. Without using some complicated date functions and calculations I use a procedure like this to do these sorts of moves;

1. List all files and grep -v those not from this year; ls -ltr | grep -v " 199" | grep -v " 200"
This will take out all files from years 199* and 200* but leave files from this year.

2. Now, to find the files for the month you want you can do; ls -ltr | grep " Jan "
This will list all files from Jan this year (but by time modified - for created use ls -lctr)

3. Now you can pipe this into a script and process;
To move Jan 2002 files;
for i in $(ls -ltr |grep -v " 199" | grep -v " 200" | grep " Jan "| awk '{print $9}')
do
echo moving $i to
mv $i
done

And you can change Jan to Feb then Mar etc.

Im from Palmerston North, New Zealand, but somehow ended up in London...
Steve Steel
Honored Contributor

Re: seript help !! cron job

Hi

To help you on the way

look at

http://www.introcomp.co.uk/examples/copybus5.html

To sort a directory by date

#Parameter = directory
cd $1
year=$(date '+%Y')
ll -tr|while read a b c d e f g h i
do
h1=$(echo $h|grep -v ":")
if [ "$h1" = "" ]
then
h=$year
fi
echo $f" "$h" "$PWD"/"$i
done


If you still cannot make your script please log the request for more help

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
James R. Ferguson
Acclaimed Contributor

Re: seript help !! cron job

Hi:

Use the '-M' option of 'sort'. This compares as months. For instance, the American month names are compared such that JAN < FEB < ... < DEC.

Have a look at the man pages for 'sort' for more information.

Regards!

...JRF...
mw_4
Frequent Advisor

Re: seript help !! cron job

I wonder if I could run the script end of month..
Feb has 28th, and each of month has 30, 31 respectively..
who could give the answer..?
Step by step
Reinhard Burger
Frequent Advisor

Re: seript help !! cron job

Hi

If you want to run the script always on the last day of the month the most easiest way :

enter for each month one line into a cron.
This alows you to define on which day of the month it should be running
example for FEB and MAR:

#Min Hour Day Month WeekDay script
0 23 28 2 script.sh
0 23 29 2 script.sh
0 23 31 3 script.sh

To be honest i have no better idea on how to handle years having Feb 29 it depends on your script if my idea is useable for you. Just running it on the 28 and 29 of feb will get you all files of feb moved to the correct dir
keep it simple
Andreas Voss
Honored Contributor

Re: seript help !! cron job

Hi,

you could run the script every day and check within the script if it is the last day of the month with:

#!/bin/sh

day=$(TZ=MET-24 date +%d) # will look at the next day
if [[ $day -ne 1 ]]
then
exit 0 # not the last day of month
fi
# start here with your code

(In my example i used timezone MET, you have to change this for your timezone)
Regards
mw_4
Frequent Advisor

Re: seript help !! cron job

Andreas Voss!!
could you make clear that
TZ=MET-24 date +%d?
Step by step
Andreas Voss
Honored Contributor
Solution

Re: seript help !! cron job

Hi,

TZ=MET-24 date +%d

shifts the daylight saving time towards one day.
So yuo get the value of the next day.
If this returns 1 you are always at the and of a month regardless if the month has 28,29,30 or 31 days.

Regards