[transfer] linux packaging and compression command summary


tar command
[root@linux ~]# tar [-cxtzjvfpPN] files and directories....
Parameters:
-c: parameter command to create a compressed file (meaning of create);
-x: parameter command to unpack a compressed file !
-t : View the files in the tarfile!
Special attention, in the parameter assignment, only one c/x/t can exist! cannot exist at the same time!
Because it is impossible to compress and decompress at the same time.
-z : Does it also have the gzip attribute? That is, does it need to be compressed with gzip?
-j : Does it also have the properties of bzip2? That is, does it need to be compressed with bzip2?
-v : Display files during compression! This is commonly used, but it is not recommended to be used in the background execution process!
-f : Use the file name, please pay attention to the file name immediately after f! Do not add parameters!
For example, the use of "tar -zcvfP tfile sfile" is a wrong way of writing, and it should be written as
"tar -zcvPf tfile sfile"!
-p : use the original attributes of the original file (attributes will not change depending on the user)
-P : can use absolute path to compress!
-N : Newer than the following date (yyyy/mm/dd) will be packaged into the new file!
--exclude FILE: Do not pack FILE during compression!
Example:
Example 1: Package all files in the entire /etc directory into /tmp/etc.tar
[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== only packaging, not compression!
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==After packaging, compress with gzip
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc < ==After packaging, compress with bzip2
# Special attention, the file name after the parameter f is taken by ourselves, and we are accustomed to use .tar for identification.
# If the z parameter is added, use .tar.gz or .tgz to represent the gzip-compressed tar file ~
# If the j parameter is added, use .tar.bz2 as the file extension~ # When the
above instructions are executed , a warning message will be displayed:
# "tar: Removing leading `/' from member names" is a special setting for absolute paths.
Example 2: Check which files are in the above /tmp/etc.tar.gz file?
[root@linux ~]# tar -ztvf /tmp/etc.tar.gz
# Since we use gzip compression, when you want to view the files in the tar file,
# must add the parameter z! This is important!

Example 3: Extract the /tmp/etc.tar.gz file under /usr/local/src
[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -zxvf /tmp/etc.tar.gz
# By default, we can unpack the compressed file anywhere! In this example,
# I first change the working directory to /usr/local/src, and unpack /tmp/etc.tar.gz,
# The unpacked directory will be in /usr/local/src/etc Woolen cloth! In addition, if you enter /usr/local/src/etc
# you will find that the file attributes in this directory may be different from /etc/!

Example 4: Under /tmp, I just want to unpack etc/passwd in /tmp/etc.tar.gz
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -zxvf / tmp/etc.tar.gz etc/passwd
# I can use tar -ztvf to check the file names in the tarfile. If only one file is needed, # I can send it in
this way! Notice! The root directory / in etc.tar.gz has been removed!

Example 5: Backup all files in /etc/ and save their permissions!
[root@linux ~]# tar -zxvpf /tmp/etc.tar.gz /etc
# The attribute of this -p is very important, especially when you want to keep the attributes of the original file!

Example 6: In /home, files newer than 2005/06/01 are backed up
[root@linux ~]# tar -N '2005/06/01' -zcvf home.tar.gz /home

Example 7: I want to backup /home, /etc, but not /home/dmtsai
[root@linux ~] # tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

Example 8: Unpack /etc/ directly under /tmp without generating files!
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -
# This action is a bit like cp -r /etc /tmp ~ still useful !
# Note that the output file becomes - and the input file also becomes - , and another | exists ~
# This represents standard output, standard input and pipeline commands respectively!
# In this part, we will mention this command again in the Bash shell and explain it to you!




gzip, zcat command
[root@linux ~]# gzip [-cdt#] filename
[root@linux ~]# zcat filename.gz
parameters:
-c: output the compressed data to the screen, through the data stream Redirect to process;
-d : decompression parameters;
-t : can be used to check the consistency of a compressed file ~ see if the file has errors;
-# : Compression level, -1 is the fastest, but has the worst compression ratio, -9 is the slowest, but has the best compression ratio! Default is -6 ~
Example:
Example 1: Copy /etc/man.config to /tmp and compress it with gzip
[root@linux ~]# cd /tmp
[root@linux tmp]# cp /etc/man. config .
[root@linux tmp]# gzip man.config
# Now man.config will become man.config.gz!
Example 2: Read the contents of the file in Example 1!
[root@linux tmp]# zcat man.config.gz
# At this point, the content of the decompressed file man.config.gz will be displayed on the screen! !

Example 3: Unzip the file in Example 1
[root@linux tmp]# gzip -d man.config.gz

Example 4: Compress the man.config extracted in Example 3 with the best compression ratio and keep the original file
[root@linux tmp]# gzip -9 -c man.config > man.config.gz


bzip2, bzcat command
[root@linux ~]# bzip2 [-cdz] filename
[root@linux ~]# bzcat filename. bz2
parameters:
-c: output the data generated by the compression process to the screen!
-d : Decompression parameter
-z : Compression parameter
-# : Same as gzip, it is a parameter for calculating the compression ratio, -9 is the best, -1 is the fastest!
Example:
Example 1: Compress /tmp/man.config with bzip2
[root@linux tmp]# bzip2 -z man.config
# Now man.config will become man.config.bz2!
Example 2: Read the contents of the file in Example 1!
[root@linux tmp]# bzcat man.config.bz2
# At this time, the content of the decompressed file man.config.bz2 will be displayed on the screen! !

Example 3: Unzip the file in Example 1
[root@linux tmp]# bzip2 -d man.config.bz2

Example 4: Compress the man.config extracted in Example 3 with the best compression ratio and keep the original file
[root@linux tmp]# bzip2 -9 -c man.config > man.config.bz2


compress command
[root@linux ~]# compress [-dcr] file or directory
Parameters : -d : Parameters used for
decompression-
r : It can be compressed together with the files in the directory at the same time!
-c : Output compressed data as standard output (output to the screen)
Example:
Example 1: Copy /etc/man.config to /tmp and compress it
[root@linux ~]# cd /tmp
[root@linux tmp]# cp /etc/man.config .
[root@linux tmp]# compress man.config
[root@linux tmp]# ls -l
-rw-r--r-- 1 root root 2605 Jul 27 11:43 man.config.Z
Example 2: Unzip the compressed file just now
[root@ linux tmp]# compress -d man.config.Z

Example 3: Compress man.config into another file for backup
[root@linux tmp]# compress -c man.config > man.config.back.Z
[root@ linux tmp]# ll man.config*
-rw-r--r-- 1 root root 4506 Jul 27 11:43 man.config
-rw-r--r-- 1 root root 2605 Jul 27 11:46 man. config.back.Z
# This -c parameter is more interesting! It will output the data of the compression process to the screen instead of writing it into a
# file.Z file. Therefore, we can export the data to another filename by means of data stream redirection.
# We'll talk more about stream redirection in the bash shell!



dd command
[root@linux ~]# dd if="input_file" of="outptu_file" bs="block_size" \
count="number"
Parameters:
if : it is the input file, it can also be a device!
of : It is the output file, it can also be a device;
bs : The planned size of a block, if it is not set, the default is 512 bytes
count: the meaning of how many bs.
Example:
Example 1: Backup /etc/passwd to /tmp/passwd.back
[root@linux ~]# dd if=/etc/passwd of=/tmp/passwd.back
3+1 records in
3+1 records out
[root@linux ~]# ll /etc/passwd /tmp/passwd.back
-rw-r--r-- 1 root root 1746 Aug 25 14:16 /etc/passwd
-rw-r--r-- 1 root root 1746 Aug 29 16:57 /tmp/passwd.


# 512 bytes, and the meaning of another block less than 512 bytes!
# In fact, it seems to be the cp command~

Example 2: Backup MBR of /dev/hda
[root@linux ~]# dd if=/dev/hda of=/tmp/mbr.back bs=512 count=1
1+0 records in
1+0 records out
# This requires a good understanding~ We know that the MBR of the entire hard disk is 512 bytes,
# is the first sector in the hard disk, so I can use this method to It's really cool to record all the
data in #MBR! ^_^

Example 3: Backup the entire /dev/hda1 partition.
[root@linux ~]# dd if=/dev/hda1 of=/some/path/filenaem
# This command is very powerful! Back up all the contents of the entire partition~
# The following of must be in the /dev/hda1 directory~ Otherwise, you will not be able to read it~
# This action is very useful, if you have to do it another day Completely fill in the content of the entire partition,
# You can use dd if=/some/file of=/dev/hda1 to write the data to the hard disk.
# If you want the entire hard disk backup, like Norton's ghost software,
# From disk to disk, hehe~ Just use dd~ Awesome!



cpio command
[root@linux ~]# cpio -covB > [file|device] <== backup
[root@linux ~]# cpio -icduv < [file|device] <== restore
Parameters :
-o : Copy the data Output to a file or device
-i: copy data from the file or device to the system
-t: view the content of the file or device created by cpio
-c: store in a newer portable format
-v: let the process of storage The file name can be displayed on the screen
-B: The default Blocks can be increased to 5120 bytes, the default is 512 bytes!
The advantage of this is that it can speed up the storage of large files (please refer to the concept of i-nodes)
-d : Automatically create a directory! Since the contents of cpio may not be in the same directory,
there will be problems in the process of anti-backup! At this time, if -d is added,
the required directory can be automatically established!
-u : Automatically overwrite older files with newer files!
Example:
Example 1: Write all the data on the system to the tape drive!
[root@linux ~]# find / -print | cpio -covB > /dev/st0
# Generally speaking, the tape drive using the SCSI interface is codenamed /dev/st0!
Example 2: Check what files are on the tape drive?
[root@linux ~]# cpio -icdvt < /dev/st0
[root@linux ~]# cpio -icdvt < /dev/st0 > /tmp/content
# In the first action, the filename in the tape drive will be Listed on the screen, and we can use the second action,
# Record all file names to the /tmp/content file!

Example 3: Restore the data on the tape ~
[root@linux ~]# cpio -icduv < /dev/st0
# Generally speaking, the tape drive using the SCSI interface is codenamed /dev/st0!

Example 4: Backup all "files" under /etc to /root/etc.cpio!
[root@linux ~]# find /etc -type f | cpio -o > /root/etc.cpio
# This can be backed up ~ You can also use cpio -i < /root/etc.cpio
# to copy the data Data caught! ! ! !

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327016583&siteId=291194637