How to create zip / gz / tar files if files are older than certain days on UNIX or Linux

I need a script file to back up (zip or tar or gz) old log files on our unix server (which causes a space problem). Could you help me create zip or gz files for each log file in the current directory and subdirectories?

I found one command to create a gz file for old files, but it only creates one gz file for all old files. But I need a separate gz file for each log file.

find / tmp / log / -mtime +180 | xargs tar -czvPf / tmp / old_log _ $ (date +% F) .tar.gz

Thank you in advance.

+4
source share
3 answers

. .

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
+2

-

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

+3 zip 3 .

+6

First, the argument -mtimedoes not receive files that are "older" than a certain amount. Rather, it checks the last time the file was modified. File creation date is not saved on most file systems. Often the last modified time is enough, but it does not match the age of the file.

If you just want to create one tar file for each archive, use -exec instead of passing 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' {} \;
+4
source

All Articles