Unzip the package in linuxtar to a specified folder

Source: Internet
Author: User
When writing shell scripts, you often need to decompress them to the specified folder. the tar command is the most common reference. pay attention to the usage of-C. Run the tar command to decompress the file to the specified directory: tar-zxvf/home/zjx/aa.tar.gz-C/home/zjx/pftar [-cxtzjvfpPN...

 

When writing shell scripts, you often need to decompress them to the specified folder. the tar command is the most commonly used

 

For more information, see-C usage.

 

Tar command

 

 

Decompress the file to the specified directory: tar-zxvf/home/zjx/aa.tar.gz-C/home/zjx/pf

 

Tar [-cxtzjvfpPN] file and directory ....

Parameters:

-C: create a parameter command for the compressed file );

-X: Unlock the parameter command of a compressed file!

-T: view the files in the tarfile!

Note that c/x/t can only exist under the parameter! Cannot exist at the same time!

Because it is impossible to simultaneously compress and decompress.

-Z: does it have the gzip attribute at the same time? That is, do I need to use gzip for compression?

-J: does it have bzip2 attributes at the same time? That is, do I need to use bzip2 for compression?

-V: The file is displayed during compression! This is common, but it is not recommended to use it in the background execution process!

-F: use the file name. please note that the file name should be followed immediately after f! Do not add parameters!

For example, using "tar-zcvfP tfile sfile" is an incorrect method.

"Tar-zcvPf tfile sfile" is correct!

-P: use the original attributes of the original file (the attributes will not be changed based on the user)

-P: absolute paths can be used for compression!

-N: a new date (yyyy/mm/dd) will be packed into the new file!

-- Exclude FILE: Do not pack the FILE during compression!

Example:

Example 1: package all the files in the/etc directory into/tmp/etc.tar

[Root @ linux ~] # Tar-cvf/tmp/etc.tar/etc <= package only, do not compress!

[Root @ linux ~] # Tar-zcvf/tmp/etc.tar.gz/etc <= compressed with gzip

[Root @ linux ~] # Tar-jcvf/tmp/etc.tar.bz2/etc <= compressed with bzip2

# Note that when the file name behind parameter f is retrieved, we use .tar for identification.

# If z compression is added, .tar.gz or. tgz is used to represent the tar file ~ compressed by gzip ~

# If you add a j file, use .tar.bz2 as the file name ~

# A warning message is displayed when the preceding command is executed:

# "Tar: Removing leading '/' from member names" is a special setting for absolute paths.

 

Example 2: Check the files in the above/tmp/etc.tar.gz file?

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

# When we use gzip to compress the files in the tar file,

# Add the z parameter! This is important!

 

Example 3: decompress 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 uncompress files anywhere! In this example,

# First, I will transform the working directory to the/usr/local/src directory, and unlock/tmp/etc.tar.gz,

# The unlocked directory will be in/usr/local/src/etc! In addition, if you enter/usr/local/src/etc

# The File attributes in this directory may be different from those in/etc!

 

Example 4: Under/tmp, I only want to unbind the 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 name in the tarfile. if you only need one file,

# You can issue it in this way! Notice! The root directory in etc.tar.gz/is removed!

 

Example 5: Back up all the files in/etc/and save their permissions!

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

# This-p attribute is very important, especially when you want to keep the attributes of the original file!

 

Example 6: only new files in/home are backed up.

[Root @ linux ~] # Tar-N '2014/1/01'-zcvf home.tar.gz/home

 

Example 7: I want to back up/home,/etc, but not/home/dmtsai

[Root @ linux ~] # Tar -- exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc

 

Example 8: Package/etc/and unpack it under/tmp without generating a file!

[Root @ linux ~] # Cd/tmp

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

# This action is a bit like cp-r/etc/tmp ~ It is still useful!

# Note that the output file is changed to-and the input file is changed to-, and there is another file | yes ~

# This represents standard output, standard input, and pipeline commands respectively!

# In Bash shell, we will mention this command again to explain it ??? /P>

 

 

Gzip and zcat commands

[Root @ linux ~] # Gzip [-cdt #] file name

[Root @ linux ~] # Zcat file name .gz

Parameters:

-C: outputs compressed data to the screen and can be processed through data stream redirection;

-D: extract parameters;

-T: It can be used to check the consistency of a compressed file ~ Check whether the file has any errors;

-#: Compression level.-1 is the fastest, but the compression ratio is the worst.-9 is the slowest, but the compression ratio is the best! Default value:-6 ~

Example:

Example 1: Set/etc/man. config? To/tmp and compressed with gzip

[Root @ linux ~] # Cd/tmp

[Root @ linux tmp] # cp/etc/man. config.

[Root @ linux tmp] # gzip man. config

# At this time, man. config will become man.config.gz!

 

