Linux file directory compression and decompression command

Linux file directory compression and decompression command

Compress compression: compress is a compression program with a long history. After a file is compressed, the extension ". Z" will appear after its name.

[root@localhost ~]# yum install -y ncompress
[root@localhost ~]# compress --help

Command syntax:[ compress [option] File or directory ]

        -f              #Force overwrite target file
        -c              #Send the result to standard output and no file is changed
        -r              #Recursive operation mode
        -b number         #The compression efficiency is between 9 and 16
        -d              #Decompress the file instead of compressing it
        -v              #Display instruction execution process

Compress a file by using the compress -c command

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# compress -c bash > bash.Z

[root@localhost ~]# ls -lh
total 1.6M
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash
-rw-r--r--. 1 root root 596K Nov 16 06:38 bash.Z

Extract a file by using the compress -d command

[root@localhost ~]# ls -lh
total 596K
-rw-r--r--. 1 root root 596K Nov 16 06:38 bash.Z

[root@localhost ~]# compress -d bash.Z

[root@localhost ~]# ls -lh
total 944K
-rw-r--r--. 1 root root 942K Nov 16 06:38 bash

Zip compression: the extended name of zip command compression Zip various systems support zip compression format, so it can be used in general to a certain extent

[root@localhost ~]# yum install -y zip unzip
[root@localhost ~]# zip --help

Command syntax:[ zip/unzip [option] File or directory ]

        -r              #Recursive compression, compressed together with subdirectories
        -S              #Contains system and hidden files
        -v              #Display instruction execution process
        -q              #Instruction execution is not displayed

Compress the entire contents of the / etc / directory by using the zip -r -v command

[root@localhost ~]# zip -r -v lyshark.zip /etc/

[root@localhost ~]# ls -lh
total 12M
-rw-r--r--. 1 root root 12M Nov 16 09:46 lyshark.zip

Use the unzip -l command to query the files in a compressed package

[root@localhost ~]# unzip -l lyshark.zip

Unzip the file to the specified directory using the unzip -d command

[root@localhost ~]# unzip lyshark.zip -d /tmp/

[root@localhost ~]# ls -lh /tmp/
total 12K
drwxr-xr-x. 84 root root 8.0K Nov  6 11:02 etc

Gzip compression: gzip is the default compression format supported by Linux system. It can be used in combination with tar command. Gzip has a compression rate of 60% ~ 70% for text files.

[root@localhost ~]# gzip --help

Command syntax:[ gzip/zcat [option] File or directory ]

        -a              #Use ASCII text mode
        -d              #Unpack the compressed file
        -f              #Force compression of files
        -l              #Lists information about compressed files
        -n              #When compressing, the original file name and timestamp are not saved
        -N              #When compressing, save the original file name and timestamp
        -q              #Do not display warning messages
        -r              #Recursive processing
        -v              #Display instruction execution process

Compress a file by using the gzip -rv command

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# gzip -rv bash
bash:    51.2% -- replaced with bash.gz

[root@localhost ~]# ls -lh
total 460K
-rwxr-xr-x. 1 root root 460K Sep 26  2017 bash.gz

Query a file by using the gzip -l/zcat -l command

[root@localhost ~]# gzip -l bash.gz

         compressed        uncompressed  ratio uncompressed_name
             470300              964544  51.2% bash

[root@localhost ~]# zcat -l bash.gz
         compressed        uncompressed  ratio uncompressed_name
             470300              964544  51.2% bash

Unzip a file by using the gzip -dv / zcat command

[root@localhost ~]# ls -lh
total 460K
-rwxr-xr-x. 1 root root 460K Sep 26  2017 bash.gz

[root@localhost ~]# gzip -dv bash.gz
bash.gz:         51.2% -- replaced with bash

[root@localhost ~]# zcat -d bash.gz >bash_zcat

Bzip2 compression: the bzip2 command is used for creation and management, including decompression. You must install it before using this command, because this command has no integrated installation

