Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting


Tags
backup, gzip, log, script, unix

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-27-2013   -   Original Discussion by Mallikgm
Mallikgm's Unix or Linux Image
Mallikgm Mallikgm is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 4 October 2013, 8:56 AM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old Unix and Linux 09-27-2013   -   Original Discussion by Mallikgm
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 12 December 2017, 2:37 AM EST
Location: Simplicity
Posts: 3,862
Thanks: 320
Thanked 1,289 Times in 1,167 Posts

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?
Sponsored Links
    #3  
Old Unix and Linux 09-30-2013   -   Original Discussion by Mallikgm
Mallikgm's Unix or Linux Image
Mallikgm Mallikgm is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 4 October 2013, 8:56 AM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
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 Unix and Linux 10-02-2013   -   Original Discussion by Mallikgm
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 12 December 2017, 2:37 AM EST
Location: Simplicity
Posts: 3,862
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
I guess you used a Windoze text editor that saved the file in Windoze format.
Sponsored Links
    #5  
Old Unix and Linux 10-03-2013   -   Original Discussion by Mallikgm
Mallikgm's Unix or Linux Image
Mallikgm Mallikgm is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 4 October 2013, 8:56 AM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
Need script to tar files older than 30 days jamie_collins Shell Programming and Scripting 2 11-25-2009 03:39 PM
How to tar, compress and remove files older than two days edr Shell Programming and Scripting 4 04-22-2009 06:52 AM
Find files older than x days and create a consolidated single tar file. Dawn Bosch Solaris 3 09-09-2008 06:45 AM
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 05:56 AM.