script to delete contents in a directory by date

Hi ,

I am trying to make a cron job to delete the contents of a directory [basically log files] in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written :

/usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \;

Is it correct....If not so,please tell me the ammendments.

Thx in advance.

Two small things....firstly "ctime" is not the "create" time but the "change" time - probably best to read the man page to make sure you understand the difference between mtime and ctime. Secondly you will probably need to escape or quote the wildcard, eg:

/usr/bin/find / -name "*.log" -ctime +2 -exec /bin/rm -f {} \;

Cheers...

Thanks citaylor......:slight_smile:

Hi,

I am using this script to delete the files created 5 days ago in /home/directory/ location.
But somehow its not working.Can you please let me know the error in this.

find /home/directory/ -type f -mtime +5 -exec rm {} \;

Thanks

on a system with GNU touch and find I use

touch -d "-5 days" /tmp/5daysago
find /home/dir/ -type f ! -newer /tmp/5daysago -exec rm {} \;

on a system with GNU touch and find I use

touch -d "-5 days" /tmp/5daysago
find /home/dir/ -type f ! -newer /tmp/5daysago -exec rm {} \;

What happens? Please post any error messages.
Are you user "root"? If not, do you own all the files?

Script is executing without any error or notification but files are not getting deleted.
Also i am logging as a root user..please help..

What do you get it you try to just list the files:

find /home/directory/ -type f -mtime +5 -exec ls -lad {} \;

And (if there are links involved).

find /home/directory/ -follow -type f -mtime +5 -exec ls -lad {} \;

Thanks citaylor for your help....I am able to create the required script..