Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory pruning script 1

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
0
0
GB
Hi there, excuse this newbie question but I have a directory full of log files that gets added to all the time, I just wondered if someone could point me in the right direction to writing a script that will delete all files that have a modified date of older than one week?, I will run the script at the end of each day using cron

any help on this would be greatly appreciated

cheers
Gary
 
Something like the following?
Code:
find . \( -type d ! -name . -prune \) -o \( -type f ! -mtime -1 -ok rm {} \; \)
Change the ok to exec once you are satisfied that only the files you intend to delete will be deleted...
Cheers, Neil
 
Oops, instead of

"! -mtime -1"

I think you can use

"-mtime +7"

Cheers, Neil
 
thanks for that , I set up a test directory (a copy of the actual log directory) and ran the following

find . \( -type d ! -name . -prune \) -o \( -type f ! -mtime +7 -exec rm {} \; \)

Unfortunately it deleted everything, I have repopulated the directory again for testing,

I take it mtime is a standalone binary and not a switch for find? if so , when i do a 'which mtime' no such program is found

I am running solaris 8. Do i need to install this or is there another way around this

cheers
Gary
 
Note that the second example has removed the "!".

So instead of "! -mtime -1", it is "-mtime +7"

Cheers, Neil
 
I have the command how to delete empty directories within a directory. But I need to add the criteria that the creation date of the directory must be older than 1 day from today.

find ${purge_dir_path} -type d -depth -exec rmdir {} \;

How to add this criteria? How to change the creation time of a directory?
 
There is no creation time in unix.
The time related primaries for find are:
atime last access
ctime inode change
mtime modifiation

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello PHV,

Thank you for your info.

I want to know:

1. Is there any command such as "touch" to modify the mofication time of a directory?

2. How to add the logic also to check the last modification time of the directory to the following command? The last modifcation time of the directory to be purged must greater than yesterday's date.

find ${purge_dir_path} -type d -depth -exec rmdir {} \;
 
man touch
man find

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I figure out myself. The

find "/u3/log" -type d -depth -mtime +1 -exec rmdir {} \;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top