1748169 Members
4231 Online
108758 Solutions
New Discussion

Re: Script

 
SOLVED
Go to solution
Khashru
Valued Contributor

Script

i need to write a script that will go each folder under a certail folder and delete all files that are more then 4 hours old. all these folders also have folder benith that.

Please help
5 REPLIES 5
Prashant Zanwar_4
Respected Contributor

Re: Script

You can get list of directories using:

find . -type +d -xdev -exec ls -ld {} \;

there is no command to search 4hours old file, you will need to write a script to see if file is 4hours old or no. Compare time stamps.. etc

Regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
spex
Honored Contributor

Re: Script

Hi,

The following script will work provided that it's executed at or after 04:00:

#!/usr/bin/sh
touch -t $(date +%m%d$(expr $(date +%H) - 4)%M) /tmp/ref.${$}
find /dir -type f ! -newer /tmp/ref.${$} -exec rm -f {} \;
rm -f /tmp/ref.${$}
exit

If this restriction is unacceptable, use Clay's Date Hammer script to generate the reference file.

PCS
Bill Hassell
Honored Contributor

Re: Script

Actually, there is a method to locate files that are just a few hours or even a few minutes old. find has an option: -newer and to use it, you create a temporary reference file at the age you'd like use. Use touch to do this. You can create or change the time stamp of any file in any direction. The format is YYYYMMDDHHMM (and some variants - see the man page). Current time is 2:52 GMT so a touch command 2 hours ago would be 0:52 and the touch command would be:

touch -t 200609200052 timeREF

To find any files newer than timeREF:

find . -newer timeREF -type f -exec ls -ld {} \;

Change -type f to -type d to find directories.


Bill Hassell, sysadmin
spex
Honored Contributor
Solution

Re: Script

Oops. 'touch' expects a two-digit hour. This should work better:

#!/usr/bin/sh
NOW_m_d=$(date +%m%d)
NOW_H=$(date +%H)
NOW_M=$(date +%M)
THEN_H=$(( ${NOW_H}-4 ))
if [[ ${THEN_H} -lt 10 ]]
then
THEN_Z=0
fi
touch -t ${NOW_m_d}${THEN_Z}${THEN_H}${NOW_M} /tmp/ref.${$}
find /dir -type f ! -newer /tmp/ref.${$} -exec rm -f {} \;
rm -f /tmp/ref.${$}
exit

I've also attached A. Clay's Date Hammer script to this posting.

PCS
Jaydeb Chakraborty
Occasional Advisor

Re: Script

$ touch -d "13 may 2001 17:54:19" date_marker
change the date 4Hr back of current time in above format.
To find files created before that date, use the cnewer and negation conditions:
$ find . \! -cnewer date_marker

Make a list and be confirming itâ s fulfilling your expectation. Then you may proceed to delete those.

Thanks!