CentOS tar package extract (extract to the specified folder)

  • 2020-05-17 07:25:10
  • OfStack

This paper introduces the tar command, gzip, zcat command, bzip2, bzcat command, compress command, dd command, cpio command, as follows:

tar command

Unzip the file to the specified directory:


tar -zxvf /home/zjx/aa.tar.gz -C /home/zjx/pf

tar [-cxtzjvfpPN] files and directories...

Parameters:

-c: create a parameter instruction for a compressed file (create);

-x: unzip 1 compressed file parameter command!

-t: check the files in tarfile!

Note in particular that only one of c/x/t can exist in the parameter distribution! Can not exist at the same time!

Because you can't compress and uncompress at the same time.

-z: do you have gzip properties as well? Is it necessary to use gzip compression?

-j: does it also have bzip2 properties? Do I need to use bzip2 compression?

-v: compression process to display files! This is commonly used, but not recommended for background execution!

-f: use file name, please note that after f, the file name should be attached immediately. No more parameters!

For example, "tar-zcvfP tfile sfile" is the wrong way to write it

"tar-zcvPf tfile sfile" is right!

-p: use the original properties of the original file (properties do not change depending on the user)

-P: you can use absolute paths to compress!

-N: newer than the following date (yyyy/mm/dd) to be packed into the new file!

--exclude FILE: do not package FILE in the process of compression!

Example:

Example 1: package the entire /etc directory into /tmp/ etc.tar


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 

Example 2: refer to the above/tmp etc tar. gz file is what?


[root@linux ~]# tar -ztvf /tmp/etc.tar.gz

#  Because we use gzip  Compression, so look up that tar file  When inside the file, 

#  You have to add z  This parameter! This is important! 

Example 3: will/tmp etc tar. gz files 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 zip file anywhere! In this case, 

#  I'm going to change the working directory to /usr/local/src  Underneath, and untie /tmp/etc.tar.gz  . 

#  The unzipped directory will be in /usr/local/src/etc  ! Also, if you enter /usr/local/src/etc

#  You will find that the directory under the file properties and /etc/  It might be different! 

Example 4: under the/tmp, I just want to/tmp/etc tar. Within gz etc/passwd untied


[root@linux ~]# cd /tmp

