Solved

Remove files older 'x' duration

Posted on 2004-04-01
9
392 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
find <dir> -amin +3 -type f -exec rm {}
ignore the trailing r in the last post
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
sunnycoder,
> -amin

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

Stefan
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:sunsubbu
Comment Utility

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
Comment Utility
Hi,

      Please share any minimum clue on this..

Thanks
Subbu
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
what's wrong with Stefan's perl suggestion?
0
 
LVL 45

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 26
Unix - help validating the format of an incoming file 10 58
FTP on FreeBSD server 2 90
Sed question 2 45
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now