Gzip More Than One File and Deleting First Line

Explanation:

gzip, gunzip, zcat - compress or expand files 

'gzip' reduces the size of the named files using Lempel-Ziv coding. Whenever possible each file is replaced by one with extension .gz while keeping the same ownership modes, access and modification times. If no file is specified then standard input is compressed to standard output. gzip will attempt to compress only regular files.

Ex: gzip file

Will compress the file and name it as file.gz

Compressed files can be restored to their original form using gzip -d or gunzip or zcat. 

'gunzip' takes a list of files on command line with extension .Z, .z, .gz and replaces each file uncompressing it without the original extension.

Ex: gunzip file.gz

Will uncompress file.gz and replace it as file removing extension .gz.

'zcat' command will uncompress files with extensions .gz and .Z and write the uncompressed data onto standard output.

Ex: zcat file.Z

Will uncompress file.Z and write its contents to stdout. 
 

How do I gzip more than one file in a single zip file? 

If I provide : gzip file1 file2, it created two gzip files . 

The simplest way to make an archive of more than one file is as follows: 

1) Create a temporary folder 
2) Copy the files into that folder 
3) Make that folder your current directory (i.e. cd temp) 
3) Use the command: tar cvzf archivename.tar.gz ./* 

This will create a zipped and tared archive called archivename.tar.gz of every file in the current directory and in subdirectories under it. 

The file can be extracted into the current directory with: tar zxvf archivename.tar.gz 
 

Deleting the first line of .gz file 

I have too many .gz files (test.gz).

Task is to remove first line of each file.

Can I do it without unzipping the files?

Most important thing is to backup these files before you try doing "mass" updates like these.

Then try something like:

Code:

for files in *.gz
do
  gzip -dc "$files" | tail +2 | gzip -c > "$files".tmp
  if [ "$?" -eq 0 ]; then
# mv or rm - up to you
    mv "$files" "$files".old
    mv "$files".tmp "$files"
  fi
done

Or

Without the command "gzip" but with the command "gzcat"...

for files in *.gz
do
FILE=`echo ${files} | cut -d"." -f1`
gzcat ${files}| tail +2 > ${FILE}
mv ${files} ${files}.old
gzip ${FILE}
done

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.