gzip files older than 1 months

Hello,
I want to gzip files in a folder in bulk i.e. all files older than 1 month should be in *.gz format. Could you please suggest how this can be achieved. Thanks.
Regards,
Alok

It depends if you want to make one archive of them all, of leave them as individual files. If it is the latter, then the easiest will be with a find command, e.g.

find $dir -type f ! -name "*.Z" -mtime +31 -print | xargs compress

I know that this will create plain unix compressed files, but you get the idea.

Beware that this will follow symbolic links to other filesystems and will trawl through all directories /filesystems under $dir. The -type f and ! -name "*.Z" will ensure that you only get files that are not already compressed.

If you want a single archive of the files, there are a number of options depending on operating system. AIX for one allows you to provide an input list to tar, so:-

find $dir -type f ! -name "*.Z" -mtime +31 -print >/tmp/target_files
tar -cvL /tmp/target_files - |compress> $my_archive

Some others do not, so you have to be a little inventive, perhaps creating a temporary sub-directory, moving the files there and then tar/compress the whole thing in one go, e.g.

mkdir mytempdir
find $dir -type f ! -name "*.Z" -mtime +31 -exec mv {} mytempdir \;
cd mytempdir
tar -cvf - . | compress >> ../$my_archive
cd ..
rm -r mytempdir

Beware that this is riskier though. Ensure you have good error checking. If you run out of space during the tar/compress, the above will probably still delete the mytempdir and all the contents which you have just failed to secure.

Make sure you are happy with your backups to external media before you put something like this in place.

I hope that this helps.

Robin
Liverpool/Blackburn
UK