Wednesday, August 22, 2012

UNIX : File compression commands


Compression commands available in AIX unix.

  1. compress/uncompress
  2. gzip/gunzip
  3. bzip2/bunzip2


      We use tar command to encapsulate file/dirs in one file to be able to use above compression commands

Examples of Tar :

# To create tape archive of all files inside dir DIR123 in DIR123.tar
tar -cvf DIR123.tar DIR123/*

# To display the names of the files in the out.tar disk archive file on the current directory
tar -vtf out.tar

# To expand the compressed tar archive file, fil.tar.z, pass the file to the tar command, and extract all files from the expanded tar archive file
zcat fil.tar.Z | tar -xvf -

# To extract all content of tar file
tar -xvf abc.tar

# To extract file named Niraj.txt from tar
tar -xvf abc.tar Niraj.txt

Examples of compress & Uncompress :

# To compress a file
compress file1

# To uncompress a file
uncompress file1.Z

# To compress files greater than 100mb
find . –size +104857600c | xargs compress

# To compress only files in current directory which are not compressed
for var1 in ` ls -ltr | grep ^- | awk -F" " '{ print $9 }' | grep -v Z$`
do
compress $var1
echo “file $var1 compressed”
done


( gzip & bzip2 are almost identical in flags usage & syntax. They differ in their compression ability. gzip provides greater level of compression compared to bzip2 )

Examples of gzip & gunzip

# Redirect the files contained inside tarred-gzipped file keeping file intact
gzip -cd gzippedfile.tar.gz | tar tvf - >> outputfile

# gzip normal text file
gzip ABC.txt

# gzip the contents inside a directory
tar cvf - filelist* 2>> tar_error_log | gzip -c >> filelist123.tar.gz

# recover gzipped contents
gzip -cd filelist123.tar.gz | tar xvf - >> filelistdir

# gunzip the gzipped file
gunzip ABC.txt.gz
OR
gzip -d ABC.txt.gz

Examples of bzip2 & bunzip2

# To recursively compress all files under documents_folder
bzip2 -r documents_folder

# Redirect the files contained inside tarred-bzipped file keeping file intact
bzip2 -cd bzippedfile.tar.bz2 | tar tvf - >> outputfile

# bzip2 normal text file
bzip2 ABC.txt

# bzip2 the contents inside a directory#
tar cvf - filelist* 2>> tar_error_log | bzip2 -c >> filelist123.tar.bz2

# recover bzipped contents
bzip2 -cd filelist123.tar.bz2 | tar xvf - >> filelistdir

# gunzip the bzipped file
bunzip2 ABC.txt.bz2
OR
bzip2 -d ABC.txt.bz2

No comments:

Post a Comment