I'd like to write a cron - once a day delete folder - help?

U

URDRWHO

Guest
I would like to write a cron that would delete the thumbnails in the .thumb folder and also certain folders in the .cache folder (Mozilla folder and Thunderbird folder). I could tell Firefox to clear history each time I leave but I do like having the ability to type in something I looked at last week into the URL bar and have it shown.

I would like the cron to run daily at 9 PM.

How would I do it.

Why do I even get buildup of all those files in the .thumbnail folder. Some files are duplicates.

I am running thin on HD space and anything that takes extra space gets axed. I even think about the var/cache folder because its size is 1/4 of a gig.
 


Use this command to edit your crontab:
Code:
crontab -e

That should open up your crontab file in your terminals default text editor (nano, vi/vim, emacs etc)

Cron jobs look like this:
Code:
* * * * * {path to script or command} {arguments}
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

To run a script at 9am each day:
Code:
0 9 * * * /path/to/scriptToExecute.sh
Where scriptToExecute.sh is a shellscript at /path/to/ containing the commands you want to run.

Or to run a single command at 9am each day:
Code:
0 9 * * * /path/to/command arg1 arg2 arg3 etc...

So, as you currently only want to delete all of the the files in the .thumb directory, we'll go with the single command version. So perhaps this will do:
Code:
0 9 * * * rm ~/.thumb/*
Note: If .thumb is not in your home directory, you'll need to specify the exact path to the .thumb dir.

Once you have entered a line for the job you want to run, save the file and exit the editor.
Now at 9am each day, the .thumb directory will be cleaned out!
 
thanks

I'll give it a try.

As for the time to run I want 9 PM so I would place 21 into the mix instead of 9? Correct?

Whoa! I opened that with crontab -e and then I had to choose an option (I chose 2 because it said easiest) and then a file was opened. I could never find how to save the file and then if I knew how to save.....where to save?

I;m a Newbie on the cron !
 
Last edited:
thanks

I'll give it a try.

As for the time to run I want 9 PM so I would place 21 into the mix instead of 9? Correct?

Whoa! I opened that with crontab -e and then I had to choose an option (I chose 2 because it said easiest) and then a file was opened. I could never find how to save the file and then if I knew how to save.....where to save?

I;m a Newbie on the cron !
Yes 21
 
thanks

I'll give it a try.

As for the time to run I want 9 PM so I would place 21 into the mix instead of 9? Correct?

Whoa! I opened that with crontab -e and then I had to choose an option (I chose 2 because it said easiest) and then a file was opened. I could never find how to save the file and then if I knew how to save.....where to save?

I;m a Newbie on the cron !
Simply do a [File], [Save] using whatever editor you are using, then close the editor.
 
Sorry, yes 21..... Sorry, misread your post.. Thought you said AM! :/

As for saving, it depends on which editor your terminal opened. If it was vi or vim, you need to use esc to go into command mode and use the :wq command. If it was emacs it will be ctrl+x and then ctrl+s to save and ctrl+x and then ctrl+c to quit. If it was nano, you use ctrl+o to save and ctrl+x to quit.
 
Last edited:
It is nano and ctro+o takes me to the place that the file is named and then I get choices of Mac (don't want) but there are the choices of DOS, append, prepend or backup. In front there is an M example would be -- DOS choice is M-D. What is the M?

I know the sign for control but the M sign is what????
 
M stands for Meta.... Or as we mortals would know it, the Alt key!

You shouldn't need to worry about any of those options!

After editing the crontab file in nano, just use ctrl+o and then press enter to accept the default filename, which should be the filename the editor was passed when it was opened by crontab.
 
Ha! I've been playing around trying to save the file. I either have no cron saved or a bunch of them saved.

Once saved, where would I look to see them?

I have an already existing folder hierarchy should I place it in the cron.daily?

/etc/cron.daily/
 
Last edited:
All of your cron jobs will go into the one crontab file.
So if you want to add another task, you use:
Code:
crontab -e
This will open your crontab file and you can edit the file to add/remove jobs.
You can see all of the jobs in your crontab file using the following command:
Code:
crontab -l
 
This is the path that is shown at the top when I open the cron -e
File: /tmp/crontab.e1zBDS/crontab

I then hit ctrl + O and then I see it is going to save the file
File: /tmp/crontab.e1zBDS/crontab

I hit enter

I see that it wrote 23 lines

I hit ctrl _ X

Is it saved, can I see it in a folder?

I can't see anything saved and I must be doing something wrong.
 
Using crontab -l shows ------

Code:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow  command
0 21 * * * rm ~/.thumb/*


All of your cron jobs will go into the one crontab file.
So if you want to add another task, you use:
Code:
crontab -e
This will open your crontab file and you can edit the file to add/remove jobs.
You can see all of the jobs in your crontab file using the following command:
Code:
crontab -l
 
Using crontab -l shows ------

Code:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow  command
0 21 * * * rm ~/.thumb/*
Yup, that's your crontab file.

As for where the file gets saved to:
If memory serves, when you use crontab -e, it creates a temporary copy of the existing crontab file in a subdirectory of /tmp/ as you have seen (Or if this is your first time using crontab, a new file). After you save the file and exit your editor, it gets moved to /var/spool/cron/crontabs/ which you'll only be able to view if you are root. Which is why you use crontab -l to view it instead!
 
Went to /var/spool/cron/crontabs/ and to see crontabs you need root rights. I always thought because I am the admin / sudo that I have root rights. Or something like it.

Using your command worked and I'll see what happens tonight.

Yup, that's your crontab file.

As for where the file gets saved to:
If memory serves, when you use crontab -e, it creates a temporary copy of the existing crontab file in a subdirectory of /tmp/ as you have seen (Or if this is your first time using crontab, a new file). After you save the file and exit your editor, it gets moved to /var/spool/cron/crontabs/ which you'll only be able to view if you are root. Which is why you use crontab -l to view it instead!
 
Going to mention that Duckduckgo has a really useful cron feature.
Search for cron and the time values. For example:
Workspace 1_046.png
 
Hm? Are you saying that duck duck (my fav search engine) has a way to read cron file. Kind of like pasting network addresses into a browser URL?

I pasted cron and the time values into duck duck and didn't see what your screen shot shows - example cron 0 9***

Is it an Opera thing?

Going to mention that Duckduckgo has a really useful cron feature.
Search for cron and the time values. For example:
View attachment 1852
 
It's an instant answer built into duckduckgo, so it shouldn't matter which browser you use. Works for me in Firefox, Konqueror, Rekonq and Midori. It even works in lynx and w3m (command-line based browsers!)

Enter a line from your crontab and then search and it should appear as an answer in the top of the results page.

There are quite a lot of these things built into duckduckgo. There is an entire community dedicated to developing instant answers and other things for duckduckgo here:
http://duckduckhack.com/
 
It's an instant answer built into duckduckgo, so it shouldn't matter which browser you use. Works for me in Firefox, Konqueror, Rekonq and Midori. It even works in lynx and w3m (command-line based browsers!)

Enter a line from your crontab and then search and it should appear as an answer in the top of the results page.

There are quite a lot of these things built into duckduckgo. There is an entire community dedicated to developing instant answers and other things for duckduckgo here:
http://duckduckhack.com/
I'm working on something myself. But yeah, there are lots of goodies in duck duck go. For the more advanced cron entries it helps immensely.
 
Thanks, I'll check it out.

It is 9:10 PM and I just checked.....my cron didn't run.

Should I have defined the .thumbnail location?

/home/me/.thumbnails/

Should it be

15 0 * * * rm ~ /home/me/.thumbnails/

Not

15 0 * * * rm ~/.thumbnails/*

I just don't want to erase everything in my home/me folder

Actually I should define it even more -

In the .thumbnail folder are two other folders one is large and one is

/home/me/.thumbnails/large/
/home/me/.thumbnails/normal/

It is the information inside of large folder and small folder that I want to delete. I don't want to delete the large or normal folder, it is the stuff inside of the folders that I want to delete.

Do I need two different lines...two different cron's?
It's an instant answer built into duckduckgo, so it shouldn't matter which browser you use. Works for me in Firefox, Konqueror, Rekonq and Midori. It even works in lynx and w3m (command-line based browsers!)

Enter a line from your crontab and then search and it should appear as an answer in the top of the results page.

There are quite a lot of these things built into duckduckgo. There is an entire community dedicated to developing instant answers and other things for duckduckgo here:
http://duckduckhack.com/
 
Last edited:
Didn't work? Hmmm, it should have deleted any files that were in ~/.thumbnails, but it would not have deleted any files in any sub-folders. If all of the files you wanted to delete were in sub-direcories of .thumbnail, that would explain the problem.

The tilde '~' character is an alias/short-cut for /home/yourusername/, so ~/.thumbnails should work. But you could try specifying the full path (e.g.
home/yourusername/.thumbnails) in your cron job, just in case the expansion of the tilde did not work.

BTW: DO NOT use ~/home/yourusername/.thumbnail/*, as a parameter to rm as this would expand to:
rm /home/yourusername/home/yourusername/.thumbnail/*
Not the end of the world, but the command would just fail as it expands to a path that does not exist.

And especially DO NOT put a space between the ~ and the rest of the path like this:
rm ~ /home/username/.thumbnail/* as this may result in dire consequences!

If you want to remove all of the files in .thumbnail and all files in its subdirectories, you could do it in one line using the find command like this:
Code:
15 0 * * * find /home/yourusername/.thumbnails/ -type f -delete
Use crontab -e to edit your existing job as per the above line of code! The cron job above will run every day at quarter past midnight (as per your last post) - so don't forget to change the date/time fields to suit your needs if you want it to run at a different time.

The find command used in the cron job above will find all file-system objects that are files, residing in the .thumbnails directory (or any of its sub-directories) and delete them. It will not affect any other files/folders outside of ~/.thumbnail/ and will not remove any symbolic links, sub-directories or other file-system objects that are inside ~/.thumbnail/.

Thinking about it, I should probably have suggested using find in the first place, rather than using rm! :oops: :rolleyes:
 
Last edited:

Members online


Latest posts

Top