Example 2: read the content of Example 1!

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

# The man.config.gz extracted file content will be displayed on the ghost screen !!

 

Example 3: decompress the file in Example 1

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

 

Example 4: compress man. config unlocked in example 3 with the optimal 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] file name

[Root @ linux ~] # Bzcat file name .bz2

Parameters:

-C: output the data generated during the compression process to the screen!

-D: extracted parameters

-Z: compression parameters

-#: Same as gzip, it is used to calculate the compression ratio.-9 is the best, and-1 is the fastest!

Example:

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

[Root @ linux tmp] # bzip2-z man. config

# At this time, man. config will become man.config.bz2!

 

Example 2: read the content of Example 1!

[Root @ linux tmp] # bzcat man.config.bz2

# The man.config.bz2 extracted file content will be displayed on the ghost screen !!

 

Example 3: decompress the file in Example 1

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

 

Example 4: compress man. config unlocked in example 3 with the optimal 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: extract parameters.

-R: it can be compressed together with files in the directory!

-C: output compressed data to standard output (output to the screen)

Example:

Example 1: Set/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 -- 1 root 2605 Jul 27 :43 man. config. Z

 

Example 2: Unlock the compressed file

[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 -- 1 root 4506 Jul 27 :43 man. config

-Rw-r -- 1 root 2605 Jul 27 :46 man. config. back. Z

# This-c parameter is interesting! He will output the data from the compression process to the screen, instead of writing the data

# File. Z file. Therefore, we can output data to another file name through data stream redirection.

# Concerning data stream redirection, we will discuss it in detail in bash shell!

 

Dd command

[Root @ linux ~] # Dd if = "input_file" of = "outptu_file" bs = "block_size "\

Count = "number"

Parameters:

If: input file ??? Department tip Yun kiss Dad mian fu? /P>

Of: output file ~ It can also be a device;

Bs: the size of a planned block. If no value is set, the default value is 512 bytes.

Count: The number of bs.

Example:

 

Example 1: back up/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 -- 1 root 1746 Aug 25 14:16/etc/passwd

-Rw-r -- 1 root 1746 Aug 29 16:57/tmp/passwd. back

# Take a closer look. The size of my/etc/passwd file is 1746 bytes, because I have not set bs,

# Therefore, the default value is 512 bytes. Therefore, the above 3 + 1 indicates three complete

#512 bytes, and another block under 512 bytes!

# In fact, it seems like the cp 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

# What should I do? How can this problem be solved ??? Yi invasion? Le? Pang Caiyu? BR is 512 bytes,

# It is the first sector on the hard disk. Therefore, I can use this method

# Record all the data in the MBR. it's really amazing! Pai_^

 

Example 3: Back up the entire/dev/hda1 partition.

[Root @ linux ~] # Dd if =/dev/hda1 of =/some/path/filenaem

# This command is amazing! Back up all the content of the entire partition ~

# The following of must not be in the/dev/hda1 directory ~ Otherwise, you cannot finish reading ~

# This action is very useful. if you need to complete the content of the entire partition in the next day,

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

# If you want to back up the entire hard disk, it is similar to Norton's ghost software,

# From disk to disk, hey ~ You can use dd ~ Amazing!

 

Cpio command

[Root @ linux ~] # Cpio-covB> [file | device] <= backup

[Root @ linux ~] # Cpio-icduv <[file | device] <= restore

Parameters:

-O: copy the data to a file or device.

-I: copy the data from a file or device to the system.

-T: view the files or device content created by cpio

-C: a new portable format storage method

-V: enables the file name to be displayed on the screen during storage.

-B: increase the preset Blocks size to 5120 bytes. the default value is 512 bytes!

This improves the storage speed of large files (refer to the I-nodes concept)

-D: automatically create a directory! The contents of cpio may not be in the same directory,

In this case, there will be problems in the anti-backup process! If-d is added at this time,

You can automatically create the required directory!

-U: automatically overwrites older files!

Example:

Example 1: Write all information on the system to the tape drive!

[Root @ linux ~] # Find/-print | cpio-covB>/dev/st0

# Generally, the serial number of the tape drive using the SCSI interface is/dev/st0!

 

Example 2: Check what files exist on the tape drive?

[Root @ linux ~] # Cpio-icdeep </dev/st0

[Root @ linux ~] # Cpio-icvt </dev/st0>/tmp/content

# In the first action, the files in the 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 information on the tape ~

[Root @ linux ~] # Cpio-icduv </dev/st0

# Generally, the serial number of the tape drive using the SCSI interface is/dev/st0!

 

Example 4: Back up all the "files" under/etc to/root/etc. cpio!

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

# In this way, you can back up ???? ? Persimmon string? Pio-I </root/etc. cpio

# Capture the information !!!!

 

From the column hakunamatata2008

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.