Script to delete files older than 30days
Solution 1
Assuming you really need to be using find
in order to recurse through subdirectories:
find /export/home/ftp \( -name console.log -or -name server.log \) -mtime +30 -exec rm -f {} +
Solution 2
If you just need to remove the old server.log and console.log every month you can also use logrotate
which is most likely already running under RHEL. A config snippet like this will work in /etc/logrotate.d/*.conf
or wherever the config files are located on your system.
# rotate server.log and console.log every month
# delete, not compress, old file
/export/home/ftp/server.log /export/home/ftp/console.log {
monthly
rotate 0
}
A custom monthly cron, as suggested above, will also work well. In fact, since logrotate is run from cron, you could consider this a cron extension of sorts. HTH.
Solution 3
Why not just use a monthly cron?
@monthly /usr/bin/rm -f console.log @monthly /usr/bin/rm -f server.log
It would definitely be safer then doing things with find.
Related videos on Youtube
Jayakrishnan T
Updated on September 17, 2022Comments
-
Jayakrishnan T almost 2 years
Shell script should not delete any files under* root dir*. My* path will be like /export/home/ftp/ ...
I did some research and figured out the way for finding and deleting the files older than 30 days from a specific path, using find and exec commands.
*find /export/home/ftp/ -type f -mtime +30 -exec rm -f {} \;
But according to the requirement I want to delete console.log and server.log only from that directory and exclude the remaining files.
Please help me out in this issue.
-
Ignacio Vazquez-Abrams over 13 yearsSo then is your title inaccurate?
-
-
sinping over 13 yearsYou could make it a little more efficient by adding -maxdepth 1 so that it doesn't actually recurse since you know which two files you want to check exactly.
-
jgoldschrafe over 13 yearsI always forget that find has a
-delete
option. So let's amend that outright to:find /export/home/ftp -maxdepth 1 \( -name console.log -or -name server.log \) -mtime +30 -delete
-
Jayakrishnan T over 13 yearscould u pls tell any link to study shell script easily.