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

Tags
backup, gzip, log, script, shell scripts, unix

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 09-27-2013
How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also?

I found one command which is to create gz file for the older files, but it creates only one gz file for all older file. But I need individual gz file for each log file.
Code:
find /tmp/log/ -mtime +180 | xargs tar -czvPf /tmp/older_log_$(date +%F).tar.gz

Thanking you in advance.
Regards,
Mallik

Last edited by Franklin52; 09-29-2013 at 09:10 AM.. Reason: Please use code tags
# 2  
Old 09-27-2013
Code:
find /tmp/log/ -type f -size +0 ! -name '*.gz' -mtime +180 |
while read f
do
  cp -p "$f" "$f".0 &&
  gzip "$f".0
done

But maybe your OS provides the more robust rotatelog or logadm?
# 3  
Old 09-30-2013
Thanks a lot for your reply.

I put below lines (marked with blue color) in a script file "only_zip.sh" and trying to execute it, but giving the error
Code:
find /tmp/mallik3/ -type f -size +0 ! -name '*.gz' -mtime +180 |
while read f
do
  cp -p "$f" "$f".0 &&
  gzip "$f".0
done

Above script gives below error:
Code:
: command not found:
only_zip.sh: line 6: syntax error near unexpected token `done'
only_zip.sh: line 6: `done'

____________________
And I put extra commands in the another file "log_script_n.sh
" (marked with blue color) and trying to execute
Code:
#Deletes the files older than 365 days
find /tmp/mallik3/ -mtime +2200 -exec rm -f {} \;
#Make the zip/gz/compress the files, if those are older than 6 months
#another way of doing
find /tmp/mallik3/ -type f -size +0 ! -name '*.gz' -mtime +180 |
while read f
do
  cp -p "$f" "$f".0 &&
  gzip "$f".0
done
#Delete the file older than 6 months (which are alredy zipped).
find /tmp/mallik3/ -mtime +180 -exec rm -f {} \;

gives below error:
Code:
find: missing argument to `-exec'
: command not foundne 9:
log_script_n.sh: line 17: syntax error: unexpected end of file
-bash-3.2$

Could you please help in this regard.

Last edited by Franklin52; 09-30-2013 at 07:00 AM.. Reason: Please use code tags
# 4  
Old 10-02-2013
I guess you used a Windoze text editor that saved the file in Windoze format.
# 5  
Old 10-03-2013
Thanks a lot for your reply.
I got it.


Code:
 
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

Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Listing files older than 30 days Home Shell Programming and Scripting 11 10-26-2017 12:30 AM
Find all log files under all file systems older than 2 days and zip them saurabh.mishra UNIX for Dummies Questions & Answers 4 08-29-2013 05:31 AM
Create Tar and GZip files under Unix giladboker UNIX for Dummies Questions & Answers 1 05-29-2012 06:20 AM
Find and delete files and folders which are n days older from one unix server to another unix server sachinkl Shell Programming and Scripting 2 03-12-2012 11:02 AM
Files older than 50 days alok.behria UNIX for Dummies Questions & Answers 4 07-25-2011 04:13 PM
removing files older than n days big123456 UNIX for Advanced & Expert Users 2 01-21-2011 05:41 AM
Need script to tar files older than 30 days jamie_collins Shell Programming and Scripting 2 11-25-2009 03:39 PM
List the files which are older than 7 days smr_rashmy Shell Programming and Scripting 3 09-16-2009 11:45 PM
To write a shell script which groups files with certain pattern, create a tar and zip rahu_sg Shell Programming and Scripting 3 07-16-2009 03:40 PM
How to find files older than 30 days anushree.a Shell Programming and Scripting 4 07-08-2009 09:43 AM
How to tar, compress and remove files older than two days edr Shell Programming and Scripting 4 04-22-2009 06:52 AM
Delete files older than 2 days using shell script in Unix satishpabba Shell Programming and Scripting 3 02-26-2009 06:23 AM
Find files older than x days and create a consolidated single tar file. Dawn Bosch Solaris 3 09-09-2008 06:45 AM
How to create a Tar of multiple Files in Unix and FTP the tar to Windows. marconi UNIX for Advanced & Expert Users 1 08-25-2008 04:40 PM
tar files older than 30 days wfch UNIX for Dummies Questions & Answers 6 04-19-2004 05:40 AM
All times are GMT -4. The time now is 08:26 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?