Automate UNIX script...!

Hi, I'm manually running below commands to delete files from some directories. Can anyone please help me to automate the script and also let me know how to schedule it in unix crontab. It'll run every alternative day at 6 PM.

find /tempr/DSC/PH1* -mtime +3 -exec rm -f {} \;
find /tempr/DSC/PH2* -mtime +3 -exec rm -f {} \;
find /tempr/DSC/PH3* -mtime +3 -exec rm -f {} \;

Thanks in advance,
:slight_smile:

Run on "even" weekdays at 18:02

2 18 * * 0,2,4,6 find /tempr/DSC/PH[123]* \! -type d -mtime +3 -exec rm -f {} \;

A little further improvement:

Instead of the find action...

-exec rm -f {} \;

you may use...

-exec rm -f {} +

which uses less rm calls since filenames to delete are added until the max. number of arguments for a command are reached.

So for 26 files, the original calls would be:

rm -f file.a 
rm -f file.b
rm -f file.c
...
rm -f file.z

and the new call is:

rm -f file.a file.b file.c ... file.z
2 Likes

Hi, thanks for the script. I've never configured crontab. Could you please let me know how to use that.

thanks!

crontab -l list.
crontab -e edit with default editor.
EDITOR=nano crontab -e edit with nano .
(crontab -l; echo '2 18 * * 0,2,4,6 find /tempr/DSC/PH[123]* \! -type d -mtime +3 -exec rm -f {} +') | crontab add a new crontab line from the command line (or in a shell script). Some crontab versions need crontab - .

2 Likes

[quote=stomp;303045961]
A little further improvement:

Instead of the find action...

-exec rm -f {} \;

you may use...

-exec rm -f {} +

or even simpler

-delete

Hi:
I'm pretty new in unix. Could you please send me the full code and explain how it's gonna work.
Thanks!