Remove files older 'x' duration

Hi All,

 I want to remove files which are older than 'x' duration from the given directory

here 'x' is in seconds

any inputs would be appreciated

Thanks
Subbu
sunsubbuAsked:
Who is Participating?
 
stefan73Connect With a Mentor Commented:
Hi sunsubbu,
As Solaris comes with Perl, you can do:

perl -e '$now=time();for(glob("*")){print "$_\n" if $now-(stat($_))[9] > 1000;}'

This will print all files in the current directory (modify the glob for other paths) which are older than 1000 seconds. Similarly, you can use the unlink command to remove the files:

perl -e '$now=time();for(glob("*")){unlink $_ if $now-(stat($_))[9] > 1000;}'


Cheers,
Stefan
0
 
sunnycoderCommented:
find <dir> -amin +3 -type f -exec rm {}r

the only problem is that time (+3) is in minutes and not seconds
0
 
sunnycoderCommented:
find <dir> -amin +3 -type f -exec rm {}
ignore the trailing r in the last post
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
stefan73Commented:
sunnycoder,
> -amin

I think that's GNU find. Solaris find is less cozy :-/

Stefan
0
 
sunsubbuAuthor Commented:

Hi Stefan & sunnycoder,

   Thanks for your reply, but little modification is there on my question..the question is
i want to list the files which were modified in a time interval
i.e
say for example
 we need two options. -mfrom X and -mto Y where X & Y are in seconds relative to current time

 here 'mfrom' & 'mto' are in seconds..

For example, user will give following option
              -mfrom 7200 -mto 3600
that means if script runs at 2 PM it will list the files modified between 12 to 1

If user would like to list the files modified a day before then user will give -mto 86400

any help on this is greatly appreciated

Thanks & Regards
Subbu
0
 
sunsubbuAuthor Commented:
Hi,

      Please share any minimum clue on this..

Thanks
Subbu
0
 
sunnycoderCommented:
as Stefan said solaris find is does not provide much options but if the previous command worked for you, then try this

find <dir> -amin +3 -amin +5 -type f -exec rm {}

this will remove all file accessed between last 3 to 5 minutes

use ctime if you want modification time instead
0
 
ahoffmannCommented:
what's wrong with Stefan's perl suggestion?
0
 
sunnycoderCommented:
nothing is wrong ahoffmann ... the requirement has been modified a little
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.