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.
find /tmp/log/ -mtime +180 | xargs tar -czvPf /tmp/older_log_$(date +%F).tar.gz
Thanking you in advance.
Gzip is the utility provided by Operating system linux, unix for gzip the files and reduce the size of the files with compression method or algorithms. You can use find command with combination of gzip command to compressed the files older than 1o days by providing parameter mtime with find command.
The easiest way to zip a folder on Linux is to use the “zip” command with the “-r” option and specify the file of your archive as well as the folders to be added to your zip file. You can also specify multiple folders if you want to have multiple directories compressed in your zip file.
The general form of the command for creating tar.gz files is as follows: tar -czf archive-name.tar.gz file-name... Here’s what the command options mean: -c - instructs tar to create a new archive. -z - sets the compression method to gzip. -f archive-name.tar.gz - specifies the archive name.
The archive is piped to gzip, which compress and write the archive to the disk. Create a tar.gz file from all “.jpg” files: The wildcard character ( *) means all files that end with “.jpg” extension. Create a tar.gz file, transfer it over ssh and extract it on the remote machine: tar.gz file is a Tar archive compressed with Gzip.
While both .tar and .tar.gz refer to file archives, a .tar.gz file is a .tar file that’s been compressed or “zipped” using the gzip utility. Using gzip for compression is what gives the file a “.gz” double extension. Though gzip is the most common compression utility, it’s not the only one.
Using gzip for compression is what gives the file a “.gz” double extension. Though gzip is the most common compression utility, it’s not the only one. As you might imagine, using a different compression utility on a .tar file will result in a different double extension.
Best way is
find . -mtime +3 -print -exec gzip {} \;
Where +3 means zip all files which is older than 3 days.
Thanks a lot for your reply. I got it.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With