1758580 Members
1782 Online
108872 Solutions
New Discussion юеВ

Re: More Time

 
SOLVED
Go to solution
Pete Randall
Outstanding Contributor

Re: More Time

Sorry, I missed your second question.

To test, replace the "xargs rm" with "xargs ll".


Pete

Pete
Stephen Keane
Honored Contributor

Re: More Time

Which won't change the access time, so isn't a perfect test.
James R. Ferguson
Acclaimed Contributor

Re: More Time

Hi (again) Rob:

Yes, Pete is correct, the "!" symbol is negation, so "! -newer" is "not newer" or hence, older.

Yes, of course, deleting a file from a directory updates the directory's *modification* ('mtime'). Traversing (reading) a directory with 'find' updates the directory's lastaccess time ('atime').

By default the '-newer' argument to 'find' operates on the structures 'mtime'.

The idea I had was to make a pass to delete *files* matching your criteria and then in a second pass delete empty directories. The 'rmdir' will fail if the directory isn't empty which is what you want if files have been *added* anyway.

Regards!

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

Re: More Time

Hi Rob:

I must chuckle. Stephen's comment about there's more than one way to do anything is so appropriate!

Computing a file's timestamp a few minutes ago is so easy that I focused on that approach rather than a continuous crontask that simply creates its reference point the first time and nothing more.

Stephen deserves high marks here!

In any event, to be perfectly correct at the turn of a year, I should offer:

#!/usr/bin/perl
$t=((time)-300);
($year,$mon,$day,$hr,$min)=(localtime($t))[5,4,3,2,1];
$year+=1900;
$mon++;
printf("%04d%02d%02d%02d%02d\n",$year,$mon,$day,$hr,$min);

Calling the above script whatever you want (e.g. $HOME/time5) you can abbreviate your shell's creation of a reference file to:

# touch -amt `$HOME/time5` /tmp/myref

Regards!

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

Re: More Time

Hi (once more):

I'm sorry, I just noticed your other question, "What can I put on the end to replace the xargs rm and xargs rmdir that will show me which files are fixing to be deleted before I actually try and run a test of this?"

Simply write "...|xargs" without any other arguments. 'xargs' will substitute 'echo' in the absence of any argument.

Another useful feature of 'xargs' is to add a '-t' option to cause a trace of what was done to be printed:

... | xargs -t rm

Regards!

...JRF...
Rob Blake
New Member

Re: More Time

Thanks for all the great help and ideas. So far the resulting two(2) scripts have been created and tested manually against our development environment:

SCRIPT: temp_time
#!/usr/bin/perl
$t=((time)-300);
($mon,$day,$hr,$min)=(localtime($t))[4, 3, 2, 1];
$mon++;
printf("%02d%02d%02d%02d\n",$mon,$day,$hr,$min);
exit;

SCRIPT: clear_up
#!/bin/ksh
#####
# Remove old reference file
#####
rm /tmp/myref

#####
# Setup home and target directories
#####
START=${HOME}
TARGET=

#####
# Run temp_time to get the date time less 5 minutes
#####
TS=`${START}/temp_time`

#####
# Touch to create a reference file in temporary directory
#####
touch -amt ${TS} /tmp/myref

#####
# find and delete all files, directories, and
# sub-files older than the reference file.
#####
find ${TARGET} -xdev -type f ! -newer /tmp/myref | xargs rm
find ${TARGET} -xdev -type d -newer /tmp/myref | xargs rmdir

#####
# Exit script
#####
cd ${START}

exit 0

Will be copyng these to the pre-production environment to test maually and via crontab of clear_up script before moving to Production. Thanks to everyone for their input and assistance on this. Believe me - the UNT students will appreciate it.
James R. Ferguson
Acclaimed Contributor

Re: More Time

Rob:

I suggest you use the second perl script I posted which covers the change of the year.

Glad to have been of help...

Regards!

...JRF...
Rob Blake
New Member

Re: More Time

james - thanks for the advice. Changes made as suggested to temp_time.