find files older than 30 days old | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

find files older than 30 days old

UNIX for Advanced & Expert Users


Tags
mtime

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-23-2011
omonoiatis9 omonoiatis9 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 2 September 2014, 1:37 AM EDT
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
find files older than 30 days old

Hello,

I have a script which finds files in a directory that are older than 30 days and remove them.
The problem is that these files are too many and when i run this command:

Code:
 
find * -mtime +30 | xargs rm

I run this command inside the directory and it returns the error:
/usr/bin/find: 0403-027 The parameter list is too long.

Is there another command instead of find which does not produce this kind of problem?

Thank you for your attention
Sponsored Links
    #2  
Old 06-23-2011
amitranjansahu's Avatar
amitranjansahu amitranjansahu is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 6 February 2014, 9:06 AM EST
Location: Bangalore,INDIA
Posts: 507
Thanks: 4
Thanked 27 Times in 27 Posts
Try

PHP Code:
find /home/amit/log/ -mtime +30 -exec rm {} \; 
Sponsored Links
    #3  
Old 06-24-2011
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
You are wise to use xargs as it's much more efficient. The "splat" (*) on your command line is expanding to all files in the directory. What you want is to search in your current directory:


Code:
find . -mtime +30 | xargs rm

    #4  
Old 06-24-2011
hnrz hnrz is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 24 June 2011, 4:22 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
If the version of find being used supports it, I usually use the -delete option as it is usually even faster. Implementations differ but usually the -exec option of find does a fork() and then an execvp(), and then waits on the child process. It starts a child process for each file which makes it slow. xargs does the same thing but calls vfork() and concatenates the argument list and usually only creates one child process. The -delete option simply does an rmdir() or unlink() as it is walking the filesystem tree and it is usually as fast or faster than xargs in my experience. Also by using -delete you don't run into the number of arguments hard limit that xargs imposes (or rather execvp(3)). By default, -delete deletes both files and directories, so if the intention is only to delete files, the -type parameter must also be specified. Of course, if it is a script that is being run often, it doesn't hurt to profile and determine which is best.

I usually do:

Code:
find . -type f -mtime +30 -delete


Last edited by hnrz; 06-24-2011 at 03:25 AM..
Sponsored Links
    #5  
Old 06-25-2011
omonoiatis9 omonoiatis9 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 2 September 2014, 1:37 AM EDT
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
thank you guys for your tips
i tried
Code:
find . -mtime +30 | xargs rm

and it worked perfectly!
Sponsored Links
    #6  
Old 06-28-2011
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 672 Times in 642 Posts

Code:
find . -type f -mtime +30 | xargs rm

Is safer.
Sponsored Links
    #7  
Old 06-29-2011
omonoiatis9 omonoiatis9 is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 2 September 2014, 1:37 AM EDT
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
yes i agree but since i call the argument rm and not rm -r then it cannot delete directories anyway
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find files older than 10 days Pouchie1 UNIX Desktop for Dummies Questions & Answers 4 08-19-2009 06:55 PM
How to find files older than 30 days anushree.a Shell Programming and Scripting 4 07-08-2009 08:43 AM
Find files older than 30 days in directories and delete them lamoul Red Hat 2 03-21-2009 01:44 PM
Find files older than 20 days & not use find halo98 Shell Programming and Scripting 2 05-18-2006 02:19 PM
Find files older than 5 days and remove tem after listing ypatel6871 UNIX for Dummies Questions & Answers 1 09-05-2005 11:00 AM



All times are GMT -4. The time now is 07:42 AM.