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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-10-2017, 02:12 PM
|
#1
|
Member
Registered: Jul 2012
Posts: 277
Rep: 
|
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
|
|
|
04-10-2017, 02:23 PM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,867
|
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
|
|
|
04-10-2017, 02:25 PM
|
#3
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,964
|
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.
|
|
|
04-11-2017, 03:30 PM
|
#4
|
Member
Registered: Jul 2012
Posts: 277
Original Poster
Rep: 
|
Oh I see, thanks guys, I will give it a go then, cheers
|
|
|
05-01-2017, 04:55 AM
|
#5
|
Member
Registered: Jul 2012
Posts: 277
Original Poster
Rep: 
|
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?
|
|
|
05-01-2017, 04:59 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,542
|
try to add for example: -type f (before -delete)
|
|
|
05-01-2017, 07:48 AM
|
#7
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
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.
|
|
|
05-01-2017, 12:16 PM
|
#8
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,052
|
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
|
|
|
05-01-2017, 01:21 PM
|
#9
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,311
|
"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.
|
|
|
05-02-2017, 03:33 PM
|
#10
|
Member
Registered: Jul 2012
Posts: 277
Original Poster
Rep: 
|
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
|
|
|
05-04-2017, 03:27 AM
|
#11
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,052
|
Yes, with both the @weekly and the cron.weekly mechanisms you cannot decide weekday and time.
|
|
|
All times are GMT -5. The time now is 11:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|