[root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd

#  I can see through tar -ztvf  To inspect the tarfile  Inside the file name, if only 1 A file, 

#  Can come through this way! Notice! etc.tar.gz  Inside the root directory /  It was taken away! 

Example 5: backup all files in /etc/ and save their permissions!


[root@linux ~]# tar -zxvpf /tmp/etc.tar.gz /etc

#  this -p  Properties are important, especially if you want to keep the properties of the original file! 

Example 6: in /home, new files are backed up before 2005/06/01


[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/ and unpack it directly under /tmp without generating a file!


[root@linux ~]# cd /tmp

[root@linux tmp]# tar -cvf - /etc | tar -xvf -

#  It's kind of like cp -r /etc /tmp  It still has its USES! 

#  The important thing to note is that the output file becomes -  And the input file also becomes -  And how 1 a |  There are ~ 

#  And this represents standard output, standard input  With pipeline command! 

#  This part we're going to do Bash shell  When I mentioned this directive again, I would like to explain to you what I have learned. 

gzip, zcat command


[root@linux ~]# gzip [-cdt#]  File name 

[root@linux ~]# zcat  File name .gz

Parameters:

-c: output the compressed data to the screen, which can be processed by redirecting the data stream;

-d: uncompressed parameters;

-t: can be used to check the 1 ~ error of a file;

-# : compression level, -1 fastest, but worst compression ratio, -9 slowest, but best compression ratio! The default is -6 ~

Example:

Example 1: copy /etc/ man.config to /tmp and compress it with gzip


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
0

Example 2: read the contents of example 1!


[root@linux tmp]# zcat man.config.gz

#  The screen will now display man.config.gz  After decompression of the file content!! 

Example 3: unzip the file from example 1


[root@linux tmp]# gzip -d man.config.gz

Example 4: unzip man.config from example 3 with the best compression ratio and keep the original file


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
3

bzip2, bzcat command


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
4

Parameters:

-c: output data from the compression process to the screen!

-d: unzipped parameters

-z: compressed parameters

-# : same as gzip, both are in the calculation of compression ratio parameters, -9 best, -1 fastest!

Example:

Example 1: compress the previous /tmp/ man.config with bzip2


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
5

Example 2: read the contents of example 1!


[root@linux ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
6

Example 3: unzip the file from example 1


[root@linux tmp]# bzip2 -d man.config.bz2

Example 4: unzip man.config from 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 ~]# tar -cvf /tmp/etc.tar /etc <== Pack only, not compress! 

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packing, to gzip  The compression 

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packing, to bzip2  The compression 

#  Special attention is paid to the parameters f  After the file file name is my own, we are used to .tar  For identification. 

#  If you add z  Parameter, then .tar.gz  or .tgz  To represent the gzip  compressed tar file  ~ 

#  If you add j  Parameter, then .tar.bz2  As a side file name 

#  When the above instruction is executed, it will be displayed 1 Warning messages: 

#  " tar: Removing leading `/' from member names "That's a special setting for absolute paths. 
9

Parameters:

-d: parameter used to extract

-r: can be compressed along with the files in the directory!

-c: output compressed data as standard output (output to screen)

Example:

Example 1: add /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 zip 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  The parameters are more interesting! Instead of writing to the screen, he outputs the compressed data 

# file.Z  File. As a result, we can reorient the data by redirecting the data stream 1 A file name. 

#  For data flow redirection, we'll be looking at bash shell  We talked about it in detail. 

dd command


[root@linux ~]# dd if="input_file" of="outptu_file" bs="block_size" \

count="number"

Parameters:

if: it means "input file". It can also be a device.

of: it means output file. It can also be a device.

bs: the planned size of one block, if not set, is 512 bytes by default

count: 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.back

#  To look carefully 1 Next, I /etc/passwd  File size is 1746 bytes Because I didn't set it bs  . 

#  So the default is 512 bytes  for 1 One unit. So, up here 3+1  Said a 3  A complete 

# 512 bytes , and less than 512 bytes  On the other 1 a block  "Meaning! 

#  In fact, it feels like it is cp  This command 

Example 2: backup /dev/hda MBR


[root@linux ~]# dd if=/dev/hda of=/tmp/mbr.back bs=512 count=1

1+0 records in

1+0 records out

#  This needs to be solved 1 Next, we know the whole hard disk MBR  for 512 bytes . 

#  It's on the hard drive 1 a sector  Ok, so, I can use this method 

# MBR  All the information in the record down, really great! 

Example 3: back up the whole /dev/hda1 partition.


[root@linux ~]# dd if=/dev/hda1 of=/some/path/filenaem

#  That's a great command! The entire partition  The contents of all backup down ~ 

#  Followed by the of  Must not be /dev/hda1  In the directory ah ~ otherwise, how to read also can not finish reading ~ 

#  This action is very effective if you have to complete the whole thing another day partition  And fill it back in, 

#  You can use dd if=/some/file of=/dev/hda1  To write data to the hard disk. 

#  If you want to backup your entire hard drive, it's similar Norton  the ghost  software 1 A, 

#  by disk  to disk  , hehe hehe ~ use dd  You can ~ awesome! 

cpio command


[root@linux ~]# cpio -covB > [file|device] <== The backup 

[root@linux ~]# cpio -icduv < [file|device] <== reduction 

Parameters:

-o: output data copy to a file or device

-i: pull data from a file or device copy out of the system

-t: view the contents of files or devices created by cpio

-c: 1 newer portable format storage

-v: enables the name of the file to be displayed on the screen during storage

-B: allows the preset Blocks to be increased to 5120 bytes, the preset is 512 bytes!

This has the advantage of making large files faster to store (see i-nodes concept)

-d: automatically create directories! Since the contents of cpio may not be in the same directory,

In this case, there will be problems in the reverse backup process! At this point plus -d,

You can automatically set up the directory you need!

-u: automatically overwrites older files with newer ones!

Example:

Example 1: write all the data on the system to the tape drive!


[root@linux ~]# find / -print | cpio -covB > /dev/st0

# 1 Generally speaking, use SCSI  Interfacing tape drive, code name is /dev/st0  B: oh! 

Example 2: check what files are on the tape drive?


[root@linux ~]# cpio -icdvt < /dev/st0

[root@linux ~]# cpio -icdvt < /dev/st0 > /tmp/content

#  The first 1 , will put the tape machine file to the screen, and we can through the first 2 An action, 

#  Record all file names to /tmp/content  Documents to go! 

Example 3: restore the data on the tape


[root@linux ~]# cpio -icduv < /dev/st0

# 1 Generally speaking, use SCSI  Interfacing tape drive, code name is /dev/st0  B: oh! 

Example 4: backup all "files" under /etc to /root/ etc.cpio!


[root@linux ~]# find /etc -type f | cpio -o > /root/etc.cpio

#  This will enable you to backup the data cpio -i < /root/etc.cpio

#  Come and get the data!! 

Related articles: