Shellscript - check a folder and delete files with

2019-08-15 02:51发布

How can I write a shell script that will delete files only from a folder that is older than 1 hour?

This is what I have so far:

find /path/to/folder -mtime +1 -exec rm {}\;

But this has 2 issues:
1. +1 is actually day not hour
2. when I run the script, I'm getting this error. find: -exec: no terminating ";" or "+"

I plan to run that through cron and I'm testing it from mac terminal.

Thanks,
Tee

标签: linux
2条回答
时光不老,我们不散
2楼-- · 2019-08-15 03:16

Try this script

HOUR=`/bin/date +%k`

HOUR=`expr $HOUR - 01`

HOUR=`printf "%02d" $HOUR`

MIN=`/bin/date +%M`

DAY=`/bin/date +%Y%m%d`


touch -t $DAY$HOUR$MIN  /tmp/t.tmp

find /path/to/folder -type f ! -newer /tmp/t.tmp -exec ls -lt {} +

or

find /path/to/folder -type f ! -newer /tmp/t.tmp -exec ls -lt {} \;
查看更多
Luminary・发光体
3楼-- · 2019-08-15 03:36

Use -mmininstead of -mtime

I would rewrite it to:

find /folder -mmin +60 -delete

if your find does not support -delete use:

find /folder -mmin +60 -exec rm {} +
查看更多
登录 后发表回答