Packing and compressing files or directories under Linux

1. Compression command [compress/gzip/bzip2]:

Compression can only be done on files, not on directories. The directory must be packaged first and then compressed.

 

(1)compress

 

(2)gzip

1. Compressed files

 

[cpp]  view plain copy  
 
  1. gzip hosts #Compress hosts into hosts.gz without keeping the original file  
  2. gzip -c hosts > xx/hosts.gz #Compress hosts into hosts.gz, keep the original file, and specify the path  

 

2. View the contents of the compressed file

 

[cpp]  view plain copy  
 
  1. zcat hosts.gz  

 

3. Decompression

 

[cpp]  view plain copy  
 
  1. gzip -d hosts.gz #Unzip without specifying a path, unzip it to the current directory, and do not keep the compressed file  
  2. gzip -d -c hosts.gz > xx/hosts #Decompression of the specified path, while retaining the compressed file  

 

(3)bzip

1. Compressed files

 

[cpp]  view plain copy  
 
  1. bzip2 hosts.bz2 #Compress hosts into hosts.bz2 without keeping the original file  
  2. bzip2 -c hosts > xx/hosts.bz2 #Compress hosts into hosts.gz, keep the original file, and specify the path <span style= "font-size:18px;">  
  3. </span>  

 

2. View the contents of the compressed file

 

[cpp]  view plain copy  
 
  1. bzcat hosts.bz2  

 

3. Decompression

 

[cpp]  view plain copy  
 
  1. bzip2 -d hosts.bz2 #Unzip without specifying a path, unzip it to the current directory, and do not keep the compressed file  
  2. bzip2 -d -c hosts.bz2 > xx/hosts #Decompress the specified path while retaining the compressed file  
 

Summarize:

 

-c: specify the path, keep the file

-d: decompress

By default, gzip and bzip2 do not retain the original file/compressed file.

 

 

2. Packing/compressing command [tar]

2.1 Packing/Unpacking:

(1) Package a single file

 

[cpp]  view plain copy  
 
  1. tar cvf aa.tar aa #Keep the original file by default  
  2. tar cvf aa.tar aa --remove-file #Remove the original file after packaging  

 

(2) Packing multiple files

[cpp]  view plain copy  
 
  1. tar cvf ab.tar aa bb #Keep the original file by default  
  2. tar cvf aa.tar aa bb --remove-file #Remove the original file after packaging  

 

(3) View the contents of the bag

[cpp]  view plain copy  
 
  1. tar tvf aa.tar  

 

(4) Unpacking

[cpp]  view plain copy  
 
  1. tar xvf aa.tar #Unpack without specifying a path, unpack to the current directory  
  2. tar xvf aa.tar -C xx/aa #Unpack the specified path  

 

 

2.2 Pack and zip/unpack and unpack:

 

in gzip format

(1) Pack a single file and compress it

[cpp]  view plain copy  
 
  1. tar zcvf aa.tar.gz aa #Keep the original file  
  2. tar zcvf aa.tar.gz aa --remove-file #Do not keep the original file  

 

(2) Pack multiple files and compress them

[cpp]  view plain copy  
 
  1. tar zcvf ab.tar.gz aa bb #Keep the original file  
  2. tar zcvf ab.tar.gz aa bb --remove-file #Do not keep the original file  

 

(3) Decompression

[cpp]  view plain copy  
 
  1. tar zxvf ab.tar.gz #Unzip and unpack without specifying a path  
  2. tar zxvf ab.tar.gz -C xx/ab #Specify path to decompress and unpack  

 

in bizp2 format

(1) Pack a single file and compress it

[cpp]  view plain copy  
 
  1. tar jcvf aa.tar.bz2 aa #Keep the original file  
  2. tar jcvf aa.tar.bz2 aa --remove-file #Do not keep the original file  

 

(2) Pack multiple files and compress them

[cpp]  view plain copy  
 
  1. tar jcvf ab.tar.bz2 aa bb #Keep the original file  
  2. tar jcvf ab.tar.bz2 aa bb --remove-file #Do not keep the original file  

(3) Decompression

[cpp]  view plain copy  
 
  1. tar jxvf ab.tar.bz2 #Unzip and unpack without specifying a path  
  2. tar jxvf ab.tar.bz2 -C xx/ab #Specify the path to decompress and unpack  


Summarize:

-C: specify the path

--remove-file: delete the original file

The tar command keeps the original file by default.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692519&siteId=291194637