Deleting files older than 15 Days

thomastull

Verified User
Joined
Feb 11, 2014
Messages
10
Hello

I am new to this forum. I am stuck in a script and need help with you guys.

I need to design a shell/batch script in which I have to delete some files older than 15 days except php or txt or xls.
The script should search for files older than 15 days in /home/username/public_html/subfolders/*.doc (subfolders in public_html) or in a folders above public_html.
Excluding files in public_html...Eg. /home/username/public_html/*.php
 
Hello

Thanks for the quick response.
But the problem is I need to remove files older than 15 days from subfolders under public_html i.e /home/admin/public_html/subfolder/*.doc or /home/admin/subfolder above public_html.
It should not delete data in public_html directory.
 
Then start with

Code:
find [COLOR=#333333]/home/admin/public_html/subfolder/*.doc -mtime +15

and end as suggested by Martinas.[/COLOR]
 
Code:
find /home/user/domains/domain.com/public_html -name \*.doc -mtime +15 -exec rm -rf {} \;

or

Code:
find /home/user/domains/domain.com/public_html -not -name \*.php -not -name \*.xml -not -name \*.txt -mtime +15 -exec rm -rf {} \;
 
Last edited:
Thank you scsi for commands, but I would like to suggest not to use "rm -rf" to remove files found, add add "/subfolder" after public_html per tomastull's requirement. Everything else looks fine.
 
Thanks all.
But I dont know the subfolder names.There are so many subfolders in public_html..I want it will automatically checks for files older than 15 days in subfolders under public_html or above public_html leaving php/txt/xls everywhere.
And script should not touch any files in public_html.
 
Use this then:

Code:
find /home/*/domains/*/public_html/*/*.doc -mtime +15

if you need to list only *.doc files in all subfolder in all public_html of all users. Or modify it to your needs.
 
Directory structure is like:

cd /home/username/domain/
Here there are so many folders dir1,dir2,dir3,dir4,public_html....I want that it will check for all files older than 15 days in dir 1,2,3,4 and delete *.doc and *.ppt.
Then it will go to public_html and do not delete anything except doc and ppt..
Under public_html I have dir5,dir6,dir7,*.php,*.xls, *.ppt...Here it will delete older files in dir5,6,7 or doc ppt here.

I dont know the subdir names but it will check automatically subdirectories and delete.
Subdir will be deleted and created on daily basis depending on work.


But according to your command find /home/*/domains/*/public_html/*/*.doc I need to mention subdirectory name here under public_html. But sub directory will be created deleted on daily basis and it would be difficult to write for every sub directory manually.
 
You should not replace asterix with anything else there. That works as is. I only was referring to extension of files you might want to find.
 
I have files like below:
/home/user/domains/domain.com/public_html/d67/fght.doc
/home/user/domains/domain.com/public_html/dk/ffbgf.doc
/home/user/domains/domain.com/public_html/dfv.doc
/home/user/domains/domain.com/vdv.doc
/home/user/domains/domain.com/d1/vrd.doc



find /home/user/domains/domain.com/public_html/*/*.doc - It is listing doc files in subdirectories under public_html directory.

Result : /home/user/domains/domain.com/public_html/d67/fght.doc
/home/user/domains/domain.com/public_html/dk/new.doc


I need that it will list doc files in /home/user/domains/domain.com/public_html/d67/*.doc and home/user/domains/domain.com/public_html/*.doc
and home/user/domains/domain.com/subdirectories/*.doc and home/user/domains/domain.com/*.doc
 
Hello
The second commands find /home/*/domains/*/public_html/* -mindepth 1 -name \*.doc -print make it easier for me to check .doc files in subdirectories excluding .doc files in public_html.
it gave output as below which I need to list :

/home/user/domains/domain.com/public_html/d67/fght.doc
/home/user/domains/domain.com/public_html/dk/ffbgf.doc
/home/user/domains/domain.com/public_html/dfv.doc

Ignoring the /home/user/domains/domain.com/public_html/u.doc working the same way I want.


But I need to list some files above public_html i.e.
/home/user/domains/domain.com/vdv.doc
/home/user/domains/domain.com/d1/vrd.doc

How to list them with mindepth or maxdepth. By any of these it is including /home/user/domains/domain.com/public_html/u.doc which I need to ignore.
 
In my previous post there are two commands, they cover the both cases . So you may modify it to:

Code:
[COLOR=#333333]find /home/*/domains/* -maxdepth 2 -name \*.doc -print | grep -v /public_html/
[/COLOR]
 
Back
Top