Operating System - HP-UX
1754324 Members
2673 Online
108813 Solutions
New Discussion юеВ

Delete files using time stamp

 
SOLVED
Go to solution
Muktha
Frequent Advisor

Delete files using time stamp

Hi all ,

I want to delete the files of 2004 from my directory?Is there any way to remove the files using time stamp?
Could you please help me to find out a solution?

Regards
Muktha
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Delete files using time stamp

Hi Muktha:

The following solutions assume the you want to REMOVE any file last modified in 2004. To test, change 'rm' to 'echo'.

# cd /path
# ls -al|awk '/^-/ && $8~2004 {print $NF}'|xargs -i rm {}

This works for the specified directory and not for files in subordinate directories.

If you do want to descend into all subordinate directories, simply use:

# cd /path
# find . -type f -exec ls -al {}+|awk '/^-/ && $8~2004 {print $NF}'|xargs -i rm {}

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Delete files using time stamp

Hi (again) Muktha:

In keeping with TMTOWTDI, here's a pure Perl solution you could use. Change the value of 2004 to any year you want, *including* the current year.

Change the 'print' to 'unlink' and instead of actually printing the file names you will *remove* them:

# perl -MFile::Find -le '$Y=2004;find(sub{-f $_ && ((localtime((stat(_))[9]))[5]+1900==$Y) && print $File::Find::name},@ARGV)' /path

You can also pass multiple paths as arguments or a dot ('.') to signify (of course) the current directory.

Regards!

...JRF...

Adam W.
Valued Contributor

Re: Delete files using time stamp

I love referencing this. Touch.


To remove dated files. Can be modified to remove sized files as well.


# touch -amt 200504150000 /tmp/ref1
# touch -amt 200802282359 /tmp/ref2

# find /opt/mtmc/ibs/import/reject -xdev -type f -newer /tmp/ref1 -a ! -newer /tmp/ref2 -exec rm {} \+

This will remove files that are newer (more recently modified) than April 15 at 0000 AND NOT newer than Feb, 28th 2359 of this year



time Use the specified time instead of the current time.
The option argument is a decimal number of the form

[[CC]YY]MMDDhhmm[.SS]

where each two digits represents the following:

CC The first two digits of the year.

YY The second two digits of the year.

MM The month of the year (01-12).

DD The day of the month (01-31).

hh The hour of the day (00-23).

mm The minute of the hour (00-59).

SS The second of the minute (00-61).

If neither CC nor YY is given, the current year is
assumed. If YY is specified, but CC is not, CC is
derived as follows: (taken into account the local time
factor)

If YY is: CC becomes:
_______________________
69-99 19
00-68 20


If the resulting time value precedes the Epoch
(00:00:00 January 1, 1970 Greenwich Mean Time), touch
exits immediately with an error status.

The range for SS is 00 through 61 rather than 00
through 59 to accommodate leap seconds. If SS is 60 or
61, and the resulting time, as affected by the TZ
environment variable, does not refer to a leap second,
the resulting time is one second after a time where SS
is 59. If SS is not given a value, it is assumed to be
0.
There are two types of people in the world, Marines and those who wish they were.
Muktha
Frequent Advisor

Re: Delete files using time stamp

Thanks to all...