How to clean 3 days before files, from a directory?

I have a Solaris System. I am using bash shell.
I want to prepare a script which can do the below.
There are few directories i need to clean.
In those directories, I need to delete files which are older than 3 days. 3 days before files need to be deleted.
The directories are as follows.
CC1,CC2,Int.
The file format inside are(there are many files in the CC1 and CC2 directories)
tmp/test/CC1(dir)-A20140107.2355-0000_jak_file(there are files for every 5 min interval in this format)
A20140107.2355-0000_jak_test(there are files for every 5 min interval in this format)

/tmp/test/CC2(dir)-A20140107.2355-0000_jak_file(there are files for every 5 min interval in this format)
A20140107.2355-0000_jak_test(there are files for every 5 min interval in this format)

/tmp/file/Int(dir)-Int-CC1-20140107.csv
Int-CC2-20140107.csv

I have prepared something like this.. Tell me to what extent this will work??

#! /usr/bin/bash
find /tmp/test/CC1* -mtime +3 -exec rm {} \;
find /tmp/test/CC2* -mtime +3 -exec rm {} \;
find /tmp/file/Int* -mtime +3 -exec rm {} \;

But somehow i am not quite satisfied with this..
Need your expert opinion..

It works!
You'll get errors if there are sub-directories. These can be suppressed by -type f or ! -type d .
Further, Unix find offers + instead of \; for -exec ; if the command supports multiple arguments ( rm does) then + is faster.
Then, rm -f suppresses eventual questions when deleting write-protected files.

find /tmp/test/CC1* -type f -mtime +3 -exec rm -f {} +
find /tmp/test/CC2* -type f -mtime +3 -exec rm -f {} +
find /tmp/file/Int* -type f -mtime +3 -exec rm -f {} +
1 Like

Hi MadeInGermany,

Thanks for your reply and it did help. I need to do a little modification if you can help me here too.

Actually whatever files i will be deleting from the above mentioned directories I need to do a provision of logging.

find /tmp/test/CC1* -type f -mtime +3 -exec rm -f {} + find /tmp/test/CC2* -type f -mtime +3 -exec rm -f {} + find /tmp/file/Int* -type f -mtime +3 -exec rm -f {} +

How do i achieve the below.?

  1. Which dates files have been removed and when?
    Simply, provision of logging.

First, note that putting these three commands on one line with no command separator will NOT do what you want.
Second, you don't need to invoke find three times to do this (unless you run into ARG_MAX limitations).
If the rm utility on your system supports the -v option, try:

(date +"Following files removed at %c:";find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -fv {} + ) > rm.log

otherwise, try something like:

date + "Following files removed at %c:" > rm.log
find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -f {} + -exec printf "%s\n" {} + >> rm.log
1 Like

Or simply -print

{
date +"Following files removed at %c:"
find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -f {} + -print
} > rm.log
2 Likes

Yes, obviously. It must be past my bed time...

1 Like

Thnaks a lot Don Cragun and MadeInGermany.
Will utilize this and update.

In case it's germaine, be aware that rm.log logs attempted deletions, not just successful deletions.

Regards,
Alister