File maintenance programs/scripts ala logrotate

Hi all,

Before I start writing my own file maintenance script, maybe one such program/scripts already exist somewhere :slight_smile:

Am looking for a file maintenance script/application that is configurable that I can use to process files under certain criteria, for example, removing files that are x-number of days old, gzip'ping files if they are core dumped files, removing files if they are zero-sized files etc.

Am not sure logrotate is the solution that am looking for.

Any response or suggestion will be much appreciated. Thanks in advance.

Hi newbie_01,

I don't know about such custom applications, so, a custom script could be adapted to do the job.

As you can see below, for this tasks "find" command would be your friend, and combining it with desired
criterias and some other commands, you'll satisfy your requirements.

You've asked for:

1-) removing files that are x-number of days old,

# 1-) Looking for txt files older than 365 days and printing them within folder and subfolder
 find . -name "*.txt" -mtime +365

# 2-) Printing txt files modified less than 365 days ago within folder and subfolders
 find . -name "*.txt" -mtime -365

# 3-) Printing txt files modified 365 days ago within folder and subfolders
 find . -name "*.txt" -mtime 365

# 4-) Removing txt files older than 365 days within folder and subfolders
*(if you want to remove files is advisable to be sure about files listed/found by "find...")
 find . -name "*.txt" -mtime +365 -exec /bin/rm -f '{}' +

About -mtime: (-mtime n)
(+) means greater than, (-) means less than, and without any symbol means exactly equal to.

2-) Zipping files "if they are core dumped files"
*(Below examples how to zip files, but which is the characteristic of a core dumped file?)

#1-) Looking all txt files within folders and its subfolders and compress them in a unique zipped file (Compressed_Files.zip)
 find . -name "*.txt" -print | zip Compressed_Files -@

# 2--)Compressing each txt file within folder and subfolders file, adds to original name ".gz" (filename.txt to filename.txt.gz) 
# and deletes original file
 find . -name "*.txt" -exec gzip -f '{}' +

# 3-)Unzipping each txt files within folder and its subfolders, deleting original gz file.
 find . -name "*.txt.gz" -exec gunzip -f '{}' +

3-) Removing files if they are zero-sized files:

 # 1-) Removing zero size files within folder and its subfolders.
1-) find . -type f -size 0 -exec /bin/rm -f '{}' +
or
2-) find . -type f -empty -exec /bin/rm -f '{}' +

Hope it helps,

Regards.

Hi cgkmal

Thanks for your advise.

Characteristic of a core dump? For my application, core dumps are in the form of directories named core_nnnnn, so I assume I should do find -type d -exec zip -r {} ???

BTW, I've had some instance where I use find and xargs and if nothing matches the criteria, it reverts to the root directory? Have you had that problem before? Although, looking at your example, yuo are not using xargs so I don't it will be problem.

Thanks again

Hi newbie_01,

I've tried with find . -type d -name "core_*" -exec zip -r '{}' + , finds correctly all core_xxxx folders, but it zips all within a unique zip file. And doesn't work fine if the folders have spaces in their name.

Instead I did this:

With tar:
Core_Folders=$(find . -type d -name "core_*")

for each in $Core_Folders
do
tar -zcvf $(basename $each).tar.gz $each
done

 # -It looks for every folder named with pattern core_xxxx within main folder.
# -Only works fine if the folders don't have spaces in the name.
# - All core_xxxx.tar.gz will be stored in main folder. 

Or with zip:
Core_Folders=$(find . -type d -name "core_*")

for each in $Core_Folders
do
zip -r $(basename $each) $each
done
# -It looks for every folder named with pattern core_xxxx within main folder.
  # -Only works fine if the folders don't have spaces in the name.
  # - All core_xxxx.zip will be stored in main folder. 

I haven't faced that xargs reverts me to root folder so far, and if that happens not using xargs(as in examples I've posted) we will be avoiding that issue :slight_smile:

Hope it helps

Regards

Xargs is great if you end up finding heaps of files to process, but be carefull, particularly for files with spaces in their name.

I'd recommend using -print0 and xargs -0 together, also use -r to stop rm (or other command you want to use) being run when nothing is found. If your OS dosn't support these options with xargs, you are best of avoid using it all together.

1 Like