Find and delete / remove files older specific days on Linux

Find and delete / remove files older specific days 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:33 .
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
-rw-r–r–. 1 root root 0 Oct 3 13:05 file05
-rw-r–r–. 1 root root 0 Oct 3 13:05 file06
-rw-r–r–. 1 root root 0 Oct 3 13:05 file07
-rw-r–r–. 1 root root 0 Oct 3 13:05 file08
-rw-r–r–. 1 root root 0 Oct 3 13:05 file09
-rw-r–r–. 1 root root 0 Oct 3 13:05 file10
[root@localhost server]#

Today is

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

List file with full day

[root@localhost server]# ls –full-time

total 0
-rw-r–r–. 1 root root 0 2012-10-03 13:05:00.000000000 -0700 file09

 

2. How to Find and delete / remove files older specific days on Linux

90 days

[root@localhost server]# find /root/server -type f -mtime +90 -exec rm -rf {} \;

365 days

[root@localhost server]# find /root/server -type f -mtime +365 -exec rm -rf {} \;

3 days

[root@localhost server]# find /root/server -type f -mtime +3 -exec rm -rf {} \;

100 days

[root@localhost server]# find /root/server -type f -mtime +100 -exec rm -rf {} \;

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

 

One comment to Find and delete / remove files older specific days on Linux

  • yacleouf  says:

    I think that you can do this by a shell script, put variables on the mtime arguments, and then you can pilot your shell script by a config file !

    Regards

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>