Find and delete / remove files older 30 days / 1 month on Linux

Find and delete / remove files older 30 days / 1 month on Linux

1. List files in folder server with modified date

[root@localhost server]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Jan 6 10:06 .
dr-xr-x—. 28 root root 4.0K Jan 6 10:04 ..
-rw-r–r–. 1 root root 0 Oct 3 13:05 file01
-rw-r–r–. 1 root root 0 Oct 3 13:05 file02
-rw-r–r–. 1 root root 0 Oct 3 13:05 file03
-rw-r–r–. 1 root root 0 Oct 3 13:05 file04
[root@localhost server]#

Today is

[root@localhost server]# date
Sun Jan 6 10:14:06 PST 2013
[root@localhost server]#

2. How to remove files older 30 days / 1 month

[root@localhost server]# find /root/server -type f -mtime +30 -exec rm -rf {} \;
[root@localhost server]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Jan 6 10:15 .
dr-xr-x—. 28 root root 4.0K Jan 6 10:04 ..
[root@localhost server]#

That’s all. Thanks for using IThelpblog.com.

 

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>