Zipping | Compressing Files in Linux

TAR Command


TAR command is commonly used to archive directories and files in one archive file.

Tar a directory:
# tar cvfp  <archive file name>  <directory to be archived>
e.g.
# tar cvfp  /backupdisk/logs.tar  /home/oracle/Logs

c  --> Create a new archive
v --> verbose mode
f  --> use archive file
p --> preserve permissions

Tar & compress:
# tar cvfpZ  /backupdisk/logs.tar  /home/oracle/Logs
Z  --> Compress the tar file using Compress program [most low compression ration]

# tar cvfpz  /backupdisk/logs.tar  /home/oracle/Logs
z  --> Compress the tar file using gzip compressor [good compression ratio in a reasonable time]

# tar cvfpj  /backupdisk/logs.tar  /home/oracle/Logs
j  --> Compress the tar file using bzip2 compressor [Best compression ratio with longest time]

Extract the tar file on the same tar file location:
# cd /backupdisk/
# tar xvfp  logs.tar
--> Extract
--> verbose mode
f   --> file
--> preserve permissions

Extract tar file under different location than the tar file location:
Step under the location you want to extract the tar file in:
# cd /u01
Extract the tar file with providing it's full path:
# tar xvpf  /backupdisk/ORACLE_HOME/ora11g.tar

Extract specific file from the tar file:
e.g. Extract only file log1.txt from inside logs.tar:
# tar  xvf  logs.tar   log1.txt
# tar zxvf  logs.tar.gz log1.txt   
    --> In case the tar file is compressed.

Extract specific files that have extension .log from logs.tar::
# tar -xvf  logs.tar    --wildcards --no-anchored '*.log'
# tar -zxvf logs.tar.gz --wildcards --no-anchored '*.log'


View contents of a Tar file without extracting the tar file:
# tar tvf  logs.tar | less              --> Shows the contents of an archive.
# tar tvf  logs.tar | grep .txt       --> Show only files with .txt extension.
# tar ztvf logs.tar.gz                  --> Show the contents of a compressed tar file.


GZIP Command

gzip command is commonly used to compress files, gzip replaces the original files with the compressed ones at the end of compression operation. It also allow controlling the compression ratio. Compression ratio from 1 [the lowest | the fastest time] to 9 [the highest | the longest time].
The files will be compressed and have .gz extension.

Note: In general using high compression ratio will consume higher CPU resources, this is why when using a low compression ration consumes less CPU resources and finish faster than using high compression ratio that consumes more CPU resources and takes longer time to finish.

Examples:
# gzip myfile
# gzip -1 *.arc 
--> compress all files with .arc extension with the lowest compression ratio in the fastest time.
# gzip -9 *.log  --> compress all files with .log extension with the highest compression ratio in the longest time.

De-Compress  a .gz file:
# gunzip -f  myfile.gz
-f  --> will overwrite the original file having name myfile if it's already exist.


BZIP2 Command

bzip2is similar to gzip command it compress and replace the original file/directory.
bzip2 provides 9 compression levels same like gzip but bzip2 compression ratios provide higher compression than gzip but gzip is much much faster than bzip2.

Compress and replace file with the highest compression ratio:
# bzip2 -c  -9   log1.log  

Compress and keep the original file with the highest compression ratio:
# bzip2 -c  -9   log1.log  >  log1.log.bz2

De-Compress a bzip2 file:
# bzip2 -d  log1.log.bz2


ZIP Command

Similar to tar command, it creates a zip file for one or more files or for directory and keep the original files un-touched.
It provides 9 levels of compression similar to gzip command.

Compress multiple files in one file:
# zip -9  all_logs.zip   log1.log  log2.log  log3.log 
 -9 is the highest compression ratio

Compress directory:
# zip -r   backup.zip   backup

Unzip a file:
# unzip backup.zip

Zip & Encrypt:

Zip group of files with .aud extension in a compressed file and encrypt it with password:
# zip -e  audit.zip  *.aud
Enter password:
Verify password:


Unzip an encrypted compressed file:
# unzip -P <password> audit.zip

Note: Some programs available on the internet can easily reveal the password for protected files by zip command, it's recommended to use gpg utility to encrypt the compressed file.




No comments:

Post a Comment