Home
Man
Search
Today's Posts
Register

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

Tags
advanced, mtime

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-23-2011
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
# 2  
Old 06-23-2011
Try

PHP Code:
find /home/amit/log/ -mtime +30 -exec rm {} \; 
# 3  
Old 06-24-2011
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

The Following User Says Thank You to agama For This Useful Post:
Kibou (09-21-2015)
# 4  
Old 06-24-2011
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 04:25 AM..
The Following User Says Thank You to hnrz For This Useful Post:
Kibou (09-21-2015)
# 5  
Old 06-25-2011
thank you guys for your tips
i tried
Code:
find . -mtime +30 | xargs rm

and it worked perfectly!
# 6  
Old 06-28-2011
Code:
find . -type f -mtime +30 | xargs rm

Is safer.
# 7  
Old 06-29-2011
yes i agree but since i call the argument rm and not rm -r then it cannot delete directories anyway
The Following User Says Thank You to omonoiatis9 For This Useful Post:
Kibou (01-19-2015)
Login to Reply

« Previous Thread | Next 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 not matching multiple patterns and then delete anything older than 10 days wahi80 Shell Programming and Scripting 4 06-01-2016 06:13 PM
Find all log files under all file systems older than 2 days and zip them saurabh.mishra UNIX for Dummies Questions & Answers 4 08-29-2013 05:31 AM
Writte a script to copy the files older than 7 days using find and cp patricio181 Shell Programming and Scripting 5 05-02-2013 01:19 PM
Find command to search and delete files older than 1 days at a desired location Pandee Shell Programming and Scripting 3 01-16-2013 06:29 AM
Find and delete files and folders which are n days older from one unix server to another unix server sachinkl Shell Programming and Scripting 2 03-12-2012 11:02 AM
find file older than one month not by x days olds jao_madn Shell Programming and Scripting 2 12-03-2011 06:08 PM
How do I find files which are older than 30 days and greater than 1GB Hangman2 UNIX for Dummies Questions & Answers 4 08-30-2011 06:58 AM
Files older than 50 days alok.behria UNIX for Dummies Questions & Answers 4 07-25-2011 04:13 PM
Find files older than 10 days Pouchie1 UNIX Desktop Questions & Answers 4 08-19-2009 07:55 PM
How to find files older than 30 days anushree.a Shell Programming and Scripting 4 07-08-2009 09:43 AM
Find files older than 30 days in directories and delete them lamoul Red Hat 2 03-21-2009 02:44 PM
Find files older than x days and create a consolidated single tar file. Dawn Bosch Solaris 3 09-09-2008 06:45 AM
Find files older than 20 days & not use find halo98 Shell Programming and Scripting 2 05-18-2006 03:19 PM
Find files older than 5 days and remove tem after listing ypatel6871 UNIX for Dummies Questions & Answers 1 09-05-2005 12:00 PM
How can I delete files older than 7 days? odogbolu98 UNIX for Dummies Questions & Answers 3 02-26-2002 08:35 PM


All times are GMT -4. The time now is 11:51 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password