Linux tar Command pack directory and exclude directory package

Source: Internet
Author: User
Tags bz2 gz file pack

Packing the compressed directory is very simple as follows

The code is as follows Copy Code


TAR-CZF small.tar.gz Small (directory name), compressing and packaging catalogs


More examples

The code is as follows Copy Code

Example one: Pack all the files in the/etc directory into/tmp/etc.tar
[Root@linux ~]# tar-cvf/tmp/etc.tar/etc <== only packaged, not compressed!
[Root@linux ~]# tar-zcvf/tmp/etc.tar.gz/etc <== packaged, gzip compressed
[Root@linux ~]# tar-jcvf/tmp/etc.tar.bz2/etc <== after packaging to bzip2 compression
# Note that the file name after the parameter F is taken by itself, and we are accustomed to using the. Tar as an identification.
# If you add the z parameter, the. tar.gz or. tgz to represent the gzip-compressed tar file ~
# If you add the J parameter, use the. tar.bz2 as the file name.
# The above instruction will display a warning message when it is executed:
# "tar:removing leading '/' from member names" That's a special set of absolute paths.
Tar Example two: What documents are available in the above/tmp/etc.tar.gz file?
[Root@linux ~]# tar-ztvf/tmp/etc.tar.gz
# Since we use gzip compression, we need to check the files in the tar file
# We're going to have to add the Z parameter! It's very important!
Tar example three: Unzip 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
# in the preset case, we can release the compressed file anywhere! In this example,
# I first transform the working directory underneath the/USR/LOCAL/SRC and unlock the/tmp/etc.tar.gz,
# then untie the catalog will be in/USR/LOCAL/SRC/ETC! In addition, if you enter the/USR/LOCAL/SRC/ETC
# you will find that the file attributes in this directory may be different from the/etc/.
Tar example four: under/tmp, I just want to untie 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 check the file name in the Tarfile through TAR-ZTVF, if only one file
# you can make it through this way! Notice that! The root directory within the etc.tar.gz/was taken away!
Tar example five: Back up all the files in the/etc/and save their permissions!
[Root@linux ~]# 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 six: In/home, a new file is backed up in 2005/06/01
[Root@linux ~]# tar-n ' 2005/06/01′-ZCVF home.tar.gz/home
Tar example seven: I want to back up/home,/etc, but don't/home/dmtsai
[Root@linux ~]# TAR–EXCLUDE/HOME/DMTSAI-ZCVF myfile.tar.gz/home/*/etc
Tar example eight: Unpack the/etc/and untie it directly below/tmp without producing a file!
[Root@linux ~]# Cd/tmp
[Root@linux tmp]# tar-cvf–/etc | TAR-XVF-

Tar package exclude Directory


Test passed
Directory misc is located in the Web directory, packaged misc directory, but do not misc directory under the CKEditor directory, the following command
(Note that there is no backslash behind the folder, and there is no absolute difference)

The code is as follows Copy Code

cd/var/www/html/web/
TAR-ZCVF misc.tar.gz Misc--exclude=misc/ckeditor


Don't look at the contents of the following ^_^

Therefore, some directories need to be excluded
Previously seen on the Internet a method is this

The code is as follows Copy Code
TAR-ZCVF dayanmei.com.tar.gz--exclude/dayanmei.com/cache/dayanmei.com/"--is two '--'"

Put the directory you want to exclude at the end

The code is as follows Copy Code
TAR-ZCVF dayanmei.com.tar.gz/dayanmei.com/--exclude/dayanmei.com/cache "--is two '--'"

Tar Example VII: I want to back up/home,/etc, but not/home/dayanmei.com (excluding multiple directories needs to be added –exclude before excluding directories)

The code is as follows Copy Code
[Root@linux ~]# TAR--EXCLUDE/HOME/DAYANMEI.COM-ZCVF myfile.tar.gz/home/*/etc

That's how I understand it:

The position of the parameter is not important, it is important to follow the command format

Tar is followed with the parameter tar, followed by the file name to be compressed;
Exclude, behind the table is the directory to ignore.

That is, it can be written like this:

The code is as follows Copy Code
Tar zcvf myfile.tar.gz dir1 dir2–exclude dir1/dir3 dir2/dir4

It can also be written like this:

The code is as follows Copy Code
Tar zcvf myfile.tar.gz–exclude dir1/dir3 dir2/dir4 dir1 dir2

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.