Archiving and compression of documents
Tar command
Function: package and compress files
[root@localhosttest] # tar-- help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves manyfiles together into a single tape or disk archive, and can
Restore individualfiles from the archive.
Examples:
Tar-cf archive.tar foo bar # Create archive.tar from files foo and bar.
Tar-tvf archive.tar # List all files in archive.tarverbosely.
Tar-xf archive.tar # Extract all files from archive.tar.
Package:
[root@localhost ~] # tarcvf grub2.tar / boot/grub2/
# c create creation
# v details
# f filename
[root@localhost ~] # file a.txt
A.txt: POSIX tararchive (GNU)
[root@localhost ~] # file / etc/passwd
/ etc/passwd: ASCII text
File command
Function: file determines filetype
Usage: file / etc/passwd
Note: the linux system does not recognize file types based on suffix names
Use the file command to see the type of file.
Example: package two directories or target + files into a software package
[root@localhost ~] # tarcvf ss.tar / boot/ / etc/passwd
Instead of unpacking, check the contents of the tar:
Example:
[root@localhost ~] # tartvf ss.tar
Unpack:
[root@localhost ~] # tarxvf grub2.tar
Specify the decompression path:
[root@localhost] # tarxvf grub2.tar-C / opt/
Compare file size
[root@localhost ~] # du-sh / boot/grub2/
8.1M / boot/grub2/
[root@localhost ~] # ll-h grub2.tar
-rw-r--r-- 1 root root7.7M Dec 3 20:41 grub2.tar
Tar Archive + Compression:
Format (suffix): .tar.gz or .tgz
Syntax: tarzcvf newfile.tar.gz SOURCE
[root@localhost ~] # tarzcvf grub2.tar.gz / boot/grub2/
Compare the size
[root@localhost ~] # ll-h grub2.tar*
-rw-r--r-- 1 root root7.7M Dec 3 20:41 grub2.tar
-rw-r--r-- 1 root root3.1M Dec 3 21:02 grub2.tar.gz
Decompress:
[root@localhost] # tarzxvf grub2.tar.gz-C / opt/
Archiving + compression: bz2
Compression:
[root@localhost ~] # tarjcvf grub2.tar.bz2 / boot/grub2/
Format (suffix): .tar.bz2
Syntax: tar jcvf newfile.tar.bz2 SOURCE
Compare the size
[root@localhost ~] # ll-h grub2.tar*
-rw-r--r-- 1 root root7.7M Dec 3 20:41 grub2.tar
-rw-r--r-- 1 root root2.5M Dec 3 21:08 grub2.tar.bz2
-rw-r--r-- 1 root root3.1M Dec 3 21:02 grub2.tar.gz
Decompress: # tar-jxvfnewfile.tar.gz-C / PATH/TO/DISTDIR/
[root@localhost] # tarjxvf grub2.tar.bz2-C / opt/
Zip package decompression command:
Zip is the compression program and unzip is the decompression program.
Compress the file:
[root@localhost ~] # zippasswd.zip / etc/passwd
-r compressed directory
[root@localhost ~] # zip-r grub2.zip / boot/grub2/
Task: can the Linux system decompress the rar format? If possible, how to decompress it?
Decompression
[root@localhost] # unzip grub2.zip-d / opt/
Add:
Compression command: gzip bzip2 xz
Syntax format:
Gzip file
Bzip2 file
Xz file
Decompress:
Gzip-d file
Bzip2-d file
Xz-d file or unxz file
Use zcat, bzcat and xzcat to view the content without decompression.
Features: the file can only be compressed, and the source file disappears after compression (in which the xz command can add the-k parameter to retain the source file)