Operating System - HP-UX
1763708 Members
2936 Online
108915 Solutions
New Discussion юеВ

Simple question about eliminating files!

 
SOLVED
Go to solution
MAD_2
Super Advisor

Simple question about eliminating files!

OK, here is a simple one for most of you. I would like to use a command that can sort just certain files from a directory and then elminate those files meeting certain criteria.

I have used the command "find /directory/filepattern -mtime +days -print -exec rm -f {} \;"

However, when I try this one the message I get is "/usr/bin/find: arg list too long". The number of files meeting the pattern given is very (I mean very) long.

What other command may I use that will not care about the number of files found?

Thanks,

ADAM
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
4 REPLIES 4
MAD_2
Super Advisor

Re: Simple question about eliminating files!

OK, I guess I found an answer, and probably some of the problem may have been my own syntax, this is what I am using now:

"find /dir/subdir -name "*filepattern*" -mtime +daysold -print -exec rm -f {} \;

This seems to work, other ways to do it?
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
James R. Ferguson
Acclaimed Contributor
Solution

Re: Simple question about eliminating files!

Hi Mynor:

You are probably running a 10.x environment. The limitation of ARG_MAX is substantially relieved on 11.x. With that aside, there is an easy solution which is also much less resource intensive. Using 'exec' means a new process is spawned for every argument. To avoid the limitation of ARG_MAX and to avoid a new process for every argument, do this:

# find /directory/filepattern -type f -mtime +days | xargs -n 100 rm -f

Have a look at the man pages for 'xargs' for more details. Essentially this will process 100 arguments at a time until the list is exhausted.

Regards!

...JRF...
Helen French
Honored Contributor

Re: Simple question about eliminating files!

Hi Mynor,

For safety purpose, NEVER put the -exec option (especially with rm -f) until you are sure the find actually returns the names you expect. A catastrophic result will take place if you accidently put a space in front of the * for your -name parameter !

However, you can use 'xargs' command instead of -exec, if needed.

find /dir_path -name '*pattern*' -depth -mtime days -print | xargs rm -f {}

See man pages of xargs.

HTH,
Shiju
Life is a promise, fulfill it!
MAD_2
Super Advisor

Re: Simple question about eliminating files!

Thank you both.

This is exactly what I was looking for.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with