Home Man
Search
Today's Posts
Register

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?

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 08: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 06: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

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List files older than 10 days. 02Ajay Shell Programming and Scripting 3 01-28-2016 11:49 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 10:02 AM
Files older than 50 days alok.behria UNIX for Dummies Questions & Answers 4 07-25-2011 03:13 PM
deleting files older than 7 days MAYAMAYA0451 Shell Programming and Scripting 1 01-02-2011 11:49 PM
Delete files older than 2 days using shell script in Unix satishpabba Shell Programming and Scripting 3 02-26-2009 05:23 AM
delete files more than 15 days older ali560045 Shell Programming and Scripting 3 11-28-2008 04:43 AM
Find files older than x days and create a consolidated single tar file. Dawn Bosch Solaris 3 09-09-2008 05:45 AM
Removing files older than 7 days texasoeb UNIX for Dummies Questions & Answers 3 04-20-2007 04:04 PM
tar files older than 30 days wfch UNIX for Dummies Questions & Answers 6 04-19-2004 04:40 AM


All times are GMT -4. The time now is 04:57 AM.

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