2015-09-29 37 views
0

我想在下午11:30的crontab不刪除的文件在Linux

刪除所有的PDF文件,其中有30多天我加了如下代碼在crontab中

30 23 * * * find /var/www/html/site/reports/ -name "*.pdf" -type f -mtime +30 | xargs -I {} rm -f {} \; 

但它不會刪除這些文件。

你可以請檢查是什麼問題?

crontab的細節

-rw-r--r--. 1 root root 532 Sep 30 11:14 crontab 

一個,我需要刪除

-rw-r--r-- 1 apache apache 15215 Jul 25 11:24 sales_report.pdf 
+0

'+ 30'意味着** **以上30天以內。所以正好30天的文件不會被刪除。 – Barmar

+0

最後不需要'\;' - 這是爲了當你使用'find -exec'時,它不是'xargs'的一部分。但是,你爲什麼不使用'-exec'? – Barmar

+0

此外,某些版本的'find'具有刪除文件的'-delete'選項。 – Barmar

回答

0

的文件我有這樣一個工作的Linux機器。

30 23 * * * find /var/www/html/site/reports/ \(-name "*.pdf" \) -type f -mtime +30 -exec rm {} \; 
+0

當你只有一個測試時,你不需要括號。 – Barmar

+0

@Jason,這對我不起作用 – Jeff

+0

我添加了root用戶名,它現在可以工作。我還有一個問題。這兩個工作(1.我提到的那個,2.Jason提到的那個)正在工作。哪一個更合適? 「30 23 * * * root find/var/www/html/site/reports/-name」* .pdf「-type f -mtime +30 | xargs -I {} rm -f {} \;」和「30 23 * * * root find/var/www/html/site/reports/\(-name」* .pdf「\)-type f -mtime +20 -exec rm {} \ – Jeff

1

您錯過了用戶和PATH。這可能有助於

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 

再檢查的/ tmp/debug_cron

+0

感謝@aldebober,其中哪一個更適合其中的兩個。 1.「30 23 * * * root find/var/www/html/site/reports/-name」.pdf「-type f -mtime +30 | xargs -I {} rm -f {} \;」 2.「30 23 * * * root找到/ var/www/html/site/reports /(-name」)。pdf「)-type f -mtime +30 -exec rm {} \;」 – Jeff

+0

exec選項更好 – aldebober

+0

請問你能告訴我原因嗎? – Jeff