How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux

Best way is

find . -mtime +3 -print -exec gzip {} \;

Where +3 means zip all files which is older than 3 days.


Thanks a lot for your reply. I got it.

files=($(find /tmp/mallik3/ -mtime +"$days"))
for files in ${files[*]}
do
     echo $files
     zip $files-$(date --date="- "$days"days" +%F)_.zip $files
      #       tar cvfz $(files)_$(date --date='-6months' +%F).tar.gz $files
#       rm $files
done

First, the -mtime argument does not get you files that are "older" than a certain amount. Rather, it checks the last time the file was modified. The creation date of files is not kept in most file systems. Often, the last modified time is sufficient, but it is not the same as the age of the file.

If you just want to create a single tar file for each archive, use -exec instead of passing the data to xargs:

find /tmp/log/ -mtime +180 -type f -exec sh -c \
    'tar -czvPf /tmp/older_log_$(basename $0)_$(date +%F).tar.gz $0' {} \;

Tags:

Unix

Logging

Gzip