1848033 Members
3101 Online
104022 Solutions
New Discussion

file cleanup

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

file cleanup

Hello,

How do I remove/delete files older than 1 hour or last modified was 1 hour ago?

Thanks in advance
quest for perfections
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: file cleanup

Hi:

You can do this with 'find'. 'touch' a reference file and then search for files that are older than it:

cd
# touch -m -t 12041300 myref
# find . -type f ! -newer /tmp/ref -exec rm -i {} \;

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: file cleanup

Hi:

I have a Perl utility fileage.pl which can tell if a file has been accessed, modified, or changed in a period of time.

Here is one approach:

cd to desired directory:
find . -type f -print | while read X
do
fileage.pl -m 3600 ${X}
STAT1=$?
fileage.pl -c 3600 ${X}
STAT2=$?
if [ ${STAT1} -eq 0 -a ${STAT2} -eq 0 ]
then
echo "File ${X} Has not been modified in \c"
echo "3600 seconds"
# rm -f ${X}"
fi
done

Fileage.pl returns 0 if the file has not been modified and 1 if it has. Simply invoke fileage.pl without arguments for full usage. Note: The rm is commented out. Test first.

Regards, Clay
If it ain't broke, I can fix that.
G. Vrijhoeven
Honored Contributor

Re: file cleanup

Hi,

linux has the find . -cmin n optio, hp does not, create a referention file every hour and,

find . -newer file -exec rm {}\;

Hope this will help,

Gideon
A. Clay Stephenson
Acclaimed Contributor

Re: file cleanup

Ooops, the remove line should not have a trailing "

should be:
rm -f ${X}
If it ain't broke, I can fix that.
Ridzuan Zakaria
Frequent Advisor

Re: file cleanup

Thanks guys.
quest for perfections
Kofi ARTHIABAH
Honored Contributor

Re: file cleanup

You could try the following:

BASEFILE=/tmp/dummyfile
HERE=/path/to/directory/to/start/from
now=`date +%m%d%H%M`
hourago=`expr $dt - 100`
touch -t $hourago $BASEFILE
cd $HERE
find . -type f ! -newer $BASEFILE -exec rm {} \;

(note that you can replace rm with ls to list the files rather than delete them)

good luck.
nothing wrong with me that a few lines of code cannot fix!
Kofi ARTHIABAH
Honored Contributor

Re: file cleanup

Oooooppsss... in my last post, it should be now rather than dt:

hourago=`expr $now - 100`
nothing wrong with me that a few lines of code cannot fix!