Suppose, you are saving some data for safety purposes or testing purposes in a specific folder. After some day/month you don’t need the files and you need to delete the files manually. The same task can be done using some linux command and crontab.
Hence, you want to write a script that will delete these type of files which are older for more than 2 days. After, you should put the script in crontab, so this process will get automated. So, at first,you need to create a bash script “remove_file.sh” in /etc/cron.daily/
remove_file.sh should look like this-
— — — — — — — — — — — — — — — — -
#!/bin/bash
PATH=”/var/tmp/test/”
/usr/bin/find $PATH -type f -name “*.mp4” -mtime +2 -exec /bin/rm {} \;
— — — — — — — — — — — — — — — — — — —
Now, add the task to crontab using command ‘crontab -e’.
Then add the line and save after edit.
0 0 * * * /etc/cron.daily/remove_file.sh
# The script will run at 12 am everyday.
Problem you may face related to find command and cronjob:
find $PATH -type f -name “*.mp4” -mtime +2 -exec rm {} \;
This command is fine when you run the command in a console. But there may some problems arise when the same command is used in the script for crontab.
The problem is that crontab doesn’t have original path for the ‘find’ and ‘rm’ command when it runs. You can actually provide it with a path by adding this to the top of the file opened via crontab -e:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
If you don’t specify the PATH definition like above, use:
/usr/bin/find $PATH -type f -name “*.mp4” -mtime +2 -exec /bin/rm {} \;
Crontab Syntax:
Each line in the user crontab file contains six fields separated by a space followed by the command to be run.
* * * * * command(s)
- — — — -
| | | | |
| | | | — — — Day of week (0–7) (Sunday=0 or 7)
| | | — — — — Month (1–12)
| | — — — — — Day of month (1–31)
| — — — — — — Hour (0–23)
— — — — — — — Minute (0–59)
Here are some sample shortcut syntax for running cron job.
- @yearly — Run the specified task once a year at midnight (12:00am) of 1st of January. Equivalent to 0 0 1 1 *.
- @monthly — Run the specified task once a month at midnight on the first day of the month. Equivalent to 0 0 1 * *.
- @weekly — Run the specified task a week at midnight on Sunday. Equivalent to 0 0 * * 0.
- @daily — Run the specified task once a day at midnight. Equivalent to 0 0 * * *.
- @hourly — Run the specified task once an hour at the beginning of the hour. Equivalent to 0 * * * *.
- Every three days of month — 0 0 */3 * *
- @reboot — Run the specified task at the system startup (boot-time).
Crontab command:
The crontab command allows you to install or open a crontab file for editing. You can use the crontab command to view, add, remove or modify cron jobs using the following options:
- crontab -e — Edit crontab file, or create one if it doesn’t already exist.
- crontab -l — Display crontab file contents.
- crontab -r — Remove your current crontab file.
- crontab -i — Remove your current crontab file with a prompt before removal.
- crontab -u <username> — Edit other use crontab file. Requires system administrator privileges.
Ref:
https://linuxize.com/post/scheduling-cron-jobs-with-crontab/
https://serverfault.com/questions/742704/find-delete-works-ok-but-not-with-cron