Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

logging

gzip

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.

like image 216
mgr Avatar asked Sep 27 '13 16:09

mgr


People also ask

How do I zip a 10 day old file in Unix?

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.

How do I zip an old file in Linux?

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.

How do I create a tar gz file in Linux?

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.

How to create a tar archive with gzip?

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.

What is the difference between tar and tar gz?

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.

What is the difference between gzip and tar compression?

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.


2 Answers

Best way is

find . -mtime +3 -print -exec gzip {} \;

Where +3 means zip all files which is older than 3 days.

like image 56
Shiva Avatar answered Sep 20 '22 17:09

Shiva


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
like image 23
mgr Avatar answered Sep 17 '22 17:09

mgr