Friday, March 11, 2011

Linux Delete Files/Folder Older Than n Days

[root@localhost /]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

Test it:
1, create a new folder
mkdir 20090909

[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Mar 11 16:25 20090909

2, change folder time
touch -t 200909090909 20090909/

3, check time
[root@localhost xxx]# touch -t 200909090909 20090909/
[root@localhost xxx]# ll
drwxr-xr-x 2 root root 4096 Sep 9 2009 20090909

4, delete files and folder older than 30 days

[root@localhost xxx]# find /your/folder/ -mtime +30 -exec rm -rf {} \;

[root@localhost xxx]# ll
drwxr-xr-x 2 esl esl 20480 Mar 11 16:21 20110311

No comments:

Post a Comment