Linux tar (package. Compress. Unzip) Command Description | How does tar extract files to the specified directory?

Source: Internet
Author: User
Tags bz2 tar extract



Packaging Example: package files in the /usr/local/src/zlib-1.2.5 directory into zlib-1.2.5.tar.gz


cd /usr/local/src
tar -czvf ./zlib-1.2.5.tar.gz ./zlib-1.2.5/*
#Compress to the specified directory (for example, / root / 2 /)
tar -czvf /root/2/zlib-1.2.5.tar.gz ./zlib-1.2.5/*







Compression
TAR-CZVF ***.tar.gz
TAR-CJVF ***.tar.bz2
Decompression
TAR-XZVF ***.tar.gz
TAR-XJVF ***.tar.bz2
#Note:. Tar.gz use more!


Brief introduction



Parameters:



-C: Create a compressed file parameter directive (the meaning of Create);



-x: Unlock a compressed file parameter command!



-T: View the files inside the Tarfile!



In particular, in the release of the parameters, c/x/t can only exist one! Cannot exist at the same time!



Because it is not possible to compress and decompress simultaneously.



-Z: Do you have the properties of gzip at the same time? i.e. do I need gzip compression?



-j: Do you have bzip2 properties at the same time? i.e. is it necessary to compress with bzip2?



-V: Files are displayed during compression! This is commonly used, but is not recommended for use in the background execution process!



-F: Use the file name, please note, after F to immediately answer the file name Oh! Don't add any more arguments!



For example, using "TAR-ZCVFP tfile sfile" is the wrong way to write



"TAR-ZCVPF tfile sfile" Just right!



-P: The original attribute of the original file is used (the attribute will not change according to the user)



-P: You can use absolute path to compress!



-N: Newer than next date (YYYY/MM/DD) will be packaged in the new file!



--exclude file: In the process of compression, do not package file!



Example:



Example one: Package all files in the/etc directory into/tmp/etc.tar


[[email protected] ~]# tar-cvf/TMP /etc.tar /etc <== package only, not compressed!
[[email protected] ~]# tar-czvf/TMP /etc.tar.gz /etc <==
[[email protected] ~]# tar-cjvf/TMP /etc.tar.bz2 /etc <== after packaging, compress with bzip2 


# Note that the file name after parameter f is taken by yourself, and we are accustomed to using. Tar as an identification.



# if the z parameter is added, the. tar.gz or. tgz represent the gzip compressed tar file ~



# If you add the J parameter, use. tar.bz2 as the file name.



# When the above instruction is executed, a warning message is displayed:



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



Example two: Access to the above/tmp/etc.tar.gz files in the file?


[Email protected] ~]# tar-tzvf/tmp/etc.tar.gz


# because we use gzip compression, when we look at the files in the tar file,



# you have to add Z to this parameter! It's important!



Example three: Extracting/tmp/etc.tar.gz files under/USR/LOCAL/SRC


[Email protected] ~]# cd/usr/local/src[[email protected] src]# tar-xzvf/tmp/etc.tar.gz


# in the case of presets, we can unzip the file anywhere! In the case of this example,



# I'm going to transform my working directory under/USR/LOCAL/SRC and untie/tmp/etc.tar.gz,



# then the unpacked catalogue will be in/USR/LOCAL/SRC/ETC! Also, if you enter/USR/LOCAL/SRC/ETC



# you'll find that the file attributes in this directory may be different from the/etc/.



Example four: under/tmp, I just want to untie the etc/passwd inside the/tmp/etc.tar.gz.


[Email protected] ~]# cd/tmp[[email protected] tmp]# tar-xzvf/tmp/etc.tar.gz etc/passwd


# I can check the file name in the Tarfile through TAR-TZVF, if only a single file,



# can be released in this way! Notice that! The root directory within the etc.tar.gz/is taken away!



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


[Email protected] ~]# tar-czvpf/tmp/etc.tar.gz/etc


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



Example six: In/home, more than 2005/06/01 new files are backed up


[Email protected] ~]# tar-n ' 2005/06/01 '-CZVF home.tar.gz/home


Example seven: I want to back up/home, etc, but don't/home/dmtsai


[Email protected] ~]# tar--exclude/home/dmtsai-czvf myfile.tar.gz/home/*/etc


Example eight: The/etc/is packaged and unpacked directly under/TMP without generating a file!


[Email protected] ~]# cd/tmp[[email protected] tmp]# TAR-CVF-etc | TAR-XVF-


# This action is a bit like cp-r/etc/tmp ~ still has its use!



The thing to note is that the output becomes-and the input file becomes-and another | exist ~



# This represents the standard output, the standard input and the pipeline command!



# This part we'll be talking about this command again in Bash Shell and explain it to everyone!








How does tar extract files to the specified directory?


TAR-CZVF Mysql.tar.gz/var/lib/mysql



Compressed files, I want to extract into the current directory, not the absolute path under, how to unzip?



TAR-CXVF mysql.tar.gz , will cover all the files under/var/lib/mysql? I now have a table broken, want to restore a table of data, rather than restore all the database, so to extract to the specified directory, and then the table data can not be recovered.



============================================



#tar-ZXVF mysql.tar.gz-c/home/aaa



============================================



TAR-ZXVF mysql.tar.gz-c/HOME/AAA



So, what is the structure of the extracted directory?



/home/aaa/var/lib/mysql ?



============================================



Just try and know ....



============================================



All files in the archive are relative references, and the archive is decompressed (released) in the current directory. Archive files are always released to the current directory to prevent the destruction of files with duplicate names in the file system.



Therefore, the landlord's decompression will not overwrite /var/lib/mysql, will only be released in the current directory first, that is ./var/lib/mysql If you want to overwrite, use the- p parameter when archiving and decompression:


TAR-CZVF  mysql.tar.gz  -p/var/lib/mysqltar-xzvfp  mysql.tar.gz


============================================



Linux tar (package. Compress. Unzip) Command Description | How does tar extract files to the specified directory?


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.