System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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
Dennis Handly
Acclaimed Contributor
Solution

Re: Find command to delete files in dir

>if I want to exclude particular directory then what will be the syntax:

Is that directory name unique and do you want to exclude subdirectories too?
find /startdir -name exclude-dir -prune -o -type f -mtime +180 -exec echo rm {} +

Anything in exclude-dir or deeper would be skipped. Remove "echo" after you verified it works.

If that directory wasn't unique, you would need to use -path.

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

Using "-exec ... +" would be much faster:
find /startdir -type f -mtime +180 -exec rm {} +

I recently had to remove 100,000 messages out of /var/spool/mqueue/ and "-exec ... +" was much faster than xargs. And I was using -n1000.
HP-UX_Ali
Regular Advisor

Re: Find command to delete files in dir

I have found the solution & hence closing the thread.