Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux Cronjob

Status
Not open for further replies.

Momozone

IS-IT--Management
Dec 2, 2004
103
IE
I am looking for a script i can run as a cron job that will create a folder every month and copy files older that 30 days to the folder from a specific directory.

Can anyone help?

Thanks in advance!

MoMoZoNe
 
What have you got so far?

All I ask of you
Is make my wildest dreams come true
 
Well, to get you started perhaps a script which:

1. Creates the folder:

mkdir /path/to/your/folder

2. Copies files older than 30 days from the destination folder to the new one:

find /path/to/dest -mtime +30 -exec cp {} /path/to/your/folder \;

If you want the files moved rather than copied, replace the cp with mv.

3. Put the script in crontab so that it runs once per month:

30 12 28 * * /path/to/your/script

This would run the job at 12:30 pm on the 28th of every month. Adjust this as required, but remember that to run every month, the date must be <=28!

Hope this helps.

All I ask of you
Is make my wildest dreams come true
 
Thats great but I am really not familiar with this stuff so see my questions below:

Do I just create a file called file.cron in the cron.monthly folder with

30 12 28 * * /path/to/my/script

in it?

And the my script which is

find /path/to/dest -mtime +30 -exec cp {} /path/to/your/folder \

does that need to have a specific file extensions or does it just need to be made executable?

Thanks



MoMoZoNe
 
Sorry, Linux isn't really my forte, but usually (ie in other *nixes) you can edit an existing crontab directly using crontab -e (though it's not really recommended, as it's better to copy the crontab with crontab -l > newcron, edit newcron to ensure no errors and then activate it with crontab newcron, but that's getting into deeper water than perhaps you like).

You can pick up specific file extensions in the find by adding -name '*.ext' to the above, ie:

find /path/to/dest -mtime +30 -name '*.ext' -exec cp {} /path/to/your/folder \;

replacing ext with whatever your extension is.

You do need to make the script executable. Use:

chmod 755 scriptname

to do this.


All I ask of you
Is make my wildest dreams come true
 
to KenCunningham:

I think Momozone wants to know if the script file itself needs to have an extention.

I personaly would give it '.sh' extention.




Regards Dan
 
Dan - good catch and thanks. It doesn't, but it's good practice to give it a recognisable extension (your .sh for example) anyway.

I don't mind people who aren't what they seem. I just wish they'd make their mind up.

Alan Bennett.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top