Operating System - HP-UX
1833312 Members
2871 Online
110051 Solutions
New Discussion

Finding and removing directories

 
SOLVED
Go to solution
Terrence
Regular Advisor

Finding and removing directories

Can anyone help me with a piece of script that finds directories over a certain number of days old? I need to remove data that's generated every day into a new directory. I've used the find command, but it's only picking up files, and leaving directories.

DIR="/var/data/archive"
DAYS="45"
find $DIR -type f -mtime +$DAYS | xargs rm

Any ideas on how to remove entire directories and their contents based upon their creation date?
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Finding and removing directories

Hi,

Try "-type d" and "xargs rm -rf"

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Martin Johnson
Honored Contributor

Re: Finding and removing directories

find $DIR -type d -exec rm -fR {} \;

should work

HTH
Marty
Darrell Allen
Honored Contributor

Re: Finding and removing directories

Leave off the "-type" arg if you want to remove files and directories (using "rm -rf" as Sri suggests).

If you want to delete stuff that is more than 45 days old, you need to change "+$DAYS" to "-$DAYS".

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: Finding and removing directories

Oops! You're right, +$DAYS.

Don't know what I was thinking. Guess I wasn't. Sorry.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Terrence
Regular Advisor

Re: Finding and removing directories

Okay youse guys are freaky fast. Thanks for the primer on the find command.

Darrell good thing you chimed in because I was going to bust your chops on the +- argument. (I knew that part worked fine) You get 10 points for the shortest command.

Tanks all!
A. Clay Stephenson
Acclaimed Contributor

Re: Finding and removing directories

I suppose that I should correct a small misconception. Believe it or not there is no creation date in UNIX. You have no way (unless accidently to know when a file was created). The -ctime refers to the change time (e.g. the last time the inode was chmod'ed) (which to add to the confusion ain't the same as modification time - the last time the file itself was written to.
If it ain't broke, I can fix that.
Darrell Allen
Honored Contributor

Re: Finding and removing directories

Some days I just don't think straight any more. Don't know if it's old age, Alzheimer's, or what.

Consider chops busted (and rightfully so) although you probably can't imagine how heavy I came down on myself for such a stupid reply.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)