[root@localhost ~]# yum install -y bzip2
[root@localhost ~]# bzip2 --hlep

Command syntax:[ bzip2/bzcat [option] File or directory ]

        -c              #Output compression decompression process
        -d              #Perform decompression
        -k              #Keep the original file after decompression
        -v              #Detailed output during compression or decompression
        -z              #Force compression

Compress the bash file by using the bzip2 -kzv command

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# bzip2 -kzv bash
  bash:     2.164:1,  3.697 bits/byte, 53.79% saved, 964544 in, 445742 out.

Extract bash. Net by using the bzip2 -kdv command Bz2 file

[root@localhost ~]# ls -lh
total 436K
-rwxr-xr-x. 1 root root 436K Sep 26  2017 bash.bz2

[root@localhost ~]# bzip2 -kdv bash.bz2
  bash.bz2: done

tar archive and compress: this command is often used in combination with gz,bz2 and other compression commands.

[root@localhost ~]# tar --hlep

Command syntax:[ tar [option] File or directory ]

        -c              #New package
        -t              #View packaged files
        -x              #Unpack the package file
        -j              #Compression and decompression through bz2
        -z              #Compression and decompression through gz
        -J              #Compression and decompression through xz
        -v              #Displays the compression and decompression process
        -C              #Specify where to unzip

Compress, decompress and query by using tar -czvf xzvf tvf command

[root@localhost ~]# tar -czvf lyshark.tar.gz /etc/*        #Compress the contents in / etc / * directory into lysark tar. gz
[root@localhost ~]# tar -tvf lyshark.tar.gz                #Query contents in package
[root@localhost ~]# tar -xzvf lyshark.tar.gz -C /etc/      #Unzip the compressed package to the / etc / directory

Compress, decompress and query by using tar -cjvf xjvf tvf command

[root@localhost ~]# tar -cjvf lyshark.tar.bz2 /etc/*        #Compress the contents in / etc / * directory into lysark tar. bz2
[root@localhost ~]# tar -tvf lyshark.tar.bz2                #Query contents in package
[root@localhost ~]# tar -xjvf lyshark.tar.bz2 -C /etc/      #Unzip the compressed package to the / etc / directory

Compress, decompress and query by using tar -cJvf xJvf tvf command

[root@localhost ~]# tar -cJvf lyshark.tar.xz /etc/*         #Compress the contents in / etc / * directory into lysark tar. xz
[root@localhost ~]# tar -tvf lyshark.tar.xz                 #Query contents in package
[root@localhost ~]# tar -xJvf lyshark.tar.xz -C /etc/       #Unzip the compressed package to the / etc / directory

cpio block level compression: a tool program used by cpio to create or restore backups. The cpio command can back up anything, including device files.

[root@localhost ~]# cpio --help

Compression syntax:[ cpio –cvBo > [file|equipment] ]

        -c              #Using portable format storage
        -v              #Display process
        -B              #Set the I / O block to 512bytes
        -o              #Enter copy out mode (compression)

View syntax:[ cpio –ivct < [cpio file] ]

        -i              #Copy data from file to system
        -c              #Using portable format storage
        -t              #View the contents of CPIO compressed package

Restore syntax:[ cpio –idvcu < [cpio file] ]

        -i              #Enter copy in mode (decompression)
        -d              #Restore to the specified location (depending on compression)
        -v              #Display process
        -c              #Using portable format storage
        -u              #Replace all files without prompting

Use the cpio -cvBo command to back up the / etc / directory

[root@localhost ~]# find /etc/ -print | cpio -cvBo > /root/etc.cpio

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

Use the cpio -cvBo command to back up the contents in the / etc / directory to the / sdb disk

[root@localhost ~]# find /etc/ -print | cpio -cvBo > /dev/vdb

Use the cpio -ivct command to query and compress the package contents

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

[root@localhost ~]# cpio -ivct < etc.cpio

Use cpio -idvcu to restore files to the / etc / directory

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

[root@localhost ~]# cpio -idvcu < /root/etc.cpio

Create ISO image: the mkisofs command is used to make the specified directory and file into an image file in ISO 9660 format.

[root@localhost ~]# mkisofs --help

Command syntax:[ mkisofs [option] File or directory ]

        -o              #Followed by image name
        -r              #Record with file attributes
        -v              #Show build process
        -m              #Exclude specified files
        -V              #New volume

Make an image through mkisofs and make the / etc directory lysark iso.

[root@localhost ~]# mkisofs -r -v -o /root/lyshark.iso /etc/

[root@localhost ~]# ls -lh
total 123M
-rw-r--r--. 1 root root 123M Nov 16 11:14 lyshark.iso

Disk speed measurement and copy: dd command is used for disk speed measurement and backup. This command is very low-level, so it can complete many functions that cannot be completed by backup tools.

[root@localhost ~]# dd --help

Backup syntax:[ dd [if=source file] [of=output] [count=count] ]

        if=file name        #Enter file name
        of=file name        #Output file name
        bs=bytes        #Read in count
        count=blocks    #Copy blocks only
        /dev/zero       #Yes, the device will continue to return 0 value bytes
        /dev/null       #Empty equipment, equivalent to a trash can

By using dd to measure the disk speed, the disk read-write speed can be measured (1024204840968192 can be tested respectively)

[root@localhost ~]# dd if=/dev/zero of=/dev/null bs=1024 count=10000
10000+0 records in
10000+0 records out
10240000 bytes (10 MB) copied, 0.00852247 s, 1.2 GB/s

Regular disk backup by using dd

#Backup the local / dev/sda disk to / dev/hdd
[root@localhost ~]# dd if=/dev/sda /of=/dev/hdd

#Backup the full disk data of / dev/sda to the image file of the specified path
[root@localhost ~]# dd if=/dev/sda of=/tmp/image

#Restore the image backup file to the specified disk / dev/sdb
[root@localhost ~]# dd if=/tmp/image of=/dev/sdb

#Back up the full disk data of / dev/sda, compress it with gzip tool, and save it to / TMP / image gz
[root@localhost ~]# dd if=/dev/sda | gzip > /tmp/image.gz

#Restore the compressed backup file / TMP / image GZ, restore to disk / dev/sdb
[root@localhost ~]# gizp -dc /tmp/image.gz | dd of=/dev/sdb

Use the dd command to back up the disk MBR and restore the partition MBR

#Back up the MBR. Back up the MBR information of 512 bytes from disk / dev/sda to the specified file / tmp/mbr512
#Where: count=1 refers to copying only one block; bs=512 means that the block size is 512 bytes
[root@localhost ~]# dd if=/dev/sda of=/tmp/mbr512 count=1 bs=512
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00015245 s, 3.4 MB/s

#Restore the MBR and write the MBR information of backup / tmp/mbr512 to the beginning of disk / dev/sda
[root@localhost ~]# dd if=/tmp/mbr512 of=/dev/sda
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000201242 s, 2.5 MB/s

Copy the memory data and optical disc data to the specified location by using the dd command.

#Copy memory contents to disk
[root@localhost ~]# dd if=/dev/mem of=/tmp/mem_ Image BS = 1024 (specify a block size of 1k)
dd: error reading '/dev/mem': Bad address
632+0 records in
632+0 records out
647168 bytes (647 kB) copied, 0.001591 s, 407 MB/s

#Copy the contents of the disc to the specified folder and save it as / TMP / lysark ISO file
[root@localhost ~]# dd if=/dev/sr0 of=/tmp/LyShark.iso
1249+0 records in
1248+0 records out
638976 bytes (639 kB) copied, 0.368021 s, 1.7 MB/s

#Fill the disk with random numbers and completely destroy the disk / dev/sdb data
[root@localhost ~]# dd if=/dev/urandom of=/dev/sdb

Added by ericdorman on Sat, 01 Jan 2022 14:05:47 +0200