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
- gzip hosts #Compress hosts into hosts.gz without keeping the original file
- 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
- zcat hosts.gz
3. Decompression
- gzip -d hosts.gz #Unzip without specifying a path, unzip it to the current directory, and do not keep the compressed file
- gzip -d -c hosts.gz > xx/hosts #Decompression of the specified path, while retaining the compressed file
(3)bzip
1. Compressed files
- bzip2 hosts.bz2 #Compress hosts into hosts.bz2 without keeping the original file
- bzip2 -c hosts > xx/hosts.bz2 #Compress hosts into hosts.gz, keep the original file, and specify the path <span style= "font-size:18px;">
- </span>
2. View the contents of the compressed file
- bzcat hosts.bz2
3. Decompression
- bzip2 -d hosts.bz2 #Unzip without specifying a path, unzip it to the current directory, and do not keep the compressed file
- 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
- tar cvf aa.tar aa #Keep the original file by default
- tar cvf aa.tar aa --remove-file #Remove the original file after packaging
(2) Packing multiple files
- tar cvf ab.tar aa bb #Keep the original file by default
- tar cvf aa.tar aa bb --remove-file #Remove the original file after packaging
(3) View the contents of the bag
- tar tvf aa.tar
(4) Unpacking
- tar xvf aa.tar #Unpack without specifying a path, unpack to the current directory
- 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
- tar zcvf aa.tar.gz aa #Keep the original file
- tar zcvf aa.tar.gz aa --remove-file #Do not keep the original file
(2) Pack multiple files and compress them
- tar zcvf ab.tar.gz aa bb #Keep the original file
- tar zcvf ab.tar.gz aa bb --remove-file #Do not keep the original file
(3) Decompression
- tar zxvf ab.tar.gz #Unzip and unpack without specifying a path
- 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
- tar jcvf aa.tar.bz2 aa #Keep the original file
- tar jcvf aa.tar.bz2 aa --remove-file #Do not keep the original file
(2) Pack multiple files and compress them
- tar jcvf ab.tar.bz2 aa bb #Keep the original file
- tar jcvf ab.tar.bz2 aa bb --remove-file #Do not keep the original file
(3) Decompression
- tar jxvf ab.tar.bz2 #Unzip and unpack without specifying a path
- 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.