LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2017, 02:12 PM   #1
jazzo
Member
 
Registered: Jul 2012
Posts: 270

Rep: Reputation: Disabled
running a script to delete files every sunday


Hi guys, I'm trying to run a script on a folder to remove all the files every sunday at 23.00.
I've done a little bt of research, to find out what's involved etc, and what I got out of it is that I need to use cron to automate the process.
Needless to say I've never used it before o I had a look at a few examples and I came up with something, but I have no idea whether it is correct, so I was wondering if anyone could give it a quick glance.
Thi is the line of that will go in cron:
Code:
@weekly 00 23 * * 7 find /home/antobbo/Public-ubuntu-big -delete
The @weekly seems to indicate that it gets executed every week and I thought that adding time and date will do the rest. Am I on the right way?
thanks
 
Old 04-10-2017, 02:23 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,493

Rep: Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107Reputation: 4107
Code:
       @reboot    :    Run once after reboot.
       @yearly    :    Run once a year, ie.  "0 0 1 1 *".
       @annually  :    Run once a year, ie.  "0 0 1 1 *".
       @monthly   :    Run once a month, ie. "0 0 1 * *".
       @weekly    :    Run once a week, ie.  "0 0 * * 0".
       @daily     :    Run once a day, ie.   "0 0 * * *".
       @hourly    :    Run once an hour, ie. "0 * * * *".
The shortcuts replace the time and date fields. @weekly runs every Sunday at 00:00. Just use
Code:
00 23 * * 7 find /home/antobbo/Public-ubuntu-big -delete
 
Old 04-10-2017, 02:25 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,316
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
logrotate(8) was designed for this purpose. It does not have to be log files, it can be any file. You can set it up to run weekly at the specific time you want and give it the files using wildcard or exacting names. You can also compress and rotate so as to retain the superseded files up to some chosen number of rotations.
 
Old 04-11-2017, 03:30 PM   #4
jazzo
Member
 
Registered: Jul 2012
Posts: 270

Original Poster
Rep: Reputation: Disabled
Oh I see, thanks guys, I will give it a go then, cheers
 
Old 05-01-2017, 04:55 AM   #5
jazzo
Member
 
Registered: Jul 2012
Posts: 270

Original Poster
Rep: Reputation: Disabled
right, this command
Code:
00 23 * * 7 find /home/antobbo/Public-ubuntu-big -delete
deletes the folder and not the files in it...how do I preserve the folder and delete files only?
 
Old 05-01-2017, 04:59 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
try to add for example: -type f (before -delete)
 
Old 05-01-2017, 07:48 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
wouldn't just tossing a script it into cron.weekly on a Sunday and running it keep it running every Sunday too?

I only say this because I have a trim script tossed into weekly and it runs once a week. though I never marked the day I put it in there against when it actually runs. but it does run once a week, and I have not added it to a run job.

Last edited by BW-userx; 05-01-2017 at 07:50 AM.
 
Old 05-01-2017, 12:16 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,741

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
Delete recursively everything but the start-directory
Code:
00 23 * * 7 find /home/antobbo/Public-ubuntu-big -depth -mindepth 1 -delete
Or
Code:
00 23 * * 7 cd /home/antobbo/Public-ubuntu-big && find . -depth \! -name . -delete
 
Old 05-01-2017, 01:21 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,142
Blog Entries: 4

Rep: Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229Reputation: 3229
"Kind pilgrim, heed thee, now, the most-wise admonition given thee by rtmistler in reply #3!"

logrotate is a tool that was specifically designed to deal with the inevitable accumulation of "historical files of any sort." It can do a lot of other useful things, too.

But it is smart about doing it. rm is ... not.

Last edited by sundialsvcs; 05-01-2017 at 01:23 PM.
 
Old 05-02-2017, 03:33 PM   #10
jazzo
Member
 
Registered: Jul 2012
Posts: 270

Original Poster
Rep: Reputation: Disabled
thanks, I updated the script, will let you know how it goes as I'm off till sunday :-)!

Quote:
wouldn't just tossing a script it into cron.weekly on a Sunday and running it keep it running every Sunday too?
The way I understood it is that if you do that you can't decide the time, I may be wrong though
 
Old 05-04-2017, 03:27 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,741

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
Yes, with both the @weekly and the cron.weekly mechanisms you cannot decide weekday and time.
 
  


Reply

Tags
cron


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to run a script on first sunday of every month jayakumar01 Linux - Server 2 05-29-2012 02:40 PM
Script to delete older files not running . linuxlover.chaitanya Linux - Newbie 10 01-12-2009 01:27 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete old files script simpi Linux - Newbie 11 04-25-2008 02:37 AM
Every sunday running difference backup job. kwingng Linux - Server 2 05-01-2007 10:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration