Operating System - HP-UX
1753726 Members
4670 Online
108799 Solutions
New Discussion юеВ

Re: Find command to delete files in dir

 
SOLVED
Go to solution
HP-UX_Ali
Regular Advisor

Find command to delete files in dir

Hello all,

Request proper command to delete the files in directory.
scenario : keep 6 months files till date & delete all the files 6 months before.

Thanks to confirm..

11 REPLIES 11
HP-UX_Ali
Regular Advisor

Re: Find command to delete files in dir

hello all

is this command will work

find . -mtime +180 -exec rm -rf {} \;

regards
S-M-S
Valued Contributor

Re: Find command to delete files in dir

Bill Hassell
Honored Contributor

Re: Find command to delete files in dir

>> find . -mtime +180 -exec rm -rf {} \;

I would not use the above command until you clarify your requirements. Do you want all files *and* directories *and* subdirectories deleted that are more than 180 days old? If not, you will need to add more options. For example, to delete only ordinary files, add -type d to the find command.


Bill Hassell, sysadmin
Pete Randall
Outstanding Contributor

Re: Find command to delete files in dir

> "-type d" ???

Maybe "-type f" ????


Pete

Pete
Prasanth V Aravind
Trusted Contributor

Re: Find command to delete files in dir

i suggest to take list of those files you going to delete & double check it before actual deletion.

Command to list the files:-

find . -mtime +180 -type f -print

Gudluck
Prasanth


HP-UX_Ali
Regular Advisor

Re: Find command to delete files in dir

Hi Bill.

I want to keep the files of six months till date.
example: today is may6 and till november 6 2009 i have keep all of them. and to remove all files NOT directory.

In my scenario there are many files under many directories , i dont want to delete the directories. and my files are from 2007 & 2008 etc. so i have keep latest 6 month data & delete only files from directories & other directories
Pete Randall
Outstanding Contributor

Re: Find command to delete files in dir

Then you would want:

find /startdir -type f -mtime +180 -exec rm {} \;

- or -

find /startdir -type f -mtime +180 |xargs rm

The second form cuts down a bit on resource usage.

And you should test first by replacing the "rm" with "ll".


Pete

Pete
HP-UX_Ali
Regular Advisor

Re: Find command to delete files in dir

Hi Pete,

Thanks a lot for your quick response...

I got it... :)

regards

HP-UX_Ali
Regular Advisor

Re: Find command to delete files in dir

hi Pete

if i wanna exclude particular directory then what will be the syntax

regards