System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Script for tar & compressing files

SOLVED
Go to solution
mjos
Super Advisor

Script for tar & compressing files

Hi,
need help with tar & compress all files in a folder older than 10 days. Then the script should delete the gz files that are older than 10 days.
9 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Script for tar & compressing files

Hi:

What have you tried? Use 'find' to find files ( -type f ) older than 10-days ( -mtime +10 ).

Collect the filenames output and use this list as a argument to 'tar'. Then 'gzip' the tarball. When done, use 'find' again looking for files named "*.gz" that are older than 10-days ( -name "*.gz* -mtime +10 ) and remove them ( '-exec rm {} +' ).

Regards!

...JRF...
mjos
Super Advisor

Re: Script for tar & compressing files

Thanks, i am not good in scripting.
Does the below script look fine.

cd /tmp
val1=find /tmp -mtime +10
if [$? -eg 0 ]
then
tar cvf $val1 | gzip *.tar
else
echo "No files older than 10 days"
val3=find /tmp -name "*.gz" -mtime +10
echo " List of tar files $val3 "
if [$? -eg 0 ]
then
rm *.gz
fi
fi
Steven Schweda
Honored Contributor

Re: Script for tar & compressing files

> [...] Then 'gzip' the tarball. [...]

tar [...] | gzip

No need for a (temporary) "tarball" file.
James R. Ferguson
Acclaimed Contributor

Re: Script for tar & compressing files

Hi:

OK, you have the general idea. Try this:

#!/usr/bin/sh
cd /tmp || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > /var/tmp/myarch.gz
find . -type f -name "*.gz" -mtime +10 -exec rm {} +
exit 0

...notice that we use a pipe to 'gzip' as Steven suggested to eliminate an intermediate file.

Noitce too, that the proper word in UNIX is "directory", not "folder" :-)

Regards!

...JRF...

mjos
Super Advisor

Re: Script for tar & compressing files

thanks, if the gz file already exists then, it should create a new gz file. How do I do that in the script.
James R. Ferguson
Acclaimed Contributor

Re: Script for tar & compressing files

Hi:

> if the gz file already exists then, it should create a new gz file. How do I do that in the script.

You don't need to do anything. The redirection of the 'gzip' STDOUT to the file specified causes the shell to clobber any file already there.

If you prefer, 'rm' the final output file when the script begins:

#!/usr/bin/sh
set -u
GZARCH=/var/tmp/myarch.gz
rm -f "${GZARCH}"
cd /tmp/dummydir || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > ${GZARCH}
find . -type f -name "*.gz" -mtime +10 -exec rm {} +
exit 0

Regards!

...JRF...
mjos
Super Advisor

Re: Script for tar & compressing files

Thanks James.
I dont want to delete all gz files. I only want to delete only those gz files which are older than 10 days. And when I run the script, if there is a gz file which is not older than 10 days, then the script should check & create another gz file without deleted the old gz file.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script for tar & compressing files

Hi (again):

> I dont want to delete all gz files. I only want to delete only those gz files which are older than 10 days. And when I run the script, if there is a gz file which is not older than 10 days, then the script should check & create another gz file without deleted the old gz file.

Then consider this:

#!/usr/bin/sh
set -u
SEARCH=/tmp
GZDIR=/var/tmp
GZFILE=${GZDIR}/myarch_$(date +%Y%m%d).gz
cd ${SEARCH} || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > ${GZFILE}
find ${GZDIR} -type f -name "*.gz" -mtime +10 -exec rm {} +
rm ${FILES} #...to remove what you archived...
exit 0

Regards!

...JRF...

mjos
Super Advisor

Re: Script for tar & compressing files

Thanks a lot James.
Appreciate your help. I have assaigned points to you.