crontab is not deleting the files in linux - linux

I am trying to delete all the pdf files which are more than 30 days old at 11:30 PM
I added the below given code in crontab
30 23 * * * find /var/www/html/site/reports/ -name "*.pdf" -type f -mtime +30 | xargs -I {} rm -f {} \;
But it doesn't delete the files.
Can you please check what the issue is?
The crontab details
-rw-r--r--. 1 root root 532 Sep 30 11:14 crontab
One of the files which i need to delete
-rw-r--r-- 1 apache apache 15215 Jul 25 11:24 sales_report.pdf

You missed user and PATH. This may help
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
30 23 * * * root find /var/www/html/site/reports/ \( -name "*.pdf" \) -type f -mtime +30 -exec rm {} \; >> /tmp/debug_cron 2>&1
And then check /tmp/debug_cron

I have this working a a linux box.
30 23 * * * find /var/www/html/site/reports/ \( -name "*.pdf" \) -type f -mtime +30 -exec rm {} \;

Related

how to delete 3days old back files automatically in server and My backup files looks like?

web_20_10_2022
web_21_10_2022
web_22_10_2022
web_23_10_2022
web_24_10_2022
web_25_10_2022
I need auto delete script for 3 days old backup files from server and with cron job.
below are some commond I have tried but it is not working, so please help me out!!!
find /home -type f -mtime +3 -exec rm -rf {} +
find /home* -mtime +3 -type f -delete
find /home -mtime +3 -type d -exec rmdir {} ;
Kindly run the below command to find out the files older than 3 days.
find /path/to/directory/* -mtime +3 -print
The older files will be displayed in the mentioned directory if the above command is used.
To delete the 3 day old files:
find /path/to/directory/* -type f -mtime +3 | xargs rm -f
or
find /path/to/directory/* -type f -mtime +3 -exec rm -f {} \;
or
find /path/to/directory/* -type f -mtime +3 -delete
To automate the process:
Copy the below code and save the file in a directory where you are not performing any housekeeping activity.
#!/bin/bash
find /path/to/directory/* -mtime +3 -delete
Make the file an executable file and set a cron job.
crontab -e
To run the script at every hour, enter
00 * * * * /path/to/executablefile

linux Find command not working through crontab

I am using the command given below to delete the 2 days old data from the folder. The command works fine when I run this through directly terminal but not working through crontab entry.
find /backup/DWHPROD/ -type f -mtime +1 -exec rm -r {} \;
Crontab Entry :
30 16 * * * find /backup/DWHPROD/ -type f -mtime +1 -exec rm -r {} \;
Please run the command in your terminal:
which find
I am having find located at /usr/bin/find.
Try adding that in your crontab script
30 16 * * * /usr/bin/find /backup/DWHPROD/ -type f -mtime +1 -exec rm -r {} \;

Deleting a directory starting with a specific string in shell script

When I'm trying to delete all directories starting with tmp,
I used this command:
find -type d -name tmp* -exec rmdir {} \;
And it does the trick, but this command is exiting with an error code:
find: `./tmp09098': No such file or directory
What causing failing my build.
Can anyone tell me how I can delete those folders without getting the error?
After trying what #anubhava suggested and quoted 'temp*',
find -type d -name 'tmp*' -exec rmdir {} \;
I still get the same error:
find: `./tmp0909565g': No such file or directory
find: `./tmp09095': No such file or directory
When running:
find -type d -name 'tmp*' -exec ls -ld '{}' \;
This is the result:
drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp0909565g
drwxr-xr-x 2 root root 4096 Jun 16 10:07 ./tmp09095
drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp09094544656
You should quote the pattern otherwise it will be expanded by shell on command line:
find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
-prune causes find to not descend into the current file/dir.
This works for me:
#! /bin/bash
tmpdirs=`find . -type d -name "tmp*"`
echo "$tmpdirs" |
while read dir;
do
echo "Removing directory $dir"
rm -r $dir;
done;

Cron to delete folders older than required time deletes parent folder

I have a cron job that creates folders within the "backup" directory \tmp\backup.
I am looking to have a second job to delete folders within "backup" which are older than 1 minute using the job below
55 19 * * * find /tmp/backup/ -maxdepth 1 -type d -mmin +1 -execdir rm -rf {} \;
But this job deletes the parent directory "backup" too, I am confused on where I am going wrong. Any help is appreciated !
Easy enough to test.
for a in {1..3}; do mkdir -p /tmp/backup/${a}; done
find /tmp/backup/ -maxdepth 1 -type d -mmin +1
This returned
/tmp/backup
/tmp/backup/2
/tmp/backup/1
/tmp/backup/3
But
find /tmp/backup/* -maxdepth 1 -type d -mmin +1
returned
/tmp/backup/2
/tmp/backup/1
/tmp/backup/3
Add a asterix

Delete files older than X minutes

I would like to delete files that are older than 59 minutes. I have this so far:
find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*" -exec rm -f {} \;
This doesn't work and seems to delete all files. I've tested this several times and I think the issue is to do with daystart.
I've read the man page and it seems to base time on the beginning of the day rather than from 24 hours ago. If this is the case how can I accurately delete files that are older than 59 minutes? Do I need to account for daystart and add some more minutes?
Example:
ubuntu#ip-10-138-30-118:/tmp$ ls -la
total 8
drwxrwxrwt 2 root root 4096 Jul 20 14:39 ./
drwxr-xr-x 23 root root 4096 Jun 25 18:34 ../
-rw-rw-r-- 1 ubuntu ubuntu 0 Jul 20 12:35 a.txt
Both the following commands, return the file:
ubuntu#ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*"
/tmp/a.txt
And:
ubuntu#ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +359 -type f -name "*.*"
/tmp/a.txt
However, the file is not older than 659 minutes (10.9 hours)! But at 759 (12.65 hours), it doesn't return the file anymore?
When used with -mmin, -daystart appears to make it calculate from the end of today, not the beginning.
If you just want to find files modified more than 59 minutes ago, you don't need that option. -mmin calculates from the current time by default.
barmar#dev:~/testdir$ date
Sat Jul 20 10:02:20 CDT 2013
barmar#dev:~/testdir$ ls -l
total 0
-rw-r--r-- 1 barmar adm 0 Jul 20 09:57 a.txt
barmar#dev:~/testdir$ find . -maxdepth 1 -mmin +2 -type f
./a.txt
barmar#dev:~/testdir$ find . -maxdepth 1 -mmin +10 -type f
this should work for you
find /path -mmin +59 -type f -exec rm -fv {} \;

Resources