Operating System - HP-UX
1833758 Members
2175 Online
110063 Solutions
New Discussion

File find/cleanup cron job

 
SOLVED
Go to solution
Jason W. Neiss
Valued Contributor

File find/cleanup cron job

Hello;

I have a directory tree that's used as a file transfer system between projects. It has a tendency to collect old files, so I'd like to set up a cron job that will delete files older than, say, six months. So:

find /repository -type f -ctime +180 -exec rm {} \;

will do the job. However, in each project subdirectory, there resides a file (named sync.cfg) that cannot be deleted, or the transfer automation will break.

I'm looking for a way to test the name of the found file and skip deleting it (ie, if basename $file != "sync.cfg", delete $file). A one-liner seems preferable to a script, though a script would probably be acceptable.

Any ideas?

Thanks;
Jason
5 REPLIES 5
RAC_1
Honored Contributor

Re: File find/cleanup cron job

find /repository -type f -ctime +180 -exec rm {} \; > /tmp/file_list

grep -v "full_path_of_file_not_to_be_deleted" /tmp/file_list|xargs rm

Anil
There is no substitute to HARDWORK
Sridhar Bhaskarla
Honored Contributor
Solution

Re: File find/cleanup cron job

Hi Jason,

A quick thought is to use something like

find /repository \( -type f -a ! -name file2 \) -ctime +180 |xargs rm

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
curt larson_1
Honored Contributor

Re: File find/cleanup cron job

how about something like
find /repository -type f -ctime +180 | grep -v sync.cfg | xargs rm
Helen French
Honored Contributor

Re: File find/cleanup cron job

Try this:

# find /repository -type f -ctime +180 \( ! -name sync.cfg \) -exec rm {} \;
Life is a promise, fulfill it!
Jason W. Neiss
Valued Contributor

Re: File find/cleanup cron job

Thanks for all the responses. After some quick testing, Sri's solution was the most elegant, so you get the full ten points. :-)

Jason