Monday, April 27, 2015

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

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

Problem: In the production servers, disk space is the problem when the log files creating every day for log entries. To overcome this we can backup the log files as a zip or tar or gz files in our unix server.

Solutions:

1) To make a one zip or gz file for files (in the current directory and sub-directory) older than particular period (example: days). 

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

Above command looks for files older than 180 days in the current directory and sub-directory and creates gz file with name "".

 

2) To make individual zip or gz file for each files older than particular period (example: days)


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


Above command useful for create gz files for each individual files older than particular days. First it finds all files older than particular days in given directory (and sub directory also). And iterates to indivial file and creates the gz file for it.



Will be adding more details to this topic...

No comments:

Post a Comment