Linux Decompression and Decompression tar

Source: Internet
Author: User
Tags bz2 gz file syslog

The most commonly used packaging command under Linux is tar, which can be compressed with other commands when packaged with the tar command. How to use the TAR command
Tar command
[[Email protected] ~]# tar [-cxtzjvfppn] files and directories ....
Parameters:
-C: Create a compressed file parameter directive (the meaning of Create);
-x: Unlock the parameter instructions for a compressed file!
-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!
-C (UPPERCASE): This parameter can be used when extracting a directory to extract it in a specific directory.
-P: Use original file properties (attributes are not changed according to user)
-P (Uppercase): 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!

In fact, the simplest way to use tar is to remember:
Compression: TAR-ZCVF filename.tar.gz The name of the file or directory to be compressed
View: TAR-ZTVF filename.tar.gz
Unzip: TAR-ZXVF filename.tar.gz-c the directory to unzip

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

[[email protected] ~]# tar -cvf /tmp/etc.tar  /etc <== package only, do not compress! [[email protected] ~]# tar -zcvf /tmp/etc.tar.gz /etc <== after packing, to  gzip   compression [[email protected] ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== is packaged to  bzip2  compression [[Email protected] ~]# ls -lh /tmptotal 36m-rw-r--r--.  1  root root  22m dec  4 04:38 etc.tar-rw-r--r--.  1 root  root 6.6m dec  4 04:38 etc.tar.bz2-rw-r--r--.  1 root root  7.4m dec  4 04:38 etc.tar.gz 

# Special Note that the file name after the parameter F is taken by itself, and we are accustomed to using. Tar as a recognition.
# If the z parameter is added, the. tar.gz or. tgz to represent the gzip compressed tar file ~
# If you add the J parameter, use. tar.bz2 as the file name.
When backing up important system data, we can use the-p parameter to retain the original Permissions and properties of the file
# when the above instruction is executed, a warning message is displayed:
# "tar:removing leading '/' from member names" that is a special setting for absolute paths.

Tar Example two: When you package a file, it is generally packaged   from the previous directory of the directory or file you want to package;
If you want to package this file and its absolute path, you can package it from/start

[[email protected] ~]# Tar-zcvf/tmp/etc1.tar.gz/tmp/dingjian <== package its file Dingjian with absolute path tar:removing leading '/' from M Ember Names/tmp/dingjian//tmp/dingjian/aa/tmp/dingjian/dd/tmp/dingjian/cc/tmp/dingjian/bb[[email protected] ~]# CD /tmp <== The file is only packaged on this dingjian file [[email protected] tmp]# tar-zcvf/tmp/etc2.tar.gz./dingjian./ding Jian/./dingjian/aa./dingjian/dd./dingjian/cc./dingjian/bb

The paths extracted separately:

[Email protected] ~]# lsanaconda-ks.cfg Dingjian install.log install.log.syslog tar.sh tmp[[email protected] ~]# tar -zxvf/tmp/etc1.tar.gz [[email protected] ~]# Tree Tmptmp└──dingjian├──aa├──bb├──cc└──dd[[email Prot Ected] ~]# tar-zxvf/tmp/etc2.tar.gz 1 directory, 4 files[[email protected] ~]# tree dingjiandingjian├──aa├──bb├──cc└─ ─dd



Tar example three: Check out which files are in each packaged file in the/tmp/directory above?

[[email protected] ~]# tar-tvf/tmp/etc.tar[[email protected] ~]# tar-jtvf/tmp/etc.tar.bz2[[email protected] ~]# tar-z Tvf/tmp/etc.tar.gz


# If you look at bz2 or gz compressed files, you have to add the J or Z parameter! It's important!


Tar Example four: Extract the/tmp/etc.tar.gz file under/USR/LOCAL/SRC

[Email protected]/]# cd/usr/local/src[[email protected] src]# tar-zxvf/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 will find that the file attributes in this directory may be different from the/etc/.

Tar Example five: Extract the/tmp/etc.tar.gz file to the specified directory/root

[[email protected] ~]# tar-zxvf/tmp/etc.tar.gz-c/root[[email protected] ~]# lsanaconda-ks.cfg etc install.log Insta Ll.log.syslog tar.sh


Tar example six: under/tmp, I just want to untie the etc/passwd in/tmp/etc.tar.gz.

[[email protected] tmp]# tar -ztvf etc.tar.gz  |grep passwd-rw-r--r-- root/root       146 2012-02-22  19:48 etc/pam.d/passwd-rw-r--r-- root/root      1176  2013-11-05 05:52 etc/passwd--rw-r--r-- root/root      1176  2013-11-05&NBSP;05:52&NBSP;ETC/PASSWD-RW------- root/root          0 2013-02-22 08:16 etc/security/opasswd[[email protected] tmp]# tar - zxvf /tmp/etc.tar.gz etc/passwdetc/passwd[[email protected] tmp]# ls -l  Etctotal 4-rw-r--r--.  1 root root 1176 nov  5 05:52 passwd 

#我们通过tar-ZTVF etc.tar.gz to filter the location of passwd by combining pipe grep
#在解压过程中etc/passwd cannot be/etc/passwd because it is a file name that is recorded within etc.tar.gz


Tar example seven: Back up all the files in the/etc/and save their permissions!
[[email protected] ~]# tar-zxvpf/tmp/etc.tar.gz/etc
# This property of-p is important, especially if you want to preserve the properties of the original file!

Tar example eight: in/etc, newer files than 2013/11/01 are backed up
[[email protected] tmp]# tar-n ' 2013/11/01 '-ZCVF etc_2013.tar.gz /etc

Tar Example IX: Package backup Dingjian folder under current directory, but do not package inside Linuxfile file

[[email protected] tmp]# tree dingjiandingjian├── 11├── 22├── 33├──  44├── 55├── aa├── bb├── cc├── dd├── ee├── linuxfile├── q1├──  Q2└── q3.45 directories, 9 files[[email protected] tmp]# tar --exclude  ./dingjian/linuxfile -zcvf dingjian.tar.gz ./dingjian./dingjian/./dingjian/33/./dingjian/ aa./dingjian/11/./dingjian/55/./dingjian/dd./dingjian/ee./dingjian/q2./dingjian/cc./dingjian/q1./dingjian/bb./ Dingjian/44/./dingjian/q3.4./dingjian/22/  [[email protected] tmp]# tar -ztvf  dingjian.tar.gzdrwxr-xr-x root/root         0  2013-12-04 09:04 ./dingjian/drwxr-xr-x root/root          0 2013-12-04 09:02 ./dingjian/33/-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/aadrwxr-xr-x root/root          0 2013-12-04 09:02 ./dingjian/11/drwxr-xr-x root/root          0 2013-12-04 09:02 ./dingjian/55/-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/ Dd-rw-r--r-- root/root         0 2013-12-04 09:02  ./dingjian/ee-rw-r--r-- root/root         0  2013-12-04 09:04 ./dingjian/q2-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/cc-rw-r--r-- root/root          0 2013-12-04 09:04 ./dingjian/q1-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/bbdrwxr-xr-x root/root          0 2013-12-04 09:02 ./dingjian/44/-rw-r--r-- root/root          0 2013-12-04 09:04 ./dingjian/q3.4drwxr-xr-x  root/root         0 2013-12-04 09:02 ./ dingjian/22/


Tar example ten: Package backup the current directory under the Dingjian folder, but do not pack the files in each file that starts with Q

[[email protected] tmp]# tree dingjiandingjian├── 11├── 22├── 33├──  44├── 55├── aa├── bb├── cc├── dd├── ee├── linuxfile├── q1├──  q2└── q3.4[[email protected] tmp]# tar --exclude=./dingjian/q*  -zcvf  myfile.tar.gz ./dingjian ./dingjian/./dingjian/33/./dingjian/aa./dingjian/11/./dingjian/55/./ Dingjian/dd./dingjian/ee./dingjian/linuxfile./dingjian/cc./dingjian/bb./dingjian/44/./dingjian/22/[[email  protected] tmp]# tar -ztvf myfile.tar.gz drwxr-xr-x root/root          0 2013-12-04 09:04 ./dingjian/drwxr-xr-x root/ root         0 2013-12-04 09:02 ./dingjian/33/- Rw-r--r-- root/root         0 2013-12-04 09:02  ./dingjian/aadrwxr-xr-x root/root         0 2013-12-04 09:02 ./dingjian/11/ Drwxr-xr-x root/root         0 2013-12-04 09:02  ./dingjian/55/-rw-r--r-- root/root         0  2013-12-04 09:02 ./dingjian/dd-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/ee-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/linuxfile-rw-r--r-- root/root          0 2013-12-04 09:02 ./dingjian/ Cc-rw-r--r-- root/root         0 2013-12-04 09:02  ./dingjian/bbdrwxr-xr-x root/root         0  2013-12-04 09:02 ./dingjian/44/dRwxr-xr-x root/root         0 2013-12-04 09:02  ./dingjian/22/

Tar Example 11: Working Environment instance:
Package The Site Directory/var/www/html back up to the/data directory (preferably every time you backup
Generate a different backup package)

[Email protected] tmp]# TAR-ZCVF html_$ (date +%f). tar.gz/var/www/htmltar:removing leading '/' from member NAMES/VAR/WW W/html/[[email protected] tmp]# lshtml_2013-12-04.tar.gz

Tar Example 12: Unpack the/etc/directly under/TMP without generating files!
[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!
Note that the output file becomes-and the input file becomes-and another | exist ~
# This represents the standard output, the standard input and the pipeline command!

This article is from the "Mr.xiong's OPS log" blog, so be sure to keep this source http://mrxiong2017.blog.51cto.com/12559394/1924197

Linux Decompression and Decompression tar

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.