Linux - NewbieThis 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.
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.
I want to keep the 1/2/3/4/5 folders, but any files/subfolders under them I want to delete automatically every other Tuesday. I think the basic bash would just be something like
cd /etc/stuff/1
rm -rf *
cd /etc/stuff 2
rm -rf *
(etc)
But I'm not sure if *.* is the proper syntax, and very much unsure how to set it to run on those days.
To see how to setup your cron, run "man -S5 crontab" to see the syntax options.
For your remove commands:
Code:
cd /etc/stuff/1
rm -rf *.*
...this can be dangerous. What if your cd command failed? Like if the directory had inadvertantly been removed prior to cron running this command? You would recursively remove stuff that you didn't intend to depending on what your old working directory was. Could be disasterous. Better to fully specify the target removal to the command, like this::
One other tip related to what I said above. If you expect something to be a directory, not a file, it's even better to explicitly say so on the command line. Add a trailing slash to the target remove path to force it to be interpreted as a directory. Like this:
One other tip related to what I said above. If you expect something to be a directory, not a file, it's even better to explicitly say so on the command line. Add a trailing slash to the target remove path to force it to be interpreted as a directory. Like this:
Code:
rm -rf /directory/subdirectory/
In this case my exact path is /etc/stuff/cq. I need to maintain the /cq/ folder itself, just delete all files and folders in it. Would I be better off doing:
rm -rf /etc/stuff/cq/*
Or am I way off base and that isn't even close?
(I've always said that it's fine to ask silly questions as long as you only ask 'em once, I hope you guys don't mind me being such a novice here).
Or simply use:
rm -rf /etc/stuff/[12345]/*
or something like that.
homey, you're close, but I think you can't use the "/2" syntax when you're not specifying a range (multiple
values).
And "2" is just 1 value, not a range. Think of "a-b/2" as "from a to b in steps of 2".
So, using "2/2" as day of the week would probably run every Tuesday or not at all.
Using a combination of day of month and day of week doesn't work either, since there is the job runs when either of the two entries is matched ("or" relationship, not "and"). At least, that's so for my Vixie cron.
Other implementations of cron (ATT cron, BSD cron) may work differently.
Look carefully at the man pages, is my advice...
One alternative, would be to run the script every Tuesday, but to make it check (via a toggle) if it should
be run or not.
So: first Tuesday => script runs and sets the toggle (ie via a file) so that it knows that it shouldn't run
on next Tuesday.
2nd Tuesday => script runs again, but sees the toggle, clears it, and stops immediately after, without
performing the "rm -rf" task.
3rd Tuesday, no toggle, so same as first Tuesday...
Running a script every Tuesday is easy:
0 2 * * 2 path/to/your_script
I believe the easiest way of implementing a toggle like that is to store a file, ie in /var/run.
You'll need to have write permission on the directory where the toggle file is stored.
The script can then simply check (in Bash):
if [[ -f /var/run/toggle_file ]]; then
#toggle exists
rm -f /var/run/toggle_file
exit;
else
touch /var/run/toggle_file #stores the empty toggle file
#now do the clean up commands
exit;
This will work. If you also have hidden files in these directories (hidden files begin with a dot), you should add a command to get those explicitly, e.g.:
Code:
rm -rf /etc/stuff/cq/*
rm -rf /etc/stuff/cg/.*
An standalone * wildcard will not match a filename that begins with a dot.
To see how to setup your cron, run "man -S5 crontab" to see the syntax options.
For your remove commands:
Code:
cd /etc/stuff/1
rm -rf *.*
...this can be dangerous. What if your cd command failed? Like if the directory had inadvertantly been removed prior to cron running this command? You would recursively remove stuff that you didn't intend to depending on what your old working directory was. Could be disasterous. Better to fully specify the target removal to the command, like this::
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.