Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove files older 'x' duration

Posted on 2004-04-01
9
Medium Priority
?
402 Views
Last Modified: 2013-12-27
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
0
Comment
Question by:sunsubbu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 1500 total points
ID: 10730338
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10730393
find <dir> -amin +3 -type f -exec rm {}r

the only problem is that time (+3) is in minutes and not seconds
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10730400
find <dir> -amin +3 -type f -exec rm {}
ignore the trailing r in the last post
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 10730745
sunnycoder,
> -amin

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

Stefan
0
 

Author Comment

by:sunsubbu
ID: 10731108

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
 

Author Comment

by:sunsubbu
ID: 10731685
Hi,

      Please share any minimum clue on this..

Thanks
Subbu
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10731702
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10744537
what's wrong with Stefan's perl suggestion?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10746820
nothing is wrong ahoffmann ... the requirement has been modified a little
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question