Compressing files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compressing files
# 1  
Old 12-19-2012
Compressing files

I need help to do a script that will compress a file that's bigger than 5000 octets and won't overwrite the previous compress file.

lets say I have mylogfile.log and I would compress it I would become
mylogfile. 1. log and if I would compress again mylogfile.log it would be
mylogfile. 2. log.

My script(doesn't work)

Code:
if  [-f myfilelog.log < 5000 ] ; then
  for i in $(/var/log/mrfilelog.log)
do
 zip myfilelog.i.log
fi

Thanks for your answer.

Last edited by joeyg; 12-19-2012 at 03:50 PM.. Reason: Please wrap commands and data with CodeTags
# 2  
Old 12-19-2012
I don't think repeated compression is a good idea. It can lead to file corruption.

I suggest to tar the file first and then apply a bzip2 compression on top of that.
Code:
tar myfilelog.log.tar myfilelog.log
bzip2 myfilelog.log.tar

---------- Post updated at 14:41 ---------- Previous update was at 14:32 ----------

BTW if you want to create a new file name each time you compress, then here is syntax:-
Code:
for i in {1..3}
do
  zip myfilelog.${i}.log myfilelog.log 
done

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-19-2012
what does tar do?
# 4  
Old 12-19-2012
Usually, zip needs a zip archive file to save to. For compressed content, zip just copies, so there is no waste. Generally, compressing twice makes a third file bigger than the second.

You can make a compressed copy of an open file in a new file ( "compress <old >new" or "gzip <old >new" and zip as far as it works with open files ) but it lacks data integrity as the original is still growing. You can break really big files apart, especially logs with time stamps, where it is pretty easy to break them into days, months or years.

Usually, you rotate the log file first, so it is not attached, and then you archive it. Many servers will restart (reopen) the log, so you rename it (so the name is open -- legal while attached, as they are logging on an open fd to an inode unless they are shell scripts, and sometimes even (...)>then) and send then a signal with kill to the logging daemon. Many use 'logrotate' and 'cron' for this.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 12-19-2012
tar is an archiving utility. You can check the manual pages here for further reference.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 12-19-2012
tar is an archiver somewhat like zip, older and not tied to the archive file so closely, not doing updates, deletions and such that zip does.
This User Gave Thanks to DGPickett For This Post:
# 7  
Old 12-19-2012
Thanks guy your helping me a lot i'm new at this, so ya thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Logrotate and Compressing only yesterdays files

Hello, I have a syslog server at home and am currently experiencing an issue where my logs will rotate and compress however it will rotate and compress yesterdays file and the newly created log file for the current day. When it does this however it will also create another new file for today... (9 Replies)
Discussion started by: MyUserName7000
9 Replies

2. Shell Programming and Scripting

Problem in compressing and moving files

Hi I am writing a sample script (sample.ksh) to compress files in the source directory and move them to another directory. The script takes a config file (files.config) as the paramter the contents of which are as given under: /abc/src ${TSTENV}-xxx-yyy~1.log /abc/src/dest /abc/src... (8 Replies)
Discussion started by: swasid
8 Replies

3. Shell Programming and Scripting

Getting latest files and compressing from a textfile

I'm doing a cleanup script for a directory using KSH. I'm keeping the file name prefixes in a text file. In a KSH, I want to read the prefix from the file, and match the pattern of the file and keep and compress(.Z) the latest 4 versions of the matched files in the directory. And I want to delete... (1 Reply)
Discussion started by: manchimahesh
1 Replies

4. Shell Programming and Scripting

What is the code for compressing files using pkzip ?

hi everyone , Can someone provide me a shell program to compress and decompress files using gzip , i dont know anything in shell programming , this code is a part of my project. So can someone help with me the code ? (2 Replies)
Discussion started by: mig23
2 Replies

5. UNIX for Dummies Questions & Answers

Compressing of log files

Hello All My first post in the forum. :) I've this huge log files of size 20GB-30 GB in my unix server. I want to analyse the log file for some error messages. But because of the enormity of the size of these files i'm not able to grep/search the pattern in the file . Also, tried to gzip the... (1 Reply)
Discussion started by: sgbhat
1 Replies

6. UNIX for Advanced & Expert Users

Compressing files on NAS mount

Hello, I am having difficulty compressing the files using compress or GZIP utility on NAS share NFS mounted on my linux server. Any one have idea on how to do this ? I get the followign error but the trying to compress the files STRP2> compress STR_OUTBOUND_CDM_LOG_LOB.PRT2008_26.txt... (1 Reply)
Discussion started by: kamathg
1 Replies

7. Shell Programming and Scripting

tarring/compressing files in Unix directory

hi guys, i'm totally new with Unix sripting and no idea how to do the scripting at all. My problem is that my boss asked me to do this: 1.) create a script that will tar or gzip the files in particular directory eg: i'm on my home directory and I need to tar/gzip the file in.. assuming... (1 Reply)
Discussion started by: montski
1 Replies

8. UNIX for Dummies Questions & Answers

compressing two files in a script

Hi, i have written a script in unix which produces two files(.csv file) at the end. Now i want to add these to files in a zip file and send the zip file across the network by FTP. Problem is that i dunno how to make a single zip file containing the two files that have been created by the script.... (1 Reply)
Discussion started by: nimish
1 Replies

9. UNIX for Dummies Questions & Answers

Compressing files

I have never used this command before. I need to use the "compress" command to compress all files located in the subdirectories under the following directories: /home/ftp/inbound/Fordin Please advise, I appreciate your help. Thanks, Syed (3 Replies)
Discussion started by: sh9492
3 Replies

10. UNIX for Dummies Questions & Answers

Compressing files from DOS to Unix

As the title sugests I need to compress files from DOS to Unix. The files should be in .Z format, as created using 'compress' in an Unix environment. Ligs (2 Replies)
Discussion started by: ligs
2 Replies
Login or Register to Ask